What's wrong with cin >> num1, num2?

后端 未结 3 1973
予麋鹿
予麋鹿 2021-01-26 05:42
#include 
using namespace std;

int main() {
  char choice;
  int solution, num1, num2;

  cout << \"Menu\";
  cout << \"\\n========\";
  cou         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-26 06:22

    cin >> num1, num2;
    

    This syntax is not correct for what you want. To chain, use

    cin >> num1 >> num2;
    

    If you compile with warnings you'll get notified about this by the compiler

    int a{}, b{};
    std::cin >> a, b;
    

    gives the error:

    warning: right operand of comma operator has no effect [-Wunused-value]
    std::cin >> a, b;
    

    The whole statement is parsed as

    ((std::cin >> a), b);
    

    Which consists of two comma-separated expressions. The b in that case doesn't have any effect. If you print the variables after the std::cin line above, you will always get 0 for b

提交回复
热议问题