简单的程序可以用C ++预订票

时间:2019-01-31 02:16:03

标签: c++ algorithm computer-science

我试图构建一个可以预订票的小型C ++程序

我遇到两个问题

  1. 运行代码“我不知道为什么”时出现错误
  2. 我想增加每25人之后的航班数量

这是您可以阅读以了解问题的代码,并且如果对此代码进行任何编辑以使其比这更好,我将非常高兴并在此先感谢。

#include <iostream>
#include<string>
using namespace std;
static int flight_num=1;
int main()
{
string name;
char reply;

START:

cout<<"do you want to book a ticket (y/n) \n";
cin>>reply;

if(cin.fail())
{
    cin.clear();
    cin.ignore(10000,'\n');
    goto START;
}

else if(reply=='n')
{
    cout<<"Thanks for using our program \n";
    return 0;
}

//Loop to make a repetition for book a ticket

while(reply=='y')
{
    LOOP:

cout<<"please enter your name as written in your passport \n";
cin>>name;

issue_ticket(flight_num,name);

cout<<"Do you wan to book onther one (y/n)"<<"\n";
cin>>reply;

if(cin.fail())
{
    cin.clear();
    cin.ignore(10000,'\n');
    goto LOOP;
}

else if(reply=='n')
{
    cout<<"Thanks for using our program \n";
    return 0;
}

else if(reply=='y')
{
    goto LOOP;
}
}

//function of tickets

void issue_ticket (int flight_num , string name)
{
    int ticket_num=0;


    cout<<" \t \t ***************************** \n";

    cout<<"Flight number : "<<flight_num<<"\n";
    cout<<"Ticket number: "<<ticket_num ++<<"\n";
    cout<<"Issued for: "<<name<<"\n";

    cout<<" \t \t ***************************** \n";
}

1 个答案:

答案 0 :(得分:2)

我做了以下修改:

  • 删除goto和不必要的cin
  • ticket_num每次您想预订新票时都会增加
  • flight_num将在ticket_num达到25后增加

您可能不希望同时使用using namespace std。你可以自己做。该代码将起作用:

#include <iostream>
#include<string>
using namespace std;
static int flight_num=1;

//function of tickets
void issue_ticket (int flight_num , int ticket_num, string name)
{
    cout<<" \t \t ***************************** \n";

    cout<<"Flight number : "<<flight_num<<"\n";
    cout<<"Ticket number: "<<ticket_num ++<<"\n";
    cout<<"Issued for: "<<name<<"\n";

    cout<<" \t \t ***************************** \n";
}

int main()
{
    string name;
    char reply;
    int ticket_num=0;

    cout<<"do you want to book a ticket (y/n) \n";
    cin>>reply;

    if(reply=='n')
    {
        cout<<"Thanks for using our program \n";
        return 0;
    }

    //Loop to make a repetition for book a ticket
    while(reply=='y')
    {  
        ticket_num++;

        // >=25, >=50, >=75...
        //if (ticket_num % 25 == 0)
        //flight_num++;

        cout<<"please enter your name as written in your passport \n";
        cin>>name;

        issue_ticket(flight_num, ticket_num, name);

        // >25, >50, >75...
        if (ticket_num % 25 == 0)
            flight_num++;

        cout<<"Do you wan to book onther one (y/n)"<<"\n";
        cin>>reply;

        if(reply=='n')
        {
            cout<<"Thanks for using our program \n";
            return 0;
        }
    }
}