首页 > 解决方案 > 为什么我所有的布尔值都设置为真?

问题描述

我通过打印值来测试我的代码,看看为什么它没有输出我应该得到的值。而且我发现我所有用于字符串骑行的布尔值都被设置为真,我似乎不知道为什么。我已经尝试在互联网上搜索,但我找不到任何东西。我对代码的理解不够深入,无法理解这里出了什么问题。

#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

标签: c++

解决方案


我认为这是因为您没有初始化布尔变量。没有单行,您设置“假”。因此,它们得到随机值,即任何非零值都被认为是“真”。


推荐阅读