//我的程序要長,並有錯誤..我想使它簡單易於閱讀..如何改變它在切換程序?請幫幫我!! =(如果其他程序切換程序
#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;
}
顯示兩部不好的電影,只顯示鋼鐵俠,你會砍掉2/3的代碼。 – 2010-10-09 06:16:34
@詹姆斯麥克奈利斯,但是...我喜歡哈利波特.. :) – liaK 2010-10-09 06:18:28
更嚴重的是,你需要小心你的輸入操作。從流中提取可能會失敗,並且您需要檢查並處理失敗。例如,如果用戶爲他的電影選項(「hello」不是有效整數)輸入「hello」,則將選擇變量保持未初始化狀態,並且將設置流中的失敗狀態。你可以使用'if(!cin){/ * handle error * /}'來測試流狀態以查看提取是否成功。你還必須調用'cin.clear()'來清除錯誤狀態並使用'cin.ignore()'來忽略無效輸入。 – 2010-10-09 06:23:28