所以我正在做关于电影订购系统的作业。给定的那一天应该显示它自己的电影列表,但我得到了另一件事。
整个代码
#include <iostream>
#include <string>
using namespace std;
int main() {
float totalPayment, totalPrice, price, discount, priceD, TotalpriceD;
string customerSay, ticket[99], day, days, priceS;
int movie, order, i, x, status, list;
//Ask if the user have membership or not, will apply discount
cout<<"Are you a ...?"<<endl<<"1. Member"<<endl<<"2. Student"<<endl<<"3. Non-member"<<endl<<"Status: ";
cin>>status;
switch (status) {
case 1:
discount=0.90;
break;
case 2:
discount=0.88;
break;
default:
discount=1;
}
//The receipt loop start here
do {
x=0;
while (x<1) {
cout<<endl<<"What day do you want to watch the movie? ";
cin>>day;
if (day=="Monday") {
list=1;
price=8.00;
x++;
}
else if (day=="Tuesday") {
list=1;
//priceD is for price that's not affected by any discount
priceD=8.00;
x++;
}
else if (day=="Thursday") {
list=1;
price=8.00;
x++;
}
else if (day=="Wednesday") {
list=2;
priceD=5.00;
x++;
}
else if (day=="Sunday") {
list=3;
price=10.00;
x++;
}
else {
cout<<"Sorry, during that day there's no movie available to watch."<<endl<<"Please choose another day to watch the movie."<<endl;
}
}
totalPrice=totalPrice+price;
TotalpriceD=TotalpriceD+priceD;
cout<<endl;
//Show the movie list based on the day given
if (list=1) {
cout<<"Movie's Ticket Available to Watch:"<<endl;
cout<<"MOVIE PRICE(RM)"<<endl;
cout<<"1) Ant-Man and the Wasp: Quantumania 8.00"<<endl;
cout<<"2) Avengers: The King Dynasty 8.00"<<endl;
cout<<"3) Shang-Chi and the Legend of the Ten Rings 8.00"<<endl;
cout<<"4) Spider-Man: No Way Home 8.00"<<endl;
cout<<"5) Eternals 8.00"<<endl<<endl;
cout<<"Which movie do you want to watch? ";
cin>>movie;
switch (movie) {
case 1:
ticket[i]="Ant-Man and the Wasp: Quantumania 8.00";
break;
case 2:
ticket[i]="Avengers: The King Dynasty 8.00";
break;
case 3:
ticket[i]="Shang-Chi and the Legend of the Ten Rings 8.00";
break;
case 4:
ticket[i]="Spider-Man: No Way Home 8.00";
break;
case 5:
ticket[i]="Eternals 8.00";
break;
}
}
else if (list=2) {
cout<<"Movie's Ticket Available to Watch:"<<endl;
cout<<"MOVIE PRICE(RM)"<<endl;
cout<<"1) Ant-Man and the Wasp: Quantumania 5.00"<<endl;
cout<<"2) Avengers: The King Dynasty 5.00"<<endl;
cout<<"3) Shang-Chi and the Legend of the Ten Rings 5.00"<<endl;
cout<<"4) Spider-Man: No Way Home 5.00"<<endl;
cout<<"5) Eternals 5.00"<<endl<<endl;
cout<<"Which movie do you want to watch? ";
cin>>movie;
switch (movie) {
case 1:
ticket[i]="Ant-Man and the Wasp: Quantumania 5.00";
break;
case 2:
ticket[i]="Avengers: The King Dynasty 5.00";
break;
case 3:
ticket[i]="Shang-Chi and the Legend of the Ten Rings 5.00";
break;
case 4:
ticket[i]="Spider-Man: No Way Home 5.00";
break;
case 5:
ticket[i]="Eternals 5.00";
break;
}
}
else {
cout<<"Movie's Ticket Available to Watch:"<<endl;
cout<<"MOVIE PRICE(RM)"<<endl;
cout<<"1) Ant-Man and the Wasp: Quantumania 10.00"<<endl;
cout<<"2) Avengers: The King Dynasty 10.00"<<endl;
cout<<"3) Shang-Chi and the Legend of the Ten Rings 10.00"<<endl;
cout<<"4) Spider-Man: No Way Home 10.00"<<endl;
cout<<"5) Eternals 10.00"<<endl<<endl;
cout<<"Which movie do you want to watch? ";
cin>>movie;
switch (movie) {
case 1:
ticket[i]="Ant-Man and the Wasp: Quantumania 10.00";
break;
case 2:
ticket[i]="Avengers: The King Dynasty 10.00";
break;
case 3:
ticket[i]="Shang-Chi and the Legend of the Ten Rings 10.00";
break;
case 4:
ticket[i]="Spider-Man: No Way Home 10.00";
break;
case 5:
ticket[i]="Eternals 10.00";
break;
}
break;
}
i++;
cout<<"Thank you for your purchase!"<<endl;
cout<<"Do you want to make another ticket order? (Y for Yes; N for No) ";
cin>>customerSay;
} while (customerSay=="Y");
order=i;
//Receipt loop starts here
for (i=0;i<order;i++) {
cout<<ticket[i]<<endl;
}
totalPayment=totalPrice*discount+TotalpriceD;
//Test to see if the code work
cout<<discount<<endl;
cout<<totalPrice<<endl;
cout<<totalPayment<<endl;
return 0;
}
输出
Are you a ...?
1. Member
2. Student
3. Non-member
Status: 3
What day do you want to watch the movie? Wednesday
Movie's Ticket Available to Watch:
MOVIE PRICE(RM)
1) Ant-Man and the Wasp: Quantumania 8.00
2) Avengers: The King Dynasty 8.00
3) Shang-Chi and the Legend of the Ten Rings 8.00
4) Spider-Man: No Way Home 8.00
5) Eternals 8.00
Which movie do you want to watch? 1
--------------------------------
Process exited after 32.66 seconds with return value 3221225477
Press any key to continue . . .
知道为什么会这样吗?我该如何解决?顺便说一句,第一次使用 stackoverflow
输入一天,期待正确的列表出来,错误的列表无法输入看什么电影,代码自行退出并返回值巨大
首先你已经正确检查了
if (Day == "Wednesday)"
但是然后做if (list=1)
你也应该使用'=='的地方,这应该修复显示的错误数据。
下一个问题是变量 i 从未被初始化,所以在执行
ticket[i]="Ant-Man and the Wasp: Quantumania 10.00";
时程序不知道 i 的值是什么。
您的代码中还有许多其他未初始化的变量,将来可能会导致更多问题。