+ 2

How to get numbers that separated by ","

for example they give us something like this 12,25,36: and we want to get and save them but they separated by "," and we should have three integers 12 , 25 and 36

21st Aug 2017, 4:44 AM
Mohammad Aghaie
Mohammad Aghaie - avatar
6 Answers
+ 1
//I assume we have the correct headers string s; int n = 0; bool last_was_comma = true; cin>>s; for(char c : s){ if( c == ',' && !last_was_comma ){ cout<<n<<endl; n = 0; last_was_comma = true; }else if( c <= '9' && c >= '0' ){ last_was_comma = false; n = n*10 + static_cast<int>(c - '0'); }else if( c != ',' ){ //error management, you can end the program, just break the loop, .... } } if( !last_was_comma) cout<<n<<endl;
21st Aug 2017, 6:05 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 8
Say for example you have 122536 as an integer value. Save it to a string variable using stringstream. Write the string to another string variable using a loop and add ',' for every two iterations.
21st Aug 2017, 4:47 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Did it helped ?
22nd Aug 2017, 5:53 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
you didn't get my question. we usually enter inputs that separeted by space but now they separated by comma
21st Aug 2017, 4:51 AM
Mohammad Aghaie
Mohammad Aghaie - avatar
+ 1
yes. thanks
22nd Aug 2017, 6:16 PM
Mohammad Aghaie
Mohammad Aghaie - avatar
+ 1
You are welcomed ! :D
22nd Aug 2017, 6:51 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar