Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Noky

Case and If statements

This topic is 5903 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

Okay I'm having a problem with my program. It's suppose to take your age and spit out some text depending on what your age is. But my problem is that It won't spit out the proper text after age 54. Can someone tell me what's wrong. Everything complies and I can see what is wrong. I know all those case statements are messy but I didn't want to do a bunch of if statements, copy and pasting is easier for me.
    #include <iostream.h>

// Exercise 1 of Chapter 3 - Teach Yourself C++


int age;

int main()
{
	cout << "Please enter your age.";
	cin >> age;

	switch (age)
	{
	case 1:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;	
	
	case 2:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;	
	
	case 3:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;	
	
	case 4:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;	
	
	case 5:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;	
	
	case 6:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;	
	
	case 7:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;	
	
	case 8:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;	
	
	case 9:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;	
	case 10:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;
		
	case 11:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;

	case 12:
		cout << "\nYou are still a child.";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purcahse alcohol or cigarretes.";
		break;

	case 13:
		cout << "\nYou are teenager, but...";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purchase alcohol or cigarretes.";
		break;	
	
	case 14:
		cout << "\nYou are teenager, but...";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purchase alcohol or cigarretes.";
		break;	
	
	case 15:
		cout << "\nYou are teenager, but...";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purchase alcohol or cigarretes.";
		break;	
	
	case 16:
		cout << "\nYou are teenager, but...";
		cout << "\nYou cannot legally drive.";
		cout << "\nYou cannot legally purchase alcohol or cigarretes.";
		break;

	case 17:
		cout << "\nYou are a teenager, but...";
		cout << "\nYou cannot legally purchase alcohol or cigarretes.";
		cout << "\nYou can drive legally.";
		break;

	case 18:
		cout << "\nYou are a teenager and a legal adult";
		cout << "\nYou can purchase cigarretes legally.";
		cout << "\nYou cannot legally purchase alcohol.";
		cout << "\nYou can drive legally.";
		break;	
	
	case 19:
		cout << "\nYou are a teenager and a legal adult";
		cout << "\nYou can purchase cigarretes legally.";
		cout << "\nYou cannot legally purchase alcohol.";
		cout << "\nYou can drive legally.";
		break;

	case 20:
		cout << "\nYou are an adult.";
		cout << "\nYou can purchase cigarretes legally.";
		cout << "\nYou cannot legally purchase alcohol.";
		cout << "\nYou can drive legally.";
		break;

	default:
			if ((age >= 21) && (age <= 54))
	{
		cout << "\nYou are a middle-aged adult.";
		cout << "\nYou can purcahse cigarretes and alcohol legally.";
		cout << "\nYou can drive legally.";
	}
			else
				if (age >= 55)
				{
					cout << "\nYou are a middle-aged adult.";
					cout << "\nYou can purcahse cigarretes and alcohol legally.";
					cout << "\nYou can drive legally.";
				}
	break;
	}


	cout << "\n\nThank for using Age Definer v1.0 by Bleu Shift.\n";
	return 0;
}
    
Bleu Shift - www.bleushift.tk [edited by - PSWind on July 27, 2002 9:59:24 PM]

Share this post


Link to post
Share on other sites
Advertisement
The text for age >= 55 is the same as the text for age >=21 && age <= 54. Both texts have mispelled "purchase" too.

Share this post


Link to post
Share on other sites
That makes sense, so my code wasn''t wrong just I copied and pasted without reviewed what I pasted. THanks for the help.

Share this post


Link to post
Share on other sites
Of course the true test will be to make the changes and see what happens. I''m also under the impression that with cout it''s better to end the expression with " << endl " then it is to insert a newline character directly into the string, but I don''t think that would be the cause of the problem.

Share this post


Link to post
Share on other sites

  • 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!