sendhelp
asked on
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;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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;
}
ASKER
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;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
>>>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;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>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.
>> 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.
>> 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.
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.
ASKER
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.
maybe somehow put in a while(!finished)
finished = true; in the else if. I am not sure how to work that.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Got to go now ... I'll be back in a few hours, but for now I leave you in the capable hands of jkr ;)
ASKER
>>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.
>>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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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;
}