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

时间:2013-09-19 22:20:24

标签: 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

我不确定我做错了什么。

7 个答案:

答案 0 :(得分:7)

如上所述,编辑

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

查看 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;
}

输出:

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

答案 1 :(得分:5)

end1应为endl,否则会发生此错误。

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

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

答案 2 :(得分:2)

第一个错误是因为你使用了end1&lt; --- number而不是endl&lt; --- lowercase L

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

cout&lt;&lt; “A is”&lt;&lt; a&lt;&lt; “\ tB是”&lt;&lt; b&lt;&lt; ENDL; cout&lt;&lt;“a和b的和等于”&lt;&lt; a&lt;&lt; “+”&lt;&lt; b&lt;&lt; “结果是”&lt;&lt; a + b&lt;&lt; ENDL; cout&lt;&lt;“a和b的乘积等于”&lt;&lt; a&lt;&lt; “*”&lt;&lt; b&lt;&lt; “结果是”&lt;&lt; a * b&lt;&lt; ENDL;

答案 3 :(得分:1)

您在行尾有;,但开头没有cout。一旦到达行尾的;,它就是一个新语句,并且编译器不知道如何处理像<<"a > b is " << a << ">"这样的东西,因为至少应该有一些东西在<<的左侧 - <<左侧和右侧的任何内容也应为运营商<<所接受。

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

[和其他人一样指出,endlend1不一样

答案 4 :(得分:1)

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

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

答案 5 :(得分:1)

这是解决方案......

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

答案 6 :(得分:-1)

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

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