繁体   English   中英

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

[英]Basic questions on C++ operators?

我目前有这个代码:

#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;
}

我收到这些错误

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

我不确定我做错了什么。

如上所述,编辑

  • end1 - > endl助记符:行尾
  • 删除分号(分号结束语句,下一个语句将以<<开头
  • 括号子表达式(为了获得优先权)

在科利鲁看到它

#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;
}

输出:

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应该是endl否则会发生此错误。

该错误解释了编译器如何不知道end1是什么,例如在范围中找不到。 它只是一个错误拼写的endl ,它确实存在于范围内。

此外,您需要在每行前面使用cout ,或者在最终输出之前不要使用分号。

第一个错误是因为你使用end1 <--- number 1而不是endl <--- lowercase L.

剩下的错误是因为你用分号终止了那一行。 如果你想继续输出作为一个长表达式,不要在末尾使用分号。 可能更具可读性的是在每个行的开头使用cout。 像这样:

cout <<“A is”<< a <<“\\ tB is”<< b << endl; cout <<“a和b之和等于”<< a <<“+”<< b << <<“,结果是”<< a + b << endl; cout <<“a和b的乘积等于”<< a <<“*”<< b << <<“,结果是”<< a * b << endl;

你有; 在行尾,但开头没有cout 一旦你到达了; 在行尾,它是一个新的语句,并且编译器不知道如何处理类似<<"a > b is " << a << ">" ,因为至少应该有东西的左<< -和任何上的左,右<<也应该是可以接受的运营商<<

通过在相关位置的<<前面添加cout来修复它 - 或者通过删除它使其成为一个非常长的行; - 但如果你有一个endl ,那么你也可以开始一个新的cout线。

[正如其他人所指出的那样, 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;
}

你写了'end1'代表新行.bt正确的关键字是'endl'(行尾)。解决方案在上面......

这是解决方案....

#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;
}

首先,它是endl而不是end1 (最后一个char是L而不是1)

其次,你必须把cout放在每一行的前面,这一行以<<运算符开头。 然后它会工作。

暂无
暂无

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

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