6 Replies - 562 Views - Last Post: 10 May 2009 - 07:26 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

Moving a statement out of switch into a function

Post icon  Posted 10 May 2009 - 05:09 PM

I have been having trouble all day with this program and have found alot of help on here. This program is done and works however I am not satisfied with the clutter.

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?

I have other functions in the program, but I can't get this one to work outside of main(), any ideas.

Here is the complete program as it is now.


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


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



	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
				{
					row2 = -1;
					cout << "Please select the row you would like to sit in: ";

					while(row2 == -1)
					{
						cin >> row2;
					
						if (row2 < 1 || row2 >5)
						{
							cout << endl;
							cout << "Please choose a row between (1-5)" << endl << endl;
							cout << "Please select the row you would like to sit in: ";
							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--;


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




Is This A Good Question/Topic? 0
  • +

Replies To: Moving a statement out of switch into a function

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4309
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Moving a statement out of switch into a function

Posted 10 May 2009 - 05:25 PM

Create some new functions for each of the loops, and look at what variables they use, those will be the variables you pass to the function. Then you can move each loop into the appropriate function.

For instance, you could move the first function to a loop...


void selectTheRow(int *row2) {
     cout << "Please select the row you would like to sit in: ";

     while(row2 == -1)
     {
          cin >> row2;
                   
          if (row2 < 1 || row2 >5)
          {
               cout << endl;
               cout << "Please choose a row between (1-5)" << endl << endl;
               cout << "Please select the row you would like to sit in: ";
               row2 = -1;
          }
     }
}



Then back in the do while loop you just need to call this new function and pass it row2.

do
{
     row2 = -1;
     selectTheRow(&row2);

     // Rest of statements




Notice that we took out a section of the do while loop that well pretty much self contained and placed it in a new function, then called that function from the place where it use to be. In the end your do while loop will contain a few call outs to other functions. Then perhaps that do while loop itself can then go in its own function which you call from case 2 of your switch.

For more tips on how to do this, look up "refactoring". Just make sure you don't fracture it up too much or else it will be hard to follow.

Enjoy!

"At DIC we be refactoring code ninjas... refactor one of us and you will get out coolness, leetness, and sometimes things go wrong you will get something that looks like sloth from the movie Goonies" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1305
  • View blog
  • Posts: 4,491
  • Joined: 19-February 09

Re: Moving a statement out of switch into a function

Posted 10 May 2009 - 05:30 PM

View PostMartyr2, on 11 May, 2009 - 01:25 AM, said:

"At DIC we be refactoring code ninjas... refactor one of us and you will get out coolness, leetness, and sometimes things go wrong you will get something that looks like sloth from the movie Goonies" :snap:


Arrr! That be true, me old shipmate. :pirate:
Was This Post Helpful? 0
  • +
  • -

#4 Maverick NOS  Icon User is offline

  • New D.I.C Head

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

Re: Moving a statement out of switch into a function

Posted 10 May 2009 - 05:38 PM

Now can I do this little by little then compile it or do I have to complete the whole move then try it. See the problem is we start looking at pointers in the next semester and at this point am kinda shotty on it. I'll give it shot and hopefully make it work.

thanks a lot.
ken
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1305
  • View blog
  • Posts: 4,491
  • Joined: 19-February 09

Re: Moving a statement out of switch into a function

Posted 10 May 2009 - 05:41 PM

Little by little.
Was This Post Helpful? 1
  • +
  • -

#6 Maverick NOS  Icon User is offline

  • New D.I.C Head

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

Re: Moving a statement out of switch into a function

Posted 10 May 2009 - 06:34 PM

row2 = -1;



is in both main () and in the function. with this mess with my results?



When I tried the move I got an error:

Error 2 error C2270: 'SelectTheRow' : modifiers not allowed on nonmember functions

This post has been edited by Maverick NOS: 10 May 2009 - 06:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 Maverick NOS  Icon User is offline

  • New D.I.C Head

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

Re: Moving a statement out of switch into a function

Posted 10 May 2009 - 07:26 PM

Thanks so much....
I looked into that Refactor and it great. I wish I knew about this in the beginning.'




//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 ();				 //Function Prototype to show main menu
void Show_Chart ();			   //Function Prototype to show seating chart
void Show_Info ();				//Function Prototype to show Theater info
int GetRow2 ();					  //Function Prototype for row assignment
int GetColumn2 ();				  //Function Prototype for column assignment
void Sold_Out ();				  //Function Prototype to validate ticket purchase
int GetCost_and_Confirm(int[], int, int, int);   //Cost of ticket and confirmation

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;					  //Total seats
int seat2 = 0;
int Quit = 1;
const int Num_Rows = 5;
int answer;
int choice;






int main ()
{


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



	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
				{
					row2 = GetRow2();					//Gets user input for Rows
					column2 = GetColumn2();				//Gets user input for Columns
					row2--;
					column2--;

					if (map [row2] [column2] == '*')  
						Sold_Out();						//Input validation for sold seat

					else
						cost = GetCost_and_Confirm(price, row2, column2, answer);
				}

				while (Quit == 1);
				   
				break;

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

			case 4:
				cout << "View View Theater Information\n\n";
				Show_Info ();							//Shows theater 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.							 **
//**																			**
//********************************************************************************
//********************************************************************************


// Shows the Main Menu.
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;
   
}


//**********************************************************************************************
//**********************************************************************************************
//**																						  **
//**			The following Functions are from case 2: of the switch statement			  **
//**						to help control the clutter and confusion.						  **
//**																						  **
//**********************************************************************************************
//**********************************************************************************************

// Gets the input for which row the customer would like to sit.
int GetRow2()
{
	int row2;
	row2 = -1;
	cout << "Please select the row you would like to sit in: ";

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

		if (row2 < 1 || row2 >5) //Input validation for rows
		{
			cout << endl;
			cout << "Please choose a row between (1-5)" << endl << endl;
			cout << "Please select the row you would like to sit in: ";
			row2 = -1;
		}
	}
	return row2;
}


//Gets the input for which seat the customer would to sit.
int GetColumn2()
{
	int column2;
	column2 = -1;
	cout << "Please select the seat you would like to sit in: ";

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

		if (column2 < 1 || column2 >10)  //Input validation for columns
		{
			cout << "Please choose a seat between (1-10)" << endl;
			column2 = -1;
		}
	}
	return column2;
}


//Validates user input so ticket can not be purchased twice.
void Sold_Out()
{
	cout << endl;
	cout << "Sorry that seat is sold-out, Please select a new seat.";
	cout << endl << endl;
}


//Displays the cost of the ticket to the customer, Asks for confirmation, Confirms the purchase 
// and Asks if they would like to look at another seat.
int GetCost_and_Confirm(int price[Num_Rows], int row2, int column2, int answer)
{
	int cost;
	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;
	return cost;
}



This is the finished product. I am happy with it. The one thing that I couldn't do was gather up the rest of the contents of case:2 into it's own function but that doesn't really matter. Again I can not thank everyone enough for helping me out today. Hopefully one day I will have the knowledge to return the favor.

Maverick

This post has been edited by Maverick NOS: 10 May 2009 - 09:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1