我通过打印值来测试我的代码,看看为什么它没有输出我应该得到的值。我发现我所有的用于弦乐骑行的布尔值都被设置为真,我似乎不知道为什么。我试过在互联网上搜索,但我找不到任何东西。我对代码的理解不够深入,无法理解这里出了什么问题。
#include <iostream>
#include <string>
using namespace std;
int main() {
//declarations
string ride;
char comp;
int age;
double td, tp, god, rp, sp, ta, tap;
bool fer, bump, zip, pir, test;
//print outs and input
cout<<"What ride would you like to buy a ticket for? "<<endl;
getline(cin, ride);
cout<<"How old are you?"<<endl;
cin>>age;
//functions
//ride
if (ride == "Ferris Wheel"){
rp = rp + 75.00;
fer = true;
}
if (ride == "Bumper Cars"){
rp = rp + 50.00;
bump = true;
}
if (ride == "Zipper"){
rp = rp + 100.00;
zip = true;
}
if (ride == "The Pirate Ship"){
rp = rp + 75.00;
pir = true;
}
//age
//ferris wheel
if (fer = true){
if (age >= 0 && age <= 7){
cout<<"Do you have a companion that is at least 18 years old? Y/N";
cin>>comp;
if (comp == 'Y'){
td = td + .20;
}
else{
cout<<"You need a companion that is at least 18 years old.";
}
}
else if (age >= 8 && age <= 12){
td = td + .10;
}
else if(age >= 13 && age <= 20 ){
td = td + 0.08;
}
else if(age > 20 && age < 60){
td = td + 0.05;
}
}
//bumper car
if(bump = true){
if (age >= 0 && age <= 7){
cout<<"Do you have a companion that is at least 18 years old? Y/N";
cin>>comp;
if (comp == 'Y'){
td = td + .20;
}
else{
cout<<"You need a companion that is at least 18 years old.";
}
}
else if (age >= 8 && age <= 12){
td = td + .10;
}
else if(age >= 13 && age <= 20 ){
td = td + 0.08;
}
else if(age > 20 && age < 60){
td = td + 0.05;
}
else if(age > 60){
td = td + .20;
}
}
//zipline
if (zip = true){
if (age >= 0 && age <= 7){
cout<<"Do you have a companion that is at least 18 years old? Y/N";
cin>>comp;
if (comp == 'Y'){
td = td + .20;
}
else{
cout<<"You need a companion that is at least 18 years old.";
}
}
td = td + 0.10;
}
// pirate ship
if (pir = true){
if (age >= 0 && age <= 7){
cout<<"Do you have a companion that is at least 18 years old? Y/N";
cin>>comp;
if (comp == 'Y'){
td = td + .20;
}
else{
cout<<"You need a companion that is at least 18 years old.";
}
}
else if (age >= 8 && age <= 12){
td = td + .10;
}
else if(age >= 13 && age <= 20 ){
td = td + 0.08;
}
else if(age > 20 && age < 60){
td = td + 0.05;
}
}
// maths jaajbjabjabjaj B(
cout<<fer<<endl;
cout<<bump<<endl;
cout<<zip<<endl;
cout<<pir<<endl;
cout<<td<<endl;
cout<<rp<<endl;
cout<<td<<endl;
god = rp * td;
cout<<"discount computed: "<<god<<endl;
sp = rp - god;
cout<<"sub pirce jajagnkfgdf: "<<sp<<endl;
tap = sp * 0.05;
cout<<"tap: "<<tap;
ta = sp + tap;
cout<<"ta: "<<ta;
}
骑行输入:海盗船 年龄输入:9
我从代码中得到的输出: 我从代码中获得的折扣:30 部分价格:45 总价:47.25
我应该从代码中得到的输出: 我从代码中获得的折扣:7.5 部分价格:67.5 总价:70.875
答案 0 :(得分:4)
我认为这是因为您没有初始化您的布尔变量。没有单行,您可以在其中设置“false”。因此,它们会得到随机值,即任何非零值都被视为“真”。
答案 1 :(得分:3)
为布尔值设置默认值 bool fer = false, bump = false, zip = false, pir = false, test = false;
你用错了 if
。
if (zip = true) // 如果 zip 等于 true.
if (zip == true) // 如果 zip 等于 true?
使用 else if
//骑行 如果(乘坐==“摩天轮”){ rp = rp + 75.00; 费 = 真; }
else if (ride == "Bumper Cars"){ rp = rp + 50.00; 凹凸=真; }...
乘坐不能同时等于“摩天轮”和“碰碰车”。