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

标签 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 (助记符:行尾)
  • 删除分号(分号结束语句,下一条语句将开始 << ?)
  • 将子表达式括起来(为了获得正确的优先级)

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

关于c++ - 关于 C++ 运算符的基本问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18905859/

相关文章:

Javascript 编程 : Is dot [.] 总是一个运算符(operator)?

c++ - 链接错误lnk2019

c++ - boost::promise::set_exception() 编译错误

c# - 定义一个实现 + 运算符的泛型

c++ - 运算符重载的基本规则和惯用法是什么?

javascript - Google 脚本 - 合并运算符语法在 IF 语句中不起作用

c++ - 有没有办法使用 MPI 在并行进程上共享内存?

c++ - 如何将字符空间动态分配给结构字段

java - SWIG - Java 代理类数组参数

斯卡拉 : Is there a better way to evaluate an expression tree?