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, stops
  • if(!cin) false, goes else, , prints 3
  • cin >> d_tmp; reads +, reads space, errors
  • if(!cin) true
  • cin >> ch_tmp; reads 4

the number parser of input stream treats +12 number, consume + if sees it.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -