+ 1

How to make use of the comma separator for cin >>

The result for the below code is as expected if the user input is separated by 1) spaces or 2) a new line. However, it fails if the user input is either 3) not separated by spaces or 4) separated by commas. How could I adjust the code such that it would give the correct output for 4)? #include <iostream> using namespace std; int main() { int a, b; int c, d; cin >> a >> b >> c >> d; double A = a*b; double B = c*d; if (B > A) { cout << "BBB"; } else { cout << "AAA"; } return 0; }

4th Aug 2020, 11:31 PM
Solus
Solus - avatar
5 Respostas
4th Aug 2020, 11:47 PM
BroFar
BroFar - avatar
+ 1
~ swim ~ Thank you so much! So the dummy char method only works if the input is of a specified, fixed format in accordance to where the dummy char is placed relative to the other input variables. Just curious, would there be an effective way to allow the computer to recognize "non-fixed inputs"? That is, it could recognize the input of: 1 2 3 4 1,2,3,4 1,2 3,4 1 2,3 4 etc... as all of the same thing (grouping the former two together and latter two together)
5th Aug 2020, 12:36 AM
Solus
Solus - avatar
+ 1
Solus char ch; //dummy char for reading ',' int a, b, c, d; char c2 = ":"; cin >> a >> ch >> b >> ch >> c >> ch >> d; cout << a << c2 << b << c2 << c << c2 << d << endl; input: 3, 7, 2, 8 output: 3:7:2:8 There are more ways to do this as ~ swim ~ mentioned cin >> a >> b >> ch >> c >> d;
5th Aug 2020, 12:48 AM
BroFar
BroFar - avatar