Hopefully, someone out there can help me. I'm definitely a newbie when it comes to programming and C++. I'm attempting to do a validation for the user's input for rowChosen (0 to 14) and colChosen (o to 19). When I input a value of 15 for rowChosen, it let me pass. Same for colChosen. When I enter 20, it allowed me to pass. I tried setting my int rowChosen = -1 in the header, but it then just default to the first condition which is, ""Please enter a row number between 0 and 14" but it always allows me to pass. What am I doing wrong?
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main()
{
char selection;
int rowChosen = 0;
int colChosen = 0;
int seatNum = 0;
float totalCost = 0;
//theater menu
do {
cout << "MOVIE THEATER MENU" << endl;
cout << "------------------" << endl;
cout << "1) Sell a ticket" << endl;
cout << "Q) Quit program" << endl;
cout << "Please make a selection" << endl;
cin >> selection;
if (selection == '1')
{
cout << "Please enter a row number and seat number for the ticket:\n";
cout << "Row # : ";
if (rowChosen < 0 || rowChosen > 14)
{
cout << "Please enter a row number between 0 and 14\n";
}
else
{
cin >> rowChosen;
cin.ignore(15, '\n');
}
cout << "Seat # : ";
if (colChosen < 0 || colChosen >19)
{
cout << "Please enter a column number between 0 and 19\n";
}
else
{
cin >> colChosen;
seatNum++; // count the seat that have been sold
}
cout << endl;
//if seat is already taken
if (map[rowChosen][colChosen] == '#')
{
cout << "Sorry. The ticket is not available for this seat.\n";
continue; // start the loop again
}
else // selling the ticket
map[rowChosen][colChosen] = '#';
totalCost += rowCost[rowChosen];
}
else if (selection == 'q' || selection == 'Q')
{
cout << "UPDATED SEATING CHART AND SALES INFO \n";
cout << "------------------------------------\n";
theaterSeats(map);
cout << "TOTAL TICKETS SOLD: " << seatNum << endl;
cout << fixed << setprecision(2) << "TOTAL REVENUE: $" << totalCost << endl;
}
else if (selection != 'Q' && selection != 'q')
{
cout << "Invalid selection. Please try again" << endl;
}
} while (selection != 'Q' && selection != 'q');
cout << endl;
cout << "You have chosen to quit the program. Thank you for using!" << endl;
system("PAUSE");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire