c++ - cin doesn't function properly while reading math operations -
i tried write function chop expression tokens below.
while(true) { cin >> d_tmp; if(!cin){ cin.clear(); cin >> ch_tmp; cout << ch_tmp << endl; } else { cout << d_tmp << endl; } } however, function didn't work expected. worked fine when entered sequence of random number , character when typed in "a 3 b" returns 'a' '3' , 'b', when typed "3 + 4", return '3' , '4'.
i've tried several testcases following code. seems if want program print '3' '+' '4', have type in "3 ++ 4". totally confuses me. have idea on this??? thanks!
you state types of variables are:
int d_tmp; char ch_tmp; with input 3 + 4, execution goes follows:
cin >> d_tmp;reads 3, reads space, stopsif(!cin)false, goes else, , prints3cin >> d_tmp;reads+, reads space, errorsif(!cin)truecin >> ch_tmp;reads4
the number parser of input stream treats +12 number, consume + if sees it.
Comments
Post a Comment