How to take different integer input using different variable separated by space in c++

suppose i have to take 2 integer input seprated by space and also both the input is used for calculation , how this could be achieved?

int a,b;
cin >> a >> b;

Is this what you want?

1 Like
int a,b;
char ch;
cin>>a>>ch>>b;

No, that won’t work (assuming I’m understanding the OPs requirements correctly):

[simon@simon-laptop][07:59:39]
[~/devel/hackerrank/otherpeoples]>cat ssrivastava990-read-integers.cpp 
#include <iostream>

using namespace std;

int main()
{
    int a,b;
    char ch;
    cin>>a>>ch>>b;

    cout << "a: " << a << " b: " << b << endl;
}


[simon@simon-laptop][07:59:45]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling ssrivastava990-read-integers.cpp
Successful
[simon@simon-laptop][07:59:49]
[~/devel/hackerrank/otherpeoples]>echo "12 34" | ./a.out 
a: 12 b: 4

2 Likes

Wait I read two intgers separated by comma in c++ , so I wrote this , if

then

int a , b;
cin>>a>>b;

isn’t it ?

1 Like

Yes, and your original code would be correct for comma-separated integers :slight_smile:

3 Likes