home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / gnu / gcc / bug / 2337 < prev    next >
Encoding:
Text File  |  1992-09-14  |  5.4 KB  |  187 lines

  1. Newsgroups: gnu.gcc.bug
  2. Path: sparky!uunet!cis.ohio-state.edu!diag.amdahl.COM!brm30
  3. From: brm30@diag.amdahl.COM (Brian Moyle)
  4. Subject: "cin" bug?
  5. Message-ID: <9209150106.AA15049@brain.diag.amdahl.com>
  6. Sender: gnulists@ai.mit.edu
  7. Organization: GNUs Not Usenet
  8. Distribution: gnu
  9. Date: Tue, 15 Sep 1992 01:06:41 GMT
  10. Approved: bug-gcc@prep.ai.mit.edu
  11. Lines: 174
  12.  
  13. Hello,
  14.  
  15.      I'm fairly new to C++ programming, but I've run into a little
  16. problem that seems to qualify as a bug.  The following program was 
  17. taken from The Waite Group's, "C++ Programming," by John Berry,
  18. copyright 1988 (fourth printing -- 1990).  On page 44, the following
  19. paragraph provides an explanation of how the 'while' loop is exited:
  20.  
  21. "The program also uses a trick to develop an easy user interface.
  22. Recall that a character is actually an integer value.  Because
  23. implicit type conversion is easy and unregulated in C++, the program
  24. can accept as input the value 'exit.'  The 'e' is converted from its
  25. integer representation into a real number.  The same thing happens to
  26. the 'i'.  However, the value 'x' is stored in the character variable
  27. 'opr'.  This value then triggers the exit clause of the 'switch'."
  28.  
  29.      When I run this program, and type "exit" to terminate the loop, 
  30. the program seems to ignore any future attempts at input via the "cin" 
  31. routine.  Am I violating some rule in C++ which I don't yet understand?
  32.  
  33.      To the best of my knowledge, I am running gcc 2.2.2 on a Sun 
  34. SPARCstation IPC.  Here are the results of compiling with the "-v" 
  35. option:
  36.  
  37. ~/c++_prog > gcc -v calc.C -lg++
  38. Reading specs from /usr/local/pswd/lib/gcc-lib/sun4/2.2.2/specs
  39. gcc version 2.2.2
  40.  /usr/local/pswd/lib/gcc-lib/sun4/2.2.2/cpp -lang-c++ -v -undef
  41.  -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -Dsparc -Dsun -Dunix
  42.  -D__sparc__ -D__sun__ -D__unix__ -D__sparc -D__sun -D__unix calc.C
  43.  /usr/tmp/cca13438.i
  44. GNU CPP version 2.2.2 (sparc)
  45.  /usr/local/pswd/lib/gcc-lib/sun4/2.2.2/cc1plus /usr/tmp/cca13438.i
  46.  -quiet -dumpbase calc.cc -version -o /usr/tmp/cca13438.s
  47. GNU C++ version 2.2.2 (sparc) compiled by GNU C version 2.2.2.
  48.  as -o calc.o /usr/tmp/cca13438.s
  49.  /usr/local/pswd/lib/gcc-lib/sun4/2.2.2/ld -e start -dc -dp
  50.  /lib/crt0.o -L/usr/local/pswd/lib/gcc-lib/sun4/2.2.2
  51.  -L/usr/local/pswd/lib calc.o -lg++ -lgcc -lc -lgcc
  52.  
  53. Also, this might be useful:
  54.  
  55. ~/c++_prog > uname -a
  56.    SunOS bmoyle 4.1.1 1 sun4c
  57.  
  58.  
  59. Hopefully, this is enough information.  Please send any
  60. recommendations to:
  61.  
  62.           brm30@diag.amdahl.com
  63.  
  64.  
  65. Thanks, in advance, for the help!
  66.  
  67. Brian Moyle
  68. (408)992-3185
  69. brm30@diag.amdahl.com
  70.  
  71.  
  72. *********************************************************************
  73.  
  74.  
  75. program input/output:
  76. ----------------------------------------------------------------------
  77. enter expression 5+3
  78. x=5  y=3  opr=+
  79. =8
  80.  
  81. enter expression 8*2
  82. x=8  y=2  opr=*
  83. =16
  84.  
  85. enter expression exit
  86. x=8  y=2  opr=*
  87. =16
  88.  
  89. enter expression x=8  y=2  opr=*
  90. =16
  91.  
  92. enter expression x=8  y=2  opr=*
  93. =16
  94.  
  95. enter expression x=8  y=2  opr=*
  96. =16
  97.  
  98. enter expression x=8  y=2  opr=*
  99. =16
  100.  
  101.                .
  102.                .      (continues until terminated)
  103.                .
  104.  
  105. ----------------------------------------------------------------------
  106.  
  107.  
  108.  
  109. program listing: calc.C
  110. ----------------------------------------------------------------------
  111. #include <stream.h>
  112.  
  113. // define some useful substitutions
  114.  
  115. #define BLANK ' '
  116. #define STOP 0
  117.  
  118. main ()
  119. {
  120.    double x,y,
  121.           radd(double, double), // declare a simple add function,
  122.           rsub(double, double), // ... a subtraction function,
  123.           rmul(double, double), // ... multiplication, ...
  124.           rdiv(double, double); // ... and finally, a division function.
  125.    char opr = BLANK;      // declare a variable for the operator
  126.  
  127.    while (opr != STOP)  {       // continue until the user says "exit"
  128.       cout << "enter expression ";   // prompt...
  129.       cin >> x >> opr >> y;          // ... read ...
  130.  
  131.       // I added this line for "debugging"
  132.       cout << "x=" << x << "  y=" << y << "  opr=" << opr << endl;
  133.  
  134.       switch (opr)  {      // ... and evaluate
  135.          case '+' : cout << "=" << radd(x,y);   // create a clause for each
  136.                     break;                      // operation
  137.          case '-' : cout << "=" << rsub(x,y);
  138.                     break;
  139.          case '*' : cout << "=" << rmul(x,y);
  140.                     break;
  141.          case '/' : cout << "=" << rdiv(x,y);
  142.                     break;
  143.          case 'x' : opr = STOP;      //... a cheap trick, "x" is the
  144.                     break;           // second letter in "exit"
  145.          default  : cout << "not yet implemented!\n"; // unbounded optimism
  146.          }  // switch
  147.  
  148.       cout << "\n\n";
  149.  
  150.    }  // while
  151. }  // main()
  152.  
  153. //////////////////////////////////////////////////////////////////////
  154.  
  155. double radd(double a, double b)   // a function to add doubles
  156. {
  157.    return(a+b);
  158. }
  159.  
  160. //////////////////////////////////////////////////////////////////////
  161.  
  162. double rsub(double a, double b)   // ... one to subtract them
  163. {
  164.    return(a-b);
  165. }
  166.  
  167. //////////////////////////////////////////////////////////////////////
  168.  
  169. double rmul(double a, double b)   // ... multiply ...
  170. {
  171.    return(a*b);
  172. }
  173.  
  174. //////////////////////////////////////////////////////////////////////
  175.  
  176. double rdiv(double a, double b)   // ... and divide
  177. {
  178.    if (b == 0)   // don't forget to check for division by zero.
  179.       return 0;
  180.    return(a/b);
  181. }
  182.  
  183. //////////////////////////////////////////////////////////////////////
  184.  
  185. ----------------------------------------------------------------------
  186.  
  187.