My code needs to output a char for the user's integer input. Basically, when the user types 1, the output will be one -- only in the range of 0 to 9 though. Here's what I have thus far, but for some reason when I type any char such as "zero, one, two, ..." it goes on an infinite loop, basically breaking the logic of the program. In keeping within the integrity of learning and the forum's motto of "Don't tell us to give you code, we won't do your homework, etc.", can anyone point me in the direction on how I could solve this on my own? So far I've tried adding a return 1 within the do branch, as well as at the end but I know that probably won't do anything useful for my problem.
#include<iostream> using namespace std; int main() { int grade; do { cout << "Please input a single-digit positive integer.\n"; cout << "Input any negative number to quit the program.\n"; cin >> grade; switch (grade) { case 0: cout << "Your number is zero.\n"; break; case 1: cout << "Your number is one.\n"; break; case 2: cout << "Your number is two.\n"; break; case 3: cout << "Your number is three.\n"; break; case 4: cout << "Your number is four.\n"; break; case 5: cout << "Your number is five.\n"; break; case 6: cout << "Your number is six.\n"; break; case 7: cout << "Your number is seven.\n"; break; case 8: cout << "Your number is eight.\n"; break; case 9: cout << "Your number is nine.\n"; break; default: cout << ""; } //While statement to end the program if user inputs a number less than 0. }while(grade >= 0); cout << "Your number is negative. Now exit."; //End while statment. return 0;