Jump to content
  • Advertisement
Sign in to follow this  
Serendipity

Switch Problem

This topic is 3636 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I'm following the examples in a book, however, I can't seem to get my switch to display the correct results. Ever answer in input for this function displays the default. I've tried changing grade from int type to char, but I get the same results. Please help.
void GradeBook::inputGrades()
{
	char grade;

	cout<<"Enter the letter grades." << endl
		<<"Enter the EOF character to end input."<<endl;

	//loop until user types EOF key sequence
	while((grade = cin.get() ) != EOF)
	{
		switch(grade)
		{
		case 'A':
		case 'a':
			aCount++;
			break;
		case 'B':
		case 'b':
			bCount++;
			break;
		case 'C':
		case 'c':
			cCount++;
			break;
		case 'D':
		case 'd':
			dCount++;
			break;
		default:
			cout<<"You have entered an invalid grade.\n"
				<<"Enter a new grade."<<endl;
				break;

		}//end switch
	}//end while
}//end inputGrades()

[Edited by - Serendipity on November 2, 2008 9:18:44 AM]

Share this post


Link to post
Share on other sites
Advertisement
0) Post your code in between [ source] [ /source] blocks (without spaces).


1) What compiler are you using? If you're using MSVC, it's a simple matter of putting a breakpoint on the switch statement and taking a look. If you're using g++ or other, it's a more complicated matter of putting a breakpoint on the switch statement and taking a look.

Share this post


Link to post
Share on other sites
Quote:
Original post by Serendipity
Ever answer in input for this function displays the default.

That's what I would expect. When you type the letter A followed by the return key, you get two characters, the letter A and the newline character.

Quote:
Original post by Serendipity
I've tried changing grade from int type to char

Not a good idea, because a certain valid char would be interpreted as EOF. Leave it at int.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
That's what I would expect. When you type the letter A followed by the return key, you get two characters, the letter A and the newline character.


That's what it was. I added another case to handle the newline, tab, and space characters. It works now. Thanks!

Quote:
Original post by DevFred
Not a good idea, because a certain valid char would be interpreted as EOF. Leave it at int.


I see. I changed it back to int.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!