I have some Visual Basic programing experience but I am new to C++ I am working on a homework assignment and I am not sure why my code is not working out. I am trying to write a program that changes numbers form 1 to one , 2 to two,... I have got it to work and limited the choices to 1-5 and got that part to work as well. The problem I am running into is that if I enter 21 on a line it reads it as 2- two and 1 one and does not give me the inappropate character entered line that I want I though using char instead of int would fix that but it did not. here is my code can anyone one tell me what I am doing wrong.

#include <iostream>


using std::cout;
using std::cin;
using std::endl;

int main()
{

char number ;

cout << "enter the number between 1 and 5 to be switched."<< endl
	 << "enter the EOF character to end input." << endl;
while ( (number = cin.get () ) != EOF ) {

	switch ( number ) {

		case '1': // number entered is a one
		cout << "The number you entered was ONE.\n";
			break;

		case '2':  // number two entered
		cout << "The number you entered was TWO.\n";
			break;

		case '3':  // number two entered
		cout << "The number you entered was THREE.\n";
			break;

		case '4':  // number two entered
		cout << "The number you entered was FOUR.\n";
			break;

		case '5':  // number two entered
		cout << "The number you entered was FIVE.\n";
			break;

		case '\n': // ignore newlines,
		case '\t': // tabs,
		case ' ':  // and spaces in input
			break; // exit

		default:  //catch all other characters
			cout << " Incorrect character entered."
		<< "  Enter a new number between 1 and 5." <<endl;
			break;
	}  // ends switch

} // ends while

return 0;  // indicates successful termination
} // ends main function

Thanks for any suggestions

Recommended Answers

All 3 Replies

I read the link and it references another link that did not work I still do not understand what I need to do. I do not know much about %
In the link that worked the poster stated that they assumed that 2 digits were entered. I am trying not to make the assumption that only 1 number is entered at a time

Well I made some changes in my program with a few suggestions from a friend and got the program to work like I wanted.
I took out the while so that now I can only enter one entry each time through the probram. after reading the assignment again I decided it was not necessary to have it loop. Then I changed
char number to int number changed the cases from
case '1' to case 1 ... and it works great. I am posting this so if someone else runs into this they can see how I got it to work. I still want to get the While statement to work but I think that will come in a few chapters untill then I will continue to fiddle with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.