c++ - 在 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;
}
}`
解决方案
您在 IF 语句中使用了一个 = 而不是两个。使用一个,您只是分配值而不检查它是否真的是真的。
推荐阅读
- python - 配置 PunktSentenceTokenizer 并指定语言
- c# - C#获取构建中使用的浮动版本包的具体版本
- javascript - 如何等待 onChange 事件使用反应钩子设置值?
- c# - 使用 heap.EnumerateRoots() 找不到带有 ClrMD 的实例保留路径
- python - 无需登录/通过 FastApi 的 OAuth2
- excel - 在数据透视表 excel 2019 中没有一对多的工作关系
- vba - 微软 Office 365 展望。如何更改团队-会议加入链接
- apache-spark - spark 并行性和 Elasticsearch slice 是不是类似的东西?
- ios - 如何选择项目目标以在 Flutter 中发布 iOS 应用?
- java - 如何在spring boot中对对象做出响应