if/else statment

if/else staement for input validation

Page 1 of 1

5 Replies - 1172 Views - Last Post: 10 May 2009 - 05:08 PM Rate Topic: -----

#1 Maverick NOS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 10-May 09

if/else statment

Posted 10 May 2009 - 03:17 PM

I'm having trouble getting the correct sequence of if/else statements to validate user input.

cout << "Purchase a Ticket\n\n";

				do 
				{
					cout << "Please select the row you would like to sit in: ";
					cin >> row2;


					cout << "Please select the seat you would like to sit in: ";
					cin >> column2;
					row2--;
					column2--;


					if (map [row2] [column2] == '*')
						{
							cout << endl;
							cout << "Sorry that seat is sold-out, Please select a new seat.";
							cout << endl << endl;
						}

					else 
					{
						cost = price [row2] + 0;
						total = total + cost;
						cout << endl;
						cout << "That ticket costs: $" << cost << endl;
						cout << "Confirm Purchase? Enter (1 = YES / 2 = NO): ";
						cin >> answer;
						seat = seat - answer;
						seat2 += answer;
						cout << endl;
						
						if (answer == 1)
						{ 
							cout << "Your ticket purchase has been confirmed." << endl << endl;
							map [row2][column2] = FULL;
						}
						else if (answer == 2)
						{
							cout << endl;
							cout << "Would you like to look at another seat? (1 = YES / 2 = NO)";
							cout << endl << endl;
							cin >> Quit;
						}
						
						cout << endl;
						cout << "Would you like to look at another seat?(1 = YES / 2 = NO)";
						cin >> Quit;
					}

				}
				while (Quit == 1);
					
				break;




I need it not to accept rows under 1 or over 5 and
seats under 1 or over 10

if (row2 < 1 || row2 >5)
	cout << "Please choose a row between (1-5) << endl;



I'm kinda lost of how to finish it of where to put it...

Is This A Good Question/Topic? 0
  • +

Replies To: if/else statment

#2 Maverick NOS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 10-May 09

Re: if/else statment

Posted 10 May 2009 - 03:38 PM

I don't know if will make it easier or harder but here is the complete code so far.
I don't know if it would help so you compile it. It compiles for me but when I try to mess around with the if/else statements it crashes when I run it.

//Ken Klotz
//CISM135-E31
//May 2009
//Final Project -- Theater Seating
//A program that asks the user for input to buy theater seats.

#include <iostream>
#include <iomanip>
using namespace std;

//Function Declarations
int Show_Menu ();				 //To show main menu
void Show_Chart ();			   //To show seating chart
void Show_Info ();				  //To show Theater info


const char FULL = '*';			//Seat taken
const char EMPTY = '#';		   //Seat open
const int rows = 5;			   //Number of rows
const int columns = 10;		   //Number of seats per row
char map [rows][columns];		 //Array to hold seating chart
double price;
int total = 0;
int seat = 50;
int seat2 = 0;
int Quit = 1;



int main ()
{

const int Num_Rows = 5;
int price [Num_Rows];
int row2, column2, cost;
int answer;


//Main Logo

	cout << "\t*********************************************************" << endl;
	cout << "\t*													   *" << endl;
	cout << "\t*	Welcome to The Ken Klotz Performing Arts Center	*" << endl;
	cout << "\t*													   *" << endl;
	cout << "\t*********************************************************" << endl;

	cout << endl << endl;


//Sets the row prices.

	for (int count = 0; count < rows; count++)
		{
			cout << "Please enter the price for row " << (count + 1) << ": $";
				cin >> price [count];
			   
		}

	for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
				map [i][j] = EMPTY;
		}


