2

我目前有这个代码:

#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

我不确定我做错了什么。

4

7 回答 7

7

如前所述,编辑

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

在 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
于 2013-09-19T22:27:30.660 回答
5

end1应该endl不然会出现这个错误。

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

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

于 2013-09-19T22:21:22.450 回答
2

第一个错误是因为您使用 end1<---number 1 而不是 endl<---小写 L

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

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

于 2013-09-19T22:26:52.750 回答
1

;在行尾有,但cout在开头没有。一旦你到达;行尾,它就是一个新语句,编译器不知道如何处理类似的东西<<"a > b is " << a << ">",因为至少应该在<<-左边有一些东西左右的<<也应该是操作者可以接受的<<

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

[正如其他人指出endl的那样,与end1]

于 2013-09-19T22:24:12.213 回答
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;
}
于 2016-07-17T06:22:12.273 回答
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;
}

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

于 2015-11-27T21:06:45.950 回答
-1

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

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

于 2013-09-19T22:23:13.597 回答