I am using VS2K12 version 4.5.50709.
I am working Prata's C++ Primer 6ed. The exercise sequence of Listing 6.9 through Listing 6.11 involved using a switch statement with integer input (switch.cpp: 6.9), character input (switchchar.cpp: 6.10), and using integer input with enum (enum.cpp: 6.11). The main function of each was similar, especially in the usage of the input variable used by cin ("code" in the code provided below).
With switch.cpp, it was shown that entering a character caused the loop to cycle continuously. This was because the "cin >> choice" call failed to read the character into the int (choice) variable. The 'cin.fail() test followed by cin.clear().get() was added to switch.cpp to prevent the bad read from producing the loop spin.
However, in the original version of enum.cpp (below), which also uses integer input, if I input an 'm', the response is:
Enter color code (0-6): m
Bye!Press any key to continue . . .
In other words, with character input the int based read operation apparently did not fail. The main methods were very similar, but I noticed that switch.cpp used the "!=" operator in the while loop's test expression. So I tried it in enum.cpp and it is commented out in the code below. I found that with that code in place the inability to handle character input was present. When a character is input the failing response is:
Enter color code (0-6): Always include default case.
Enter color code (0-6): Always include default case.
Enter color code (0-6): Always include default case.
...
...
I assume this is a VS2K12 quirk, but I will likely try something like gcc at work tomorrow. I think it would be easy to chase this though the iostream code, for someone who has already been there. But it is going on 1AM, and I am not certain it would be time well spent (i.e. how often do you "really" need to debug iostream code?). I did use the debugger and see that (I believe) _OK is set to false. If I do not get a response I will likely look into it further this weekend. But if anyone has noticed this issue I would appreciated any info they have gathered on it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
// enum.cpp -- Listing 6.11 (PG 279): Using enum (091513)
#include <iostream>
// const int red = 0, orange = 1, ....
enum {red, orange, yellow, green, blue, violet, indigo};
int main() {
using namespace std;
int code;
cout << "Enter color code (0-6): "; // 1: Prompt
cin >> code; // 2: Read input
while (code >= red && code <= indigo) {
// while (code != red) {
switch (code) {
case red: cout << "Her lips were red.\n"; break;
case orange: cout << "Her hair was orange.\n"; break;
case yellow: cout << "Her shows were yellow.\n"; break;
case green: cout << "Her nails were green.\n"; break;
case blue: cout << "Her sweatsuite was blue.\n"; break;
case violet: cout << "Her eyes were violet.\n"; break;
case indigo: cout << "Her mood was indigl.\n"; break;
default: cout << "Always include default case.\n";
}
cout << "Enter color code (0-6): "; // 1: Prompt
cin >> code; // 2: Read input
}
cout << "Bye!";
return 0;
}
|