Topic: Another C++ problem...


Author: grimaldo@nlsce1.seri.philips.nl (Emilio Grimaldo)
Date: Fri, 25 Feb 1994 13:23:56 GMT
Raw View
Hoi C++ gurus, this bug's for you (I THINK)...


a)     char rsp[2];
       do {
     cerr << "(A) or (B)? ";
     cin >> rsp;
     rsp[0] = (char)toupper(rsp[0]);
       } while (rsp[0] != 'A' && rsp[0] != 'B');

   Running this asks ONCE for the A or B, then whatever you enter
   is stored and converted, then regardless of the actual input
   A,B or any invalid char like X the program will go into a
   endless loop displaying the prompt in stderr, skiping the
   cin again and again, if rsp[0] has the right value (A or B)
   why the test fails? however.....


b)     char rsp[2];
       do {
     cerr << "(A) or (B)? ";
     cin >> rsp;
     rsp[0] = (char)toupper(rsp[0]);
     cout << "Uppase " << rsp[0] << endl;
       } while (rsp[0] != 'A' && rsp[0] != 'B');

 will not only print the correct uppercase letter, but
 will cause the test in the while statement to evaluate
 correctly, so.... What is this weird interaction
 between cout and the test?
--
NAME: Emilio Grimaldo              | Field Appl. Engineer |   _/_/_/_/
EMAIL: grimaldo@sce.philips.nl     | Telecom/CT1          |   _/_/
ADDRESS: Eindhoven,The Netherlands |                      |  _/_/_/_/
-----------------------------------+----------------------|  _/_/
 * * STRICTLY PERSONAL OPINIONS, I GOT MY OWN MIND :) * * | _/_/_/_/
----------------------------------------------------------+-----------
  Hi there!  enjoy reading this?
  "En b   t utan kapten aer som en man utan kvinna"
       \\v//
  Hullo, I'm just trying to look over the fence :)      (o o)
  ---------------------------------------------------ooO-(_)-Ooo------

--
NAME: Emilio Grimaldo              | Field Appl. Engineer |   _/_/_/_/
EMAIL: grimaldo@sce.philips.nl     | Telecom/CT1          |   _/_/
ADDRESS: Eindhoven,The Netherlands |                      |  _/_/_/_/
-----------------------------------+----------------------|  _/_/
 * * STRICTLY PERSONAL OPINIONS, I GOT MY OWN MIND :) * * | _/_/_/_/
----------------------------------------------------------+-----------
  Hi there!  enjoy reading this?
  "En b   t utan kapten aer som en man utan kvinna"
       \\v//
  Hullo, I'm just trying to look over the fence :)      (o o)
  ---------------------------------------------------ooO-(_)-Ooo------