home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13711 < prev    next >
Encoding:
Text File  |  1992-09-15  |  5.4 KB  |  185 lines

  1. Path: sparky!uunet!charon.amdahl.com!amdahl!JUTS!diag.amdahl.com!brm30
  2. From: brm30@diag.amdahl.com (Brian Moyle)
  3. Newsgroups: comp.lang.c++
  4. Subject: "cin" help requested
  5. Keywords: cin input problem
  6. Message-ID: <1cYd02P=22Zk01@JUTS.ccc.amdahl.com>
  7. Date: 15 Sep 92 21:47:13 GMT
  8. Sender: netnews@ccc.amdahl.com
  9. Organization: Amdahl Corporation, Cupertino, CA
  10. Lines: 173
  11.  
  12. Hello,
  13.  
  14.    I'm fairly new to C++ programming, but I've run into a little 
  15. problem that seems to qualify as a bug (or ignorance on my part).  The 
  16. following program was taken from The Waite Group's, "C++ Programming," 
  17. book, by John Berry, copyright 1988 (fourth printing -- 1990).  On page 
  18. 44, the following paragraph provides an explanation of how the 'while' 
  19. loop exits:
  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 implicit 
  23. type conversion is easy and unregulated in C++, the program can accept 
  24. as input the value 'exit.'  The 'e' is converted from its integer 
  25. representation into a real number.  The same thing happens to the 'i'.  
  26. However, the value 'x' is stored in the character variable 'opr'.  This 
  27. value then triggers the exit clause of the 'switch'."
  28.  
  29.    When I run this program, and type "exit" to terminate the loop, the 
  30. 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 this newsgroup or:
  61.  
  62.           brm30@diag.amdahl.com
  63.  
  64. Thanks, in advance, for any help!
  65.  
  66. Brian Moyle
  67. (408)992-3185
  68. brm30@diag.amdahl.com
  69.  
  70.  
  71. *********************************************************************
  72.  
  73.  
  74. program input/output:
  75. ----------------------------------------------------------------------
  76. enter expression 5+3
  77. x=5  y=3  opr=+
  78. =8
  79.  
  80. enter expression 8*2
  81. x=8  y=2  opr=*
  82. =16
  83.  
  84. enter expression exit
  85. x=8  y=2  opr=*
  86. =16
  87.  
  88. enter expression x=8  y=2  opr=*
  89. =16
  90.  
  91. enter expression x=8  y=2  opr=*
  92. =16
  93.  
  94. enter expression x=8  y=2  opr=*
  95. =16
  96.  
  97. enter expression x=8  y=2  opr=*
  98. =16
  99.  
  100.                .
  101.                .      (continues until terminated)
  102.                .
  103.  
  104. ----------------------------------------------------------------------
  105.  
  106.  
  107.  
  108. program listing: calc.C
  109. ----------------------------------------------------------------------
  110. #include <stream.h>
  111.  
  112. // define some useful substitutions
  113.  
  114. #define BLANK ' '
  115. #define STOP 0
  116.  
  117. main ()
  118. {
  119.    double x,y,
  120.           radd(double, double), // declare a simple add function,
  121.           rsub(double, double), // ... a subtraction function,
  122.           rmul(double, double), // ... multiplication, ...
  123.           rdiv(double, double); // ... and finally, a division function.
  124.    char opr = BLANK;      // declare a variable for the operator
  125.  
  126.    while (opr != STOP)  {       // continue until the user says "exit"
  127.       cout << "enter expression ";   // prompt...
  128.       cin >> x >> opr >> y;          // ... read ...
  129.  
  130.       // I added this line for "debugging"
  131.       cout << "x=" << x << "  y=" << y << "  opr=" << opr << endl;
  132.  
  133.       switch (opr)  {      // ... and evaluate
  134.          case '+' : cout << "=" << radd(x,y);   // create a clause for each
  135.                     break;                      // operation
  136.          case '-' : cout << "=" << rsub(x,y);
  137.                     break;
  138.          case '*' : cout << "=" << rmul(x,y);
  139.                     break;
  140.          case '/' : cout << "=" << rdiv(x,y);
  141.                     break;
  142.          case 'x' : opr = STOP;      //... a cheap trick, "x" is the
  143.                     break;           // second letter in "exit"
  144.          default  : cout << "not yet implemented!\n"; // unbounded optimism
  145.          }  // switch
  146.  
  147.       cout << "\n\n";
  148.  
  149.    }  // while
  150. }  // main()
  151.  
  152. //////////////////////////////////////////////////////////////////////
  153.  
  154. double radd(double a, double b)   // a function to add doubles
  155. {
  156.    return(a+b);
  157. }
  158.  
  159. //////////////////////////////////////////////////////////////////////
  160.  
  161. double rsub(double a, double b)   // ... one to subtract them
  162. {
  163.    return(a-b);
  164. }
  165.  
  166. //////////////////////////////////////////////////////////////////////
  167.  
  168. double rmul(double a, double b)   // ... multiply ...
  169. {
  170.    return(a*b);
  171. }
  172.  
  173. //////////////////////////////////////////////////////////////////////
  174.  
  175. double rdiv(double a, double b)   // ... and divide
  176. {
  177.    if (b == 0)   // don't forget to check for division by zero.
  178.       return 0;
  179.    return(a/b);
  180. }
  181.  
  182. //////////////////////////////////////////////////////////////////////
  183.  
  184. ----------------------------------------------------------------------
  185.