C++ try/catch

I am trying to use a try/catch am I am not sure I have done it correctly.
int main()
{
	ifstream inData;
	ofstream outData;
	int CourtNum, choice, gameChoice, sizeChoice, memberChoice;
	bool isMember, game, stillAvail;
	string gametype;
int rc = 0;
	bool reserved = false;
	Courts r[11];
	reservation res[100];
	inData.open("reservation.txt");
    outData.open("reservation.out");
	string str = "Please use choice 1, 2, or 3.";
 
	do{
	try
	{
 
	cout << endl << endl << endl << setw(55)<< "Welcome to the Sports Recreational Center" << endl << endl << endl <<endl;
	cout << setw(50)<< "Please choose an option" << endl << endl << endl << endl;
	cout << setw(50)<< "1 - Make a reservation" << endl << endl;
	cout << setw(50)<< "2 - Cancel reservation" << endl << endl;
    cout << setw(40)<< "3 - Finished" << endl << endl;
	cin >> choice;
	system("CLS");
	if (choice != 1 || choice != 2 || choice != 3)
		throw str;
 
	if (choice == 1){
 
		cout << endl << endl << setw(55) << "Do you prefer tennis or raquetball?" << endl << endl<<endl;
		cout << setw(40) << "1 - Tennis" << endl << endl;
	    cout << setw(44) << "2 - Raquetball" << endl << endl;
		cin >> gameChoice;
		system("CLS");
	catch (string messageStr)
	{
		cout << "messageStr << endl;
			cin.clear();
	}
return 0;
}

Open in new window

sendhelpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
Start with cleaning up the code ... You have several unmatched {}'s and ""'s.

Properly indenting your code will make things a LOT easier for you !!

        http://en.wikipedia.org/wiki/Indent_style


To know more about exceptions, check out this tutorial :

        http://www.cplusplus.com/doc/tutorial/exceptions.html
0
sendhelpAuthor Commented:
I have checked out that tutorial and as for the tags, I did not submit all of the code. The user has 3 choices. 1, 2, 3,
If they don't enter one of those numbers I want to catch it and throw the exception. If they enter one of those numbers other things happen. I am not sure how to do the try catch with the other code there. So I included if choice == 1 in this snippet.
What I don't know if I have right is it in the right place with the rest of the code? And should I use this instead?
catch (exception& e)
  {
    cout << "Standard exception: " << e.what() << endl;
  }
0
Infinity08Commented:
>> I did not submit all of the code.

Maybe you should then ;)


>> If they don't enter one of those numbers I want to catch it and throw the exception.

What's the point of throwing an exception in main if you already have the information there ?


>> What I don't know if I have right is it in the right place with the rest of the code?

As I said, you'll have to fix the unmatched {}'s and ""'s ... Otherwise I can only guess what you're trying to do. Maybe post the whole code ?


>> And should I use this instead?
>> catch (exception& e)
>>   {
>>     cout << "Standard exception: " << e.what() << endl;
>>   }

That will only catch exceptions of type exception. In the catch statement, you specify which exceptions you want to catch.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

sendhelpAuthor Commented:
I am working on this with someone else, I am just supposed to put a try/catch statement in here. Only part of the code works right now. I am not to change anything, just add the try/catch. The user is supposed to enter 1,2, or 3. If they enter a different number or character I am suppose to use the exception. I changed it some. Is this right?
int main()
{
	ifstream inData;
	ofstream outData;
	int CourtNum, choice, gameChoice, sizeChoice, memberChoice;
	bool isMember, game, stillAvail;
	string gametype;
int rc = 0;
	bool reserved = false;
	Courts r[11];
	reservation res[100];
	inData.open("reservation.txt");
    outData.open("reservation.out");
	string str = "Please use choice 1, 2, or 3.";
 
	do{
	try
	{
 
	cout << endl << endl << endl << setw(55)<< "Welcome to the Sports Recreational Center" << endl << endl << endl <<endl;
	cout << setw(50)<< "Please choose an option" << endl << endl << endl << endl;
	cout << setw(50)<< "1 - Make a reservation" << endl << endl;
	cout << setw(50)<< "2 - Cancel reservation" << endl << endl;
    cout << setw(40)<< "3 - Finished" << endl << endl;
	cin >> choice;
	system("CLS");
	if (!cin)
		throw str;
	catch (string messageStr)
	{
		cout << "messageStr << endl;
			cin.clear();
	}
 
	if (choice == 1){
 
		cout << endl << endl << setw(55) << "Do you prefer tennis or raquetball?" << endl << endl<<endl;
		cout << setw(40) << "1 - Tennis" << endl << endl;
	    cout << setw(44) << "2 - Raquetball" << endl << endl;
		cin >> gameChoice;
		system("CLS");
	catch (string messageStr)
	{
		cout << "messageStr << endl;
			cin.clear();
	}
 
        cout << endl << endl << setw(55) << "Do you prefer a singles or doubles?" << endl << endl<<endl;
		cout << setw(40) << "1 - Doubles" << endl << endl;
	    cout << setw(40) << "2 - Singles" << endl << endl;
		cin >> sizeChoice;
		system("CLS");
 
        cout << endl << endl << setw(55) << "Are you a member or guest?" << endl << endl<<endl;
		cout << setw(40) << "1 - Member" << endl << endl;
	    cout << setw(42) << "2 - Guest" << endl << endl;
		cin >> memberChoice;
		system("CLS");
 
		inData >> CourtNum; >> isMember >> game >> gametype >> stillAvail;
	
	r[1].setCourtInfo("doubles", false, true, 1);
	r[2].setCourtInfo("singles", false, false, 2);
	r[3].setCourtInfo("doubles", true, true, 3);
	r[4].setCourtInfo("doubles", true, false, 4);
	r[5].setCourtInfo("singles", true, true, 5);
	r[6].setCourtInfo("doubles", false, true, 6);
    r[7].setCourtInfo("singles", false, true, 7);
	r[8].setCourtInfo("doubles", false, true, 8);
	r[9].setCourtInfo("singles", true, false, 9);
	r[10].setCourtInfo("doubles", false, true, 10);
	bool maderes = r[2].reserveCourt(3);
	if(maderes)
		cout << "You have successfully made a reservation" << endl; 
	else
		cout << "The Court you are requesting is not available" << endl;
	
 
	cout << r[2].reserveCourt(3) << endl;
	cout << r[2].reserveCourt(3, 5) << endl;
	r[2].cancelRes(3);
	cout << r[2].reserveCourt(3, 5) << endl;
 
	while(rc < 10 && !reserved)
	{
		rc++;
		reserved = res[rc].makeReservation(r[rc], "1234", 3, 5);
	}
    	cout << rc;
	cout <<  res[1].makeReservation(r[1], "123", 3, 5) << endl;
	cout <<  res[0].makeReservation(r[7], "123", 3, 5);
 
	
 
		cout << "Confirmation: You will be in Court number " << CourtNum << endl << endl;
 
 
	 
     outData << setw(5)<< CourtNum 
			<< setw(5)<< isMember
			<< setw(5)<< game
			<< setw(5)<< gametype
                << setw(5)<< stillAvail
                << startDate
                << endDate
                << personName
                << address
                << city
                << state
                << cardType
                << cardNum
                << expDate
				<<endl;
	}  if
	
 
    } 
	do while (choice != 3);
 
 inData.close();
 outData.close();
 
	return 0;
}

Open in new window

0
sendhelpAuthor Commented:
Okay, here is what I have. If I enter a letter instead of 1,2, or 3. The string str appears at the top of the screen, but it flickers. And how do I get it to catch if I enter an integer other than 1, 2, or 3.
int main()
{
	ifstream inData;
	ofstream outData;
	int CourtNum, choice, gameChoice, sizeChoice, memberChoice;
	bool isMember, game, stillAvail;
	string gametype;
int rc = 0;
	bool reserved = false;
	Courts r[11];
	reservation res[100];
	inData.open("reservation.txt");
    outData.open("reservation.out");
	string str = "Please use choice 1, 2, or 3.";
 
	do{
	try
	{
 
	cout << endl << endl << endl << setw(55)<< "Welcome to the Sports Recreational Center" << endl << endl << endl <<endl;
	cout << setw(50)<< "Please choose an option" << endl << endl << endl << endl;
	cout << setw(50)<< "1 - Make a reservation" << endl << endl;
	cout << setw(50)<< "2 - Cancel reservation" << endl << endl;
    cout << setw(40)<< "3 - Finished" << endl << endl;
	cin >> choice;
	system("CLS");
	if (!cin)
		throw str;
	catch (string messageStr)
	{
		cout << "messageStr << endl;
			cin.clear();
	}
 
	if (choice == 1){
 
		cout << endl << endl << setw(55) << "Do you prefer tennis or raquetball?" << endl << endl<<endl;
		cout << setw(40) << "1 - Tennis" << endl << endl;
	    cout << setw(44) << "2 - Raquetball" << endl << endl;
		cin >> gameChoice;
		system("CLS");
	catch (string messageStr)
	{
		cout << "messageStr << endl;
			cin.clear();
	}
 
        cout << endl << endl << setw(55) << "Do you prefer a singles or doubles?" << endl << endl<<endl;
		cout << setw(40) << "1 - Doubles" << endl << endl;
	    cout << setw(40) << "2 - Singles" << endl << endl;
		cin >> sizeChoice;
		system("CLS");
 
        cout << endl << endl << setw(55) << "Are you a member or guest?" << endl << endl<<endl;
		cout << setw(40) << "1 - Member" << endl << endl;
	    cout << setw(42) << "2 - Guest" << endl << endl;
		cin >> memberChoice;
		system("CLS");
 
		inData >> CourtNum; //>> isMember >> game >> gametype >> stillAvail;
	
	/*r[1].setCourtInfo("doubles", false, true, 1);
	r[2].setCourtInfo("singles", false, false, 2);
	r[3].setCourtInfo("doubles", true, true, 3);
	r[4].setCourtInfo("doubles", true, false, 4);
	r[5].setCourtInfo("singles", true, true, 5);
	r[6].setCourtInfo("doubles", false, true, 6);
    r[7].setCourtInfo("singles", false, true, 7);
	r[8].setCourtInfo("doubles", false, true, 8);
	r[9].setCourtInfo("singles", true, false, 9);
	r[10].setCourtInfo("doubles", false, true, 10);
	bool maderes = r[2].reserveCourt(3);
	if(maderes)
		cout << "You have successfully made a reservation" << endl; 
	else
		cout << "The Court you are requesting is not available" << endl;
	
 
	cout << r[2].reserveCourt(3) << endl;
	cout << r[2].reserveCourt(3, 5) << endl;
	r[2].cancelRes(3);
	cout << r[2].reserveCourt(3, 5) << endl;
 
	while(rc < 10 && !reserved)
	{
		rc++;
		reserved = res[rc].makeReservation(r[rc], "1234", 3, 5);
	}
    	cout << rc;
	cout <<  res[1].makeReservation(r[1], "123", 3, 5) << endl;
	cout <<  res[0].makeReservation(r[7], "123", 3, 5);*/
 
	
 
		cout << "Confirmation: You will be in Court number " << CourtNum << endl << endl;
 
 
	 
     outData << setw(5)<< CourtNum 
			/*<< setw(5)<< isMember
			<< setw(5)<< game
			<< setw(5)<< gametype
                << setw(5)<< stillAvail
                << startDate
                << endDate
                << personName
                << address
                << city
                << state
                << cardType
                << cardNum
                << expDate*/
				<<endl;
	}  if
	
 
    } 
	do while (choice != 3);
 
 inData.close();
 outData.close();
 
	return 0;
}

Open in new window

0
jkrCommented:
Your 'catch' statements in the above are useless, since they are in the same scope as try, i.e.

try
{
  catch(...)
  {
  }
}

That has to be

try
{

}
catch(...)
{

}

instead.
0
sendhelpAuthor Commented:
That did not work.
I changed it. It works okay with an integer out of range, but when I put in a letter, the screen flickers and does not allow input. Any suggestions. I have read many tutorials on try/catch and don't really get the catch part. Is there something else that I should have in the catch part?
int main()
{
	ifstream inData;
	ofstream outData;
	int CourtNum, choice, gameChoice, sizeChoice, memberChoice;
	bool isMember, game, stillAvail;
	string gametype;
int rc = 0;
	bool reserved = false;
	Courts r[11];
	reservation res[100];
	inData.open("reservation.txt");
    outData.open("reservation.out");
	string str = "Please use choice 1, 2, or 3.";
 
	do{
	try
	{
 
	cout << endl << endl << endl << setw(55)<< "Welcome to the Sports Recreational Center" << endl << endl << endl <<endl;
	cout << setw(50)<< "Please choose an option" << endl << endl << endl << endl;
	cout << setw(50)<< "1 - Make a reservation" << endl << endl;
	cout << setw(50)<< "2 - Cancel reservation" << endl << endl;
    cout << setw(40)<< "3 - Finished" << endl << endl;
	cin >> choice;
	system("CLS");
	if (choice < 1 || choice > 3)
		throw str;
	else if (!cin)
		throw str;
	}
	catch (int x)
	{
		cout << x << endl;
			cin.clear();
	}
	catch (string s)
	{
		cout  << s << endl;
			cin.clear();
	}
 
	if (choice == 1){
 
		cout << endl << endl << setw(55) << "Do you prefer tennis or raquetball?" << endl << endl<<endl;
		cout << setw(40) << "1 - Tennis" << endl << endl;
	    cout << setw(44) << "2 - Raquetball" << endl << endl;
		cin >> gameChoice;
		system("CLS");
	catch (string messageStr)
	{
		cout << "messageStr << endl;
			cin.clear();
	}
 
        cout << endl << endl << setw(55) << "Do you prefer a singles or doubles?" << endl << endl<<endl;
		cout << setw(40) << "1 - Doubles" << endl << endl;
	    cout << setw(40) << "2 - Singles" << endl << endl;
		cin >> sizeChoice;
		system("CLS");
 
        cout << endl << endl << setw(55) << "Are you a member or guest?" << endl << endl<<endl;
		cout << setw(40) << "1 - Member" << endl << endl;
	    cout << setw(42) << "2 - Guest" << endl << endl;
		cin >> memberChoice;
		system("CLS");
 
		inData >> CourtNum; //>> isMember >> game >> gametype >> stillAvail;
	
	/*r[1].setCourtInfo("doubles", false, true, 1);
	r[2].setCourtInfo("singles", false, false, 2);
	r[3].setCourtInfo("doubles", true, true, 3);
	r[4].setCourtInfo("doubles", true, false, 4);
	r[5].setCourtInfo("singles", true, true, 5);
	r[6].setCourtInfo("doubles", false, true, 6);
    r[7].setCourtInfo("singles", false, true, 7);
	r[8].setCourtInfo("doubles", false, true, 8);
	r[9].setCourtInfo("singles", true, false, 9);
	r[10].setCourtInfo("doubles", false, true, 10);
	bool maderes = r[2].reserveCourt(3);
	if(maderes)
		cout << "You have successfully made a reservation" << endl; 
	else
		cout << "The Court you are requesting is not available" << endl;
	
 
	cout << r[2].reserveCourt(3) << endl;
	cout << r[2].reserveCourt(3, 5) << endl;
	r[2].cancelRes(3);
	cout << r[2].reserveCourt(3, 5) << endl;
 
	while(rc < 10 && !reserved)
	{
		rc++;
		reserved = res[rc].makeReservation(r[rc], "1234", 3, 5);
	}
    	cout << rc;
	cout <<  res[1].makeReservation(r[1], "123", 3, 5) << endl;
	cout <<  res[0].makeReservation(r[7], "123", 3, 5);*/
 
	
 
		cout << "Confirmation: You will be in Court number " << CourtNum << endl << endl;
 
 
	 
     outData << setw(5)<< CourtNum 
			/*<< setw(5)<< isMember
			<< setw(5)<< game
			<< setw(5)<< gametype
                << setw(5)<< stillAvail
                << startDate
                << endDate
                << personName
                << address
                << city
                << state
                << cardType
                << cardNum
                << expDate*/
				<<endl;
	}  if
	
 
    } 
	do while (choice != 3);
 
 inData.close();
 outData.close();
 
	return 0;
}

Open in new window

0
jkrCommented:
There are till some things missing, i.e.
int main()
{
	ifstream inData;
	ofstream outData;
	int CourtNum, choice, gameChoice, sizeChoice, memberChoice;
	bool isMember, game, stillAvail;
	string gametype;
int rc = 0;
	bool reserved = false;
	Courts r[11];
	reservation res[100];
	inData.open("reservation.txt");
    outData.open("reservation.out");
	string str = "Please use choice 1, 2, or 3.";
 
	do{
	try // <--- let's call this t1
	{
 
	cout << endl << endl << endl << setw(55)<< "Welcome to the Sports Recreational Center" << endl << endl << endl <<endl;
	cout << setw(50)<< "Please choose an option" << endl << endl << endl << endl;
	cout << setw(50)<< "1 - Make a reservation" << endl << endl;
	cout << setw(50)<< "2 - Cancel reservation" << endl << endl;
    cout << setw(40)<< "3 - Finished" << endl << endl;
	cin >> choice;
	system("CLS");
	if (choice < 1 || choice > 3)
		throw str;
	else if (!cin)
		throw str;
	}
	catch (int x) // OK, matches t1
	{
		cout << x << endl;
			cin.clear();
	}
	catch (string s) // OK, matches t1
	{
		cout  << s << endl;
			cin.clear();
	}
 
	if (choice == 1){
 
		cout << endl << endl << setw(55) << "Do you prefer tennis or raquetball?" << endl << endl<<endl;
		cout << setw(40) << "1 - Tennis" << endl << endl;
	    cout << setw(44) << "2 - Raquetball" << endl << endl;
		cin >> gameChoice;
		system("CLS");
	catch (string messageStr) // <--- no 'try', what do you want to catch?
	{
		cout << "messageStr << endl;
			cin.clear();
	}
 
        cout << endl << endl << setw(55) << "Do you prefer a singles or doubles?" << endl << endl<<endl;
		cout << setw(40) << "1 - Doubles" << endl << endl;
	    cout << setw(40) << "2 - Singles" << endl << endl;
		cin >> sizeChoice;
		system("CLS");
 
        cout << endl << endl << setw(55) << "Are you a member or guest?" << endl << endl<<endl;
		cout << setw(40) << "1 - Member" << endl << endl;
	    cout << setw(42) << "2 - Guest" << endl << endl;
		cin >> memberChoice;
		system("CLS");
 
		inData >> CourtNum; //>> isMember >> game >> gametype >> stillAvail;
	
	/*r[1].setCourtInfo("doubles", false, true, 1);
	r[2].setCourtInfo("singles", false, false, 2);
	r[3].setCourtInfo("doubles", true, true, 3);
	r[4].setCourtInfo("doubles", true, false, 4);
	r[5].setCourtInfo("singles", true, true, 5);
	r[6].setCourtInfo("doubles", false, true, 6);
    r[7].setCourtInfo("singles", false, true, 7);
	r[8].setCourtInfo("doubles", false, true, 8);
	r[9].setCourtInfo("singles", true, false, 9);
	r[10].setCourtInfo("doubles", false, true, 10);
	bool maderes = r[2].reserveCourt(3);
	if(maderes)
		cout << "You have successfully made a reservation" << endl; 
	else
		cout << "The Court you are requesting is not available" << endl;
	
 
	cout << r[2].reserveCourt(3) << endl;
	cout << r[2].reserveCourt(3, 5) << endl;
	r[2].cancelRes(3);
	cout << r[2].reserveCourt(3, 5) << endl;
 
	while(rc < 10 && !reserved)
	{
		rc++;
		reserved = res[rc].makeReservation(r[rc], "1234", 3, 5);
	}
    	cout << rc;
	cout <<  res[1].makeReservation(r[1], "123", 3, 5) << endl;
	cout <<  res[0].makeReservation(r[7], "123", 3, 5);*/
 
	
 
		cout << "Confirmation: You will be in Court number " << CourtNum << endl << endl;
 
 
	 
     outData << setw(5)<< CourtNum 
			/*<< setw(5)<< isMember
			<< setw(5)<< game
			<< setw(5)<< gametype
                << setw(5)<< stillAvail
                << startDate
                << endDate
                << personName
                << address
                << city
                << state
                << cardType
                << cardNum
                << expDate*/
				<<endl;
	}  if
	
 
    } 
	do while (choice != 3);
 
 inData.close();
 outData.close();
 
	return 0;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
As I said earlier : you really need to properly indent your code. It's impossible to read this way !! And it's no wonder that parts don't match up. Properly indenting your code will not only make the code easier to read and edit, but will because of that also minimize bugs.


Now ... you still haven't lined up the catch blocks with the try blocks. It really NEEDS to be like this :

        try {
            // some code that can throw exceptions
        }
        catch(...) {
            // code to handle the exception
        }

There can be more than one catch block (each catching a different kind of exception), but they all have to come immediately after the try block enclosed by {}'s.
0
Infinity08Commented:
Btw, what's this supposed to do :

      do while (choice != 3);

?

Almost all of the problems you have can be resolved by properly indenting your code ... I can't repeat it enough. Please, believe me ... It'll make your life so much easier.
0
sendhelpAuthor Commented:
Okay, how is the formatting now?
>>>Btw, what's this supposed to do :

>>>      do while (choice != 3);

>>>?
I don't know, my partner is working on that. I just have to add try/catch.
I took out the extra catch, I thought I deleted earlier, but missed it. It still does the same thing. Integers out of range, I get the string to choose the correct option and I can still choose an option. When I enter a letter, is shows the string, but the screen flickers and I can't input another option.
Is there a better catch to use?


int main()
{
	ifstream inData;
	ofstream outData;
	int CourtNum, choice, gameChoice, sizeChoice, memberChoice;
	bool isMember, game, stillAvail;
	string gametype;
	int rc = 0;
	bool reserved = false;
	Courts r[11];
	reservation res[100];
	inData.open("reservation.txt");
	outData.open("reservation.out");
	string str = "Please use choice 1, 2, or 3.";
 
	do{
		try  
		{
 
			cout << endl << endl << endl << setw(55)<< "Welcome to the Sports Recreational Center" << endl << endl << endl <<endl;
			cout << setw(50)<< "Please choose an option" << endl << endl << endl << endl;
			cout << setw(50)<< "1 - Make a reservation" << endl << endl;
			cout << setw(50)<< "2 - Cancel reservation" << endl << endl;
			cout << setw(40)<< "3 - Finished" << endl << endl;
			cin >> choice;
			system("CLS");
			if (choice < 1 || choice > 3)
				throw str;
			else if (!cin)
				throw str;
		}
		catch (int x)  
		{
			cout << x << endl;
			cin.clear();
		}
		catch (string s)  
		{
			cout  << s << endl;
			cin.clear();
		}
 
		if (choice == 1){
 
			cout << endl << endl << setw(55) << "Do you prefer tennis or raquetball?" << endl;
			cout << setw(40) << "1 - Tennis" << endl << endl;
			cout << setw(44) << "2 - Raquetball" << endl << endl;
			cin >> gameChoice;
			system("CLS");
 
			cout << setw(55) << "Do you prefer a singles or doubles?" << endl;
			cout << setw(40) << "1 - Doubles" << endl << endl;
			cout << setw(40) << "2 - Singles" << endl << endl;
			cin >> sizeChoice;
			system("CLS");
 
			cout << endl << endl << setw(55) << "Are you a member or guest?" << endl;
			cout << setw(40) << "1 - Member" << endl << endl;
			cout << setw(42) << "2 - Guest" << endl << endl;
			cin >> memberChoice;
			system("CLS");
 
			inData >> CourtNum; //>> isMember >> game >> gametype >> stillAvail;
 
			/*r[1].setCourtInfo("doubles", false, true, 1);
			r[2].setCourtInfo("singles", false, false, 2);
			r[3].setCourtInfo("doubles", true, true, 3);
			r[4].setCourtInfo("doubles", true, false, 4);
			r[5].setCourtInfo("singles", true, true, 5);
			r[6].setCourtInfo("doubles", false, true, 6);
			r[7].setCourtInfo("singles", false, true, 7);
			r[8].setCourtInfo("doubles", false, true, 8);
			r[9].setCourtInfo("singles", true, false, 9);
			r[10].setCourtInfo("doubles", false, true, 10);
			bool maderes = r[2].reserveCourt(3);
			if(maderes)
			cout << "You have successfully made a reservation" << endl; 
			else
			cout << "The Court you are requesting is not available" << endl;
 
 
			cout << r[2].reserveCourt(3) << endl;
			cout << r[2].reserveCourt(3, 5) << endl;
			r[2].cancelRes(3);
			cout << r[2].reserveCourt(3, 5) << endl;
 
			while(rc < 10 && !reserved)
			{
			rc++;
			reserved = res[rc].makeReservation(r[rc], "1234", 3, 5);
			}
			cout << rc;
			cout <<  res[1].makeReservation(r[1], "123", 3, 5) << endl;
			cout <<  res[0].makeReservation(r[7], "123", 3, 5);*/
 
 
 
			cout << "Confirmation: You will be in Court number " << CourtNum << endl << endl;
 
 
 
			outData << setw(5)<< CourtNum 
				/*<< setw(5)<< isMember
				<< setw(5)<< game
				<< setw(5)<< gametype
 
				<< setw(5)<< stillAvail
				<< startDate
				<< endDate
				<< personName
				<< address
				<< city
				<< state
				<< cardType
				<< cardNum
				<< expDate*/
				<<endl;
		}  if
 
 
	} 
	do while (choice != 3);
 
	inData.close();
	outData.close();
 
	return 0;
}

Open in new window

0
jkrCommented:
Looks good, all the 'catch()' blocks have their matching 'try()' now and the scope is OK.
0
sendhelpAuthor Commented:
Do you know why the screen flickers and won't let me input anything when I input a letter. It is fine if I put an out of range integer.
0
jkrCommented:
Your 'cin' probably still chokes on the letter in the buffer. Try using
		catch (string s)  
		{
			cout  << s << endl;
                        cin.ignore();
			cin.clear();
		}

Open in new window

0
sendhelpAuthor Commented:
>>Your 'cin' probably still chokes on the letter in the buffer. Try using1:
 >>            catch (string s)  
>>            {
>>                  cout  << s << endl;
>>                       cin.ignore();
>>                  cin.clear();
>>            }

This did nothing, I was so hopeful. I can't find any infornation on what to do. Any other suggestions.

 
0
Infinity08Commented:
>> When I enter a letter, is shows the string, but the screen flickers and I can't input another option.

the data that the user gave as input is still on the stream ... You need to get it off before continuing ... The cin.ignore() that jkr showed will only discard one character though ... You probably want to remove the whole line using getline for example.


The indentation is a lot better now ;) Makes it so much easier to read, doesn't it ?

There are still a few problems with the code though, like a lone if, an extra do, etc.
0
sendhelpAuthor Commented:
Do you think I can use a bool finished = false;
maybe somehow put in a while(!finished)
finished = true; in the else if. I am not sure how to work that.
0
Infinity08Commented:
Why ? Do you want to continue or end the application when the user gives bad input ?
0
Infinity08Commented:
Got to go now ... I'll be back in a few hours, but for now I leave you in the capable hands of jkr ;)
0
sendhelpAuthor Commented:
>>The indentation is a lot better now ;) Makes it so much easier to read, doesn't it ?

>>There are still a few problems with the code though, like a lone if, an extra do, etc.

Yes, the code is easier to read. And the lone if and extra do are gone.

>> Why ? Do you want to continue or end the application when the user gives bad input ?
I want it to force them into only giving good input, if they give bad input I want it to clear tell them to enter good input and let them try again.

0
Infinity08Commented:
>> I want it to force them into only giving good input, if they give bad input I want it to clear tell them to enter good input and let them try again.

And do you really need to use exceptions for this ? It's a bit overkill.

Anyway, the idea would be to throw an exception if the user gives bad input, and then in the catch block for that exception, you print a message saying that invalid input was given.

You have to make sure though that the rest of the loop is NOT executed if the exception occurs. So, either you enclose the whole loop body in a try block, or you add a continue to the catch block.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.