简体   繁体   中英

Basic questions on C++ operators?

I currently have this code:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int a;
cout << "Enter a number for a: "; //Prompts the user for a and b inputs
cin >> a;

int b;
cout << "Enter a number for b: ";
cin >> b;

cout << "A is " << a << "\tB is " << b << end1;
cout <<"Sum of a and b is equal to " << a << "+" << b << "and the result is " << (a + b) << end1;
cout <<"Product of a and b is equal to " << a << "*" << b << "and the result is " << (a * b) << end1;
cout <<"a > b is " << a << ">" << b << "and the result is " << (a > b) << end1;
cout <<"a < b is " << a << ">" << b << "and the result is " << (a < b) << end1;
cout <<"a == b is " << a << "==" << b << "and the result is " << (a == b) << end1;
cout <<"a >= b is " << a << ">=" << b << "and the result is " << (a >= b) << end1;
cout <<"a <= b is " << a << "<=" << b << "and the result is " << (a <= b) << end1;
cout <<"a != b is " << a << "!=" << b << "and the result is " << (a != b) << end1;

return 0;
}

and I'm receiving these errors

main.cpp: In function 'int main()':
main.cpp:10:44: error: 'end1' was not declared in this scope

I'm not sure what I'm doing wrong.

As mentioned, the edits

  • end1 -> endl ( mnemonic: end-of-line )
  • remove semicolons (semicolons end the statement, and the next statement would start with << ?)
  • and parenthesize sub expressions (in order to get the precedence right)

See it Live on Coliru

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int a;
    cout << "Enter a number for a: "; //Prompts the user for a and b inputs
    cin >> a;
    int b;
    cout << "Enter a number for b: ";
    cin >> b;
    cout << "A is "                           << a << "\tB is " << b << endl
         << "Sum of a and b is equal to "     << a << "+"       << b << "and the result is " << (a + b) << endl
         << "Product of a and b is equal to " << a << "*"       << b << "and the result is " << (a * b) << endl
         << "a > b is "                       << a << ">"       << b << "and the result is " << (a > b) << endl
         << "a < b is "                       << a << ">"       << b << "and the result is " << (a < b) << endl
         << "a == b is "                      << a << "=="      << b << "and the result is " << (a == b) << endl
         << "a >= b is "                      << a << ">="      << b << "and the result is " << (a >= b) << endl
         << "a <= b is "                      << a << "<="      << b << "and the result is " << (a <= b) << endl
         << "a != b is "                      << a << "!="      << b << "and the result is " << (a != b) << endl;
    return 0;
}

Output:

Enter a number for a: 3
Enter a number for b: 4
A is 3  B is 4
Sum of a and b is equal to 3+4and the result is 7
Product of a and b is equal to 3*4and the result is 12
a > b is 3>4and the result is 0
a < b is 3>4and the result is 1
a == b is 3==4and the result is 0
a >= b is 3>=4and the result is 0
a <= b is 3<=4and the result is 1
a != b is 3!=4and the result is 1

end1 should be endl otherwise this error occurs.

The error is explainnig how the compiler does not know what end1 is, eg not found in the scope. It is simply a misspelling of endl , which actually does exist in the scope.

Additionally, you need cout in front of each line or not have semicolons until the final output.

The first error is because you used end1<---number 1 instead of endl<---lowercase L

The remaining errors are because you terminated that line with a semicolon. If you want to continue the output as one long expression don'e use semicolons at the ends. Probably more readable would be to use cout at the beginning on each of those lines instead. Like this:

cout << "A is " << a << "\\tB is " << b << endl; cout <<"Sum of a and b is equal to " << a << "+" << b << "and the result is " << a + b << endl; cout <<"Product of a and b is equal to " << a << "*" << b << "and the result is " << a * b << endl;

You have ; at the end of the lines, but no cout at the beginning. Once you get to the ; at the end of the line, it's a new statement, and the compiler doesn't know how to deal with something like <<"a > b is " << a << ">" , because at the very least there should be something to the left of << - and whatever is on the left and right of << should also be acceptable to the operator << .

Fix it by adding cout in front of the << in relevant places - or make it a really long line by removing the ; - but if you have an endl , then you may just as well start a new cout line.

[And as others have pointed out, endl is not the same as end1 ]

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   int a;
   cout << "Enter a number for a: "; //Prompts the user for a and b   inputs
   cin >> a;

   int b;
   cout << "Enter a number for b: ";
   cin >> b;

   cout << "A is " << a << "\tB is " << b << endl;
   cout <<"Sum of a and b is equal to " << a << "+" << b << "and the   result is " << (a + b) << endl;
   cout <<"Product of a and b is equal to " << a << "*" << b << "and  the result is " << (a * b) << endl;
   cout <<"a > b is " << a << ">" << b << "and the result is " << (a > b) <<  endl;
   cout <<"a < b is " << a << ">" << b << "and the result is " << (a < b) << endl;
   cout <<"a == b is " << a << "==" << b << "and the result is " << (a == b) << endl;
   cout <<"a >= b is " << a << ">=" << b << "and the result is " << (a >= b) << endl;
   cout <<"a <= b is " << a << "<=" << b << "and the result is " << (a <= b) << endl;
   cout <<"a != b is " << a << "!=" << b << "and the result is " << (a != b) << endl;

   return 0;
}

u have written 'end1' to represent new line .bt the correct keyword is 'endl'(End of line).the solution is above...

it is the solution ....

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   int a;
   cout << "Enter a number for a: "; //Prompts the user for a and b   inputs
   cin >> a;

   int b;
   cout << "Enter a number for b: ";
   cin >> b;

   cout << "A is " << a << "\tB is " << b << endl;
   cout <<"Sum of a and b is equal to " << a << "+" << b << "and the   result is " << (a + b) << endl;
   cout <<"Product of a and b is equal to " << a << "*" << b << "and  the result is " << (a * b) << endl;
   cout <<"a > b is " << a << ">" << b << "and the result is " << (a > b) <<  endl;
   cout <<"a < b is " << a << ">" << b << "and the result is " << (a < b) << endl;
   cout <<"a == b is " << a << "==" << b << "and the result is " << (a == b) << endl;
   cout <<"a >= b is " << a << ">=" << b << "and the result is " << (a >= b) << endl;
   cout <<"a <= b is " << a << "<=" << b << "and the result is " << (a <= b) << endl;
   cout <<"a != b is " << a << "!=" << b << "and the result is " << (a != b) << endl;

   return 0;
}

first of all, it is endl and not end1 (the last char is a L not a 1)

secondly, you will have to put cout in front of every line, which begin with the << operator. Then it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM