Archived

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

Noky

Case and If statements

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