tags:

views:

97

answers:

3

//my program to long and have error..i want to make it simple and easy to read..how to change it in switch program?? help me please!! =(

#include <iostream>

using namespace std;

int main ()
{
    int movie1;
    int movie2;
    int movie3;
    int seats;
    int price;
    int select;
    char response;

    cout<<"______Wellcome to Strawberry Gold Cinema____\n"
        <<"Now you are booking a ticket cinema and please choose your movie... ";
        cout << "\nPress 1 for= Harry Potter \n"
            << "Press 2 for = Iron Man\n"
            <<"Press 3 for = Romeo and Juliet\n";
        cout<<"Enter your choice > ";
        cin>>select;


 if (select ==1 )

 {


     cout<<"The price of the ticket per seat is RM10.00\n"
        <<"Please enter the number of seat ";
        cin>>seats;


        if (seats<=30)

        {
            price = 10 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;
        }



        if ( toupper( response ) == 'Y' )
        {
        cout <<"Please choose movie 2 or movie 3\n"
        <<"Enter your choice ";
        cin>>select;




        if (select == 2)
        {
         cout<<"The price of the ticket per seat is RM11.00\n"
           <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 11 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        }
        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;

        }
            if ( toupper( response ) == 'Y' )


            {
             cout<<"The price of the ticket per seat is RM13.00\n"
             <<"Please enter the number of seat";
            cin>>seats;


        if (seats<=30)

        {
            price = 13 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Next movie in 5 hours...."<<endl;
        }

else
        cout << "Next movie in 5 hours.\n";
        }
        }
        }

//for 2

 if (select ==2 )

 {


     cout<<"The price of the ticket per seat is RM11.00\n"
        <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 11 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;
        }



        if ( toupper( response ) == 'Y' )
        {
        cout <<"Please choose movie 1 or movie 3\n"
        <<"Enter your choice ";
        cin>>select;




        if (select == 2)
        {
         cout<<"The price of the ticket per seat is RM10.00"
           <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 10 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        }
        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;

        }
            if ( toupper( response ) == 'Y' )


            {
             cout<<"The price of the ticket per seat is RM13.00\n"
             <<"Please enter the number of seat";
            cin>>seats;


        if (seats<=30)

        {
            price = 13 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Next movie in 5 hours...."<<endl;
        }
else
        cout << "Next movie in 5 hours.\n";

        }
        }
        }



//for seat 3

 if (select ==3 )

 {


     cout<<"The price of the ticket per seat is RM13.00\n"
        <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 13 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;
        }



        if ( toupper( response ) == 'Y' )
        {
        cout <<"Please choose movie 1 or movie 2\n"
        <<"Enter your choice ";
        cin>>select;




        if (select == 1)
        {
         cout<<"The price of the ticket per seat is RM10.00\n"
           <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 10 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        }
        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;

        }
            if ( toupper( response ) == 'Y' )


            {
             cout<<"The price of the ticket per seat is RM12.00\n"
             <<"Please enter the number of seat";
            cin>>seats;


        if (seats<=30)

        {
            price = 12 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Next movie in 5 hours...."<<endl;
        }
         else
        cout << "Next movie in 5 hours.\n";
        }
        }
        }






 return 0;

}
+3  A: 

As a rule of thumb, any time you find yourself copying and pasting more than one or two lines of code, you should stop and think whether it would be cleaner to refactor that code into a function.

You can in this case create a function do_movie_specific_stuff() that takes as its arguments the data that is different between the three movies (if there is anything different between the three switch cases). Then each case in your switch statement is a single statement: a call to that function with the correct arguments.

James McNellis
I strongly suspect that this person's class hasn't learned about writing custom functions yet. Early on, functions are only called, not written, and `int main()` is still just a magical incantation that goes at the top of a program.
Rob Kennedy
+1  A: 

I would recommend putting your logic into a loop and breaking your logic into functions, or possibly classes, if you're familiar. Something like this (this is non-compiled/non-tested semi-pseudo code... just to give you an idea):

bool done = false;
do
{
    cout << "Please choose a move (1, 2, or 3)" << endl;
    cin >> movieId;

    double price = getMoviePrice(movieId);
    cout << "The price of the movie is: " + price << endl;

    cout << "How many seats" << endl;
    cin >> seats;

    int availableSeats = getAvailableSeats(movieId);

    // and so on...  If the user indicates they want to quit, just set done to true!

} while (!done)

If you need to keep track of what movies the user has already tried, you can do that, you'll just have to keep track of that somewhere, and handle it in the logic.

Andy White
A: 

The code you have shown is a good example with the problems of purely linear code: It just doesn't work.

As a tip: I would either create three functions to handle each movie, or more preferably, create a generic function to handle all movies, which will take its data from some class:

class Movie {
  std::wstring name;
  int seats;
  int soldSeats;
  int pricePerSeat;
};
Alexander Rafferty