我试图构建一个可以预订票的小型C ++程序
我遇到两个问题
这是您可以阅读以了解问题的代码,并且如果对此代码进行任何编辑以使其比这更好,我将非常高兴并在此先感谢。
#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";
}
答案 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;
}
}
}