Advertisement

Case and If statements

Started by July 27, 2002 08:57 PM
2 comments, last by Noky 22 years, 3 months ago
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]
The text for age >= 55 is the same as the text for age >=21 && age <= 54. Both texts have mispelled "purchase" too.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Advertisement
That makes sense, so my code wasn''t wrong just I copied and pasted without reviewed what I pasted. THanks for the help.
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement