0

Why does this receive only one integer?
Here is the code:

#include <iostream>

int main () {
    int num1,num2,num3;
    std::cin>>num1,num2,num3;

    return 0;
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Mohamed Magdy
  • 345
  • 3
  • 15

2 Answers2

6

According to the Operator Precedence, comma operator has lower precedence than operator>>, so std::cin>>num1,num2,num3; is same as (std::cin>>num1), num2, num3;; the following num2, num3 does nothing in fact. (More precisely, std::cin>>num1 is evaluated firstly and its result is discarded; then num2 is evaluated, num3 is evaluated at last and its value is the result of the whole comma expression.)

What you want should be std::cin >> num1 >> num2 >> num3;.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
0

That is not the correct syntax. That's an application of the comma operator. You want

std::cin >> num1 >> num2 >> num3; 
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116