if/else statment

Page 1 of 1

5 Replies - 1429 Views - Last Post: 10 May 2009 - 05:08 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=104411&amp;s=ec69e0d2dc7a745c2801113bef5a86e8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Maverick NOS

Reputation: 0
• 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): ";
cout << endl;

{
cout << "Your ticket purchase has been confirmed." << endl << endl;
map [row2][column2] = FULL;
}
{
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

Reputation: 0
• 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
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;

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

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): ";
cout << endl;

{
cout << "Your ticket purchase has been confirmed." << endl << endl;
map [row2][column2] = FULL;
}
{
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.							 **
//**																			**
//********************************************************************************
//********************************************************************************

{
cout << endl << endl;
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 << endl << endl;
}

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

}

```

#3 fremgenc

Reputation: 3
• 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

#4 #define

• Duke of Err

Reputation: 1635
• Posts: 5,755
• 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)
```

#5 Maverick NOS

Reputation: 0
• 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

#6 #define

• Duke of Err

Reputation: 1635
• Posts: 5,755
• Joined: 19-February 09

Re: if/else statment

Posted 10 May 2009 - 05:08 PM

Maverick 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;
```