+ 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
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;
+ 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.
+ 2
Did it helped ?
+ 1
you didn't get my question. we usually enter inputs that separeted by space but now they separated by comma
+ 1
yes. thanks
+ 1
You are welcomed ! :D