首页 > 解决方案 > 在 printme 函数中,它将所有 false 设置为 true

问题描述

我正在创建一个简单的汽车创作者游戏来练习继承和类之类的东西。我对此感到困惑。当我调试时,它说值是它们应该是的值,但随后它转到 printme 函数,无论如何它都将它们设置为 true。

cout << "Does your car have stripes?" << endl;
bool validInput8; // stops from sending code all of the place and mixing cars
string inputStripes;
inputStripes = lower(inputStripes);
do
{
cin >> inputStripes;
    inputStripes = lower(inputStripes);

    if (inputStripes == "yes")
    {
        sportscar.Stripes = true;
        validInput8 = true;

    }
    else if (inputStripes == "no")
    {
        sportscar.Stripes = false;
        validInput8 = true;
    }
    else
    {
        cout << "Your input is not valid. Please enter yes or no!" << endl;
        cin.get();
        validInput8 = false;
    }
} while (!validInput8);
Continue();
cin.get();
clear();
sportscar.PrintCarDetails(); 

这是打印功能

#include "SportsCar.h"

SportsCar::SportsCar()
{
    Spoilers = false;
    Stripes = false;
}
void SportsCar::PrintCarDetails()
{
    cout << "You have finished your car! You are " <<                                                                         
ColorTypeToString(Color) << " and have " << numdoors << " doors!" << endl;
        if (Spoilers = true)

{
    cout << "Your sports car has some super sweet spoilers and you look like a total baller" << endl;
}
else if (Spoilers = false)
{
    cout << "Your car doesnt have spoilers so you are boring" << endl;
}
if (Stripes = true)
{
    cout << "Your car has stripes and you will often be confused as a racecar" << endl;
}
else if (Stripes = false)
{
    cout << "You dont have stripes but your driving a sports car who can complain" << endl;
}

        }`

标签: c++

解决方案


您在 IF 语句中使用了一个 = 而不是两个。使用一个,您只是分配值而不检查它是否真的是真的。


推荐阅读