Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我所有的布尔人都是真的?_C++ - Fatal编程技术网

C++ 为什么我所有的布尔人都是真的?

C++ 为什么我所有的布尔人都是真的?,c++,C++,我通过打印这些值来测试我的代码,看看为什么它没有输出我应该得到的值。我发现我所有的用于弦乐的布尔人都被认为是真的,我似乎不知道为什么。我试着在网上搜索,但什么也找不到。我对代码的理解不够深刻,无法理解这里出了什么问题 #include <iostream> #include <string> using namespace std; int main() { //declarations string ride; char comp; int age

我通过打印这些值来测试我的代码,看看为什么它没有输出我应该得到的值。我发现我所有的用于弦乐的布尔人都被认为是真的,我似乎不知道为什么。我试着在网上搜索,但什么也找不到。我对代码的理解不够深刻,无法理解这里出了什么问题

#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;
   
    
    
  
  
  }
#包括
#包括
使用名称空间std;
int main(){
//声明
绳骑术;
碳化合物;
智力年龄;
双td、tp、god、rp、sp、ta、tap;
bool-fer、bump、zip、pir、test;
//打印输出和输入

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

  • 设置布尔值的默认值
    bool-fer=false、bump=false、zip=false、pir=false、test=false;

  • 如果
  • 错误,则使用的是

    if(zip=true)//如果zip=true

    if(zip==true)//如果zip=true

  • 如果需要,请使用else

    //骑 如果(乘坐=“摩天轮”){ rp=rp+75.00; fer=真; }

    否则,如果(行驶=“碰碰车”){ rp=rp+50.00; 凹凸=真; }


  • 骑乘不能与“摩天轮”和“碰碰车”相提并论同时。

    请在问题中包括输入输出和预期输出您正在访问未初始化的值
    rp=rp+75.00;
    ,导致未定义的行为您也不初始化其他变量,并且
    fer=true
    赋值不比较请注意编译器警告。如果设置正确,您的代码将被删除oes未编译:谢谢!!我已经解决了问题!!这是一个问题,但不是问题所在。
    if(zip=true)
    中的条件计算为
    true
    是否初始化了
    zip