C++ 如果有其他程序,则切换程序

C++ 如果有其他程序,则切换程序,c++,c++builder,C++,C++builder,//我的程序太长并且有错误。我想让它简单易读。如何在切换程序中更改它??请帮帮我= #include <iostream> using namespace std; int main () { int movie1; int movie2; int movie3; int seats; int price; int select; char response; cout<<"______Wellcome

//我的程序太长并且有错误。我想让它简单易读。如何在切换程序中更改它??请帮帮我=

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

}

根据经验,每当您发现自己复制和粘贴了一行或两行以上的代码时,您都应该停下来思考将代码重构为函数是否更干净


在本例中,您可以创建一个函数do_movie_specific_stuff,该函数将三部电影之间不同的数据作为其参数(如果三种切换情况之间有任何不同)。然后,switch语句中的每一个实例都是一个单独的语句:使用正确的参数调用该函数。

根据经验,任何时候当您发现自己复制和粘贴了一行或两行以上的代码时,您都应该停下来思考,将代码重构为函数是否更干净


在本例中,您可以创建一个函数do_movie_specific_stuff,该函数将三部电影之间不同的数据作为其参数(如果三种切换情况之间有任何不同)。然后,switch语句中的每种情况都是一个单独的语句:使用正确的参数调用该函数。

我建议您将逻辑放入循环中,并将逻辑拆分为函数或类(如果您熟悉的话)。类似的东西这是未编译/未测试的半伪代码。。。给你一个想法:

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)

如果您需要跟踪用户已经尝试过的电影,您可以这样做,您只需在某个地方跟踪,并在逻辑中处理它。

我建议将您的逻辑放入一个循环,并将逻辑分解为函数或类(如果您熟悉的话)。类似的东西这是未编译/未测试的半伪代码。。。给你一个想法:

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)

如果你需要跟踪用户已经尝试过的电影,你可以这样做,你只需要在某个地方跟踪,并在逻辑中处理它。

你展示的代码是纯线性代码问题的一个很好的例子:它根本不起作用

提示:我将创建三个函数来处理每部电影,或者更可取的是,创建一个通用函数来处理所有电影,该函数将从某个类获取其数据:

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

您展示的代码是纯线性代码问题的一个很好的例子:它根本不起作用

提示:我将创建三个函数来处理每部电影,或者更可取的是,创建一个通用函数来处理所有电影,该函数将从某个类获取其数据:

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

如果你停止放映这两部糟糕的电影,只放映《钢铁侠》,你会删掉2/3的代码。@James McNellis,但是。。。我喜欢哈利·波特更重要的是,您需要小心输入操作。从流中提取可能会失败,您需要检查并处理失败。例如,如果用户为其电影选择输入hello,则hello不是有效的整数,select变量将保持未初始化状态,并且流上的失败状态将被设置。您可以使用if!测试流状态以查看提取是否成功!cin{/*句柄错误*/}。您还必须调用cin.clear来清除错误状态,并使用cin.ignore来忽略无效输入。这是家庭作业,不是吗?任务的哪一部分你遇到了麻烦?当你要求帮助和澄清时,你的导师说了什么?记住你已经为帮助付费了,所以你最好在有帮助的时候利用它。如果你停止放映这两部糟糕的电影而只放映《钢铁侠》,你将删掉代码的2/3。@James McNellis,但是。。。我喜欢哈利·波特更重要的是,您需要小心输入操作。从流中提取可能会失败,您需要检查并处理失败。例如,如果用户为其电影选择输入hello,则hello不是有效的整数,select变量将保持未初始化状态,并且流上的失败状态将被设置。您可以使用if!测试流状态以查看提取是否成功!cin{/*句柄错误*/}。您还必须调用cin.clear来清除错误状态,并使用cin.ignore来忽略无效输入。这是家庭作业,不是吗?任务的哪一部分你遇到了麻烦?当你要求帮助和澄清时,你的导师说了什么?请记住,您已经为该帮助支付了费用,因此您可以在它可用时利用它。我强烈怀疑此人的类尚未学习编写自定义函数。在早期,函数只被调用,而不是编写,int main仍然只是程序顶部的一个神奇咒语。我强烈怀疑这个人的类还没有学会编写自定义函数。早期,函数只被调用,而不是编写,int main仍然只是一个位于p顶部的魔法咒语 程序。