简体   繁体   English

关于C ++运算符的基本问题?

[英]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 ) end1 - > endl助记符:行尾
  • 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. end1应该是endl否则会发生此错误。

The error is explainnig how the compiler does not know what end1 is, eg not found in the scope. 该错误解释了编译器如何不知道end1是什么,例如在范围中找不到。 It is simply a misspelling of endl , which actually does exist in the scope. 它只是一个错误拼写的endl ,它确实存在于范围内。

Additionally, you need cout in front of each line or not have semicolons until the final output. 此外,您需要在每行前面使用cout ,或者在最终输出之前不要使用分号。

The first error is because you used end1<---number 1 instead of endl<---lowercase L 第一个错误是因为你使用end1 <--- number 1而不是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. 可能更具可读性的是在每个行的开头使用cout。 Like this: 像这样:

cout << "A is " << a << "\\tB is " << b << endl; 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 <<“a和b之和等于”<< a <<“+”<< b << <<“,结果是”<< a + b << endl; cout <<"Product of a and b is equal to " << a << "*" << b << "and the result is " << a * b << endl; cout <<“a和b的乘积等于”<< a <<“*”<< b << <<“,结果是”<< a * b << endl;

You have ; 你有; at the end of the lines, but no cout at the beginning. 在行尾,但开头没有cout 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 << . 在行尾,它是一个新的语句,并且编译器不知道如何处理类似<<"a > b is " << a << ">" ,因为至少应该有东西的左<< -和任何上的左,右<<也应该是可以接受的运营商<<

Fix it by adding cout in front of the << in relevant places - or make it a really long line by removing the ; 通过在相关位置的<<前面添加cout来修复它 - 或者通过删除它使其成为一个非常长的行; - but if you have an endl , then you may just as well start a new cout line. - 但如果你有一个endl ,那么你也可以开始一个新的cout线。

[And as others have pointed out, endl is not the same as end1 ] [正如其他人所指出的那样, endlend1 ]

#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... 你写了'end1'代表新行.bt正确的关键字是'endl'(行尾)。解决方案在上面......

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) 首先,它是endl而不是end1 (最后一个char是L而不是1)

secondly, you will have to put cout in front of every line, which begin with the << operator. 其次,你必须把cout放在每一行的前面,这一行以<<运算符开头。 Then it will work. 然后它会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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