int choice;
	do
	{
		choice = Show_Menu();	  // Shows the main menu function.

		switch (choice)
		{
			case 1:
				cout << "View Seat Prices\n\n";
				
				for (int count = 0; count < rows; count++)
				{
					cout << "The price for row " << (count + 1) << ": $";
					cout << price [count] << endl;
				}

				break;

			case 2:
				cout << "Purchase a Ticket\n\n";

				do 
				{
					cout << "Please select the row you would like to sit in: ";
					cin >> row2;


					cout << "Please select the seat you would like to sit in: ";
					cin >> column2;
					row2--;
					column2--;


					if (map [row2] [column2] == '*')
						{
							cout << endl;
							cout << "Sorry that seat is sold-out, Please select a new seat.";
							cout << endl << endl;
						}

					else 
					{
						cost = price [row2] + 0;
						total = total + cost;
						cout << endl;
						cout << "That ticket costs: $" << cost << endl;
						cout << "Confirm Purchase? Enter (1 = YES / 2 = NO): ";
						cin >> answer;
						seat = seat - answer;
						seat2 += answer;
						cout << endl;
						
						if (answer == 1)
						{ 
							cout << "Your ticket purchase has been confirmed." << endl << endl;
							map [row2][column2] = FULL;
						}
						else if (answer == 2)
						{
							cout << endl;
							cout << "Would you like to look at another seat? (1 = YES / 2 = NO)";
							cout << endl << endl;
							cin >> Quit;
						}
						
						cout << endl;
						cout << "Would you like to look at another seat?(1 = YES / 2 = NO)";
						cin >> Quit;
					}

				}
				while (Quit == 1);
					
				break;

				//To view seating chart.
			case 3:
				cout << "View Available Seats\n\n";
				Show_Chart ();
				break;

			case 4:
				cout << "View View Theater Information\n\n";
				Show_Info ();
				break;


				//Quit Message
			case 5:   
				cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
				cout << "***************************************************************\n";
				cout << "**   Thank You for shopping with us. Please come back soon.  **\n";
				cout << "***************************************************************\n";
				cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
				break;

			default : cout << "Error input, Please select a number between (1-5)\n";
		}

	} while (choice != 5);







return 0;
}

//********************************************************************************
//********************************************************************************
//**																			**
//**							  Define Functions.							 **
//**																			**
//********************************************************************************
//********************************************************************************



// Show Menu Function...

int Show_Menu()
{
	int MenuChoice;
		cout << endl << endl;
		cout << " \tMAIN MENU\n";
		cout << " 1. View Seat Prices.\n";
		cout << " 2. Purchase a Ticket.\n";
		cout << " 3. View Available Seats.\n";
		cout << " 4. View Theater Information\n";
		cout << " 5. Quit the program.\n";
		cout << "_____________________\n\n";
		cout << "Please enter your choice: ";
		cin >> MenuChoice;
		cout << endl << endl;
	return MenuChoice;
}



//Show Seating Chart Function

void Show_Chart ()
{
	cout << "\tSeats" << endl;
	cout << "	  1 2 3 4 5 6 7 8 9 10\n";

		for (int count = 0; count < 5; count++)
		{
			cout << endl << "Row " << (count + 1);

			for (int count2 = 0; count2 < 10; count2++)
			{
				cout << " " <<  map [count] [count2];
			}
		}
			cout << endl;
}


// Show Theater Information
void Show_Info ()
{
	cout << "The total amount of ticket sales is: $" << total << endl << endl;
	cout << "There are " << seat << " seats remaining in the theater." << endl << endl;
	cout << "There is a total of " << seat2 << " seats sold at this time" << endl << endl;
	
}


Was This Post Helpful? 0
  • +
  • -

#3 fremgenc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 119
  • Joined: 15-November 07

Re: if/else statment

Posted 10 May 2009 - 03:54 PM

Check for validity at user input stage:

					

row2 = -1;
cout << "Please select the row you would like to sit in: ";

while(row2 == -1)
{
cin >> row2;

if (row2 < 1 || row2 >5)
{
	cout << "Please choose a row between (1-5)" << endl;
	row2 = -1;
}

}




column2 = -1;
cout << "Please select the seat you would like to sit in: ";

while(column2 == -1)
{
cin >> column2;

if (column2 < 1 || column2 >10)
{
	cout << "Please choose a seat between (1-10)" << endl;
	column2 = -1;
}

}


row2--;
column2--;


This post has been edited by fremgenc: 10 May 2009 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,631
  • Joined: 19-February 09

Re: if/else statment

Posted 10 May 2009 - 04:02 PM

You could break the Purchase a Ticket section into
a couple of functions make it easier to read.

Don't need else here.
		 else if (answer == 2)

Was This Post Helpful? 0
  • +
  • -

#5 Maverick NOS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 10-May 09

Re: if/else statment

Posted 10 May 2009 - 04:26 PM

How would I even start to move"CASE 2: Purchase Tickets" into a function. I just tried and was thrown about 50 errors. about pointers and the such. said some of my variables weren't defined. It does look extremely messy and I would like to clean it up a bit. How could i move it down into my function list.
Anyone?

This post has been edited by Maverick NOS: 10 May 2009 - 04:30 PM

Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,631
  • Joined: 19-February 09

Re: if/else statment

Posted 10 May 2009 - 05:08 PM

View PostMaverick NOS, on 11 May, 2009 - 12:26 AM, said:

How would I even start to move"CASE 2: Purchase Tickets" into a function.


Don't move case 2: that is part of the switch statement and needs to stay with it.

Moving code was only a suggestion. I had seen your other functions and thought you would be able to do it.

Asking for another seat is repeated.

  cout << endl;
  cout << "Would you like to look at another seat?(1 = YES / 2 = NO)";
  cin >> Quit;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1