如果在c++中有语句,那就大错特错了
If statement in c++, freaks out
当我试图运行这个游戏,并想通过键入"slut"或"slut"(瑞典语中"end"的意思)来结束它时,当我在while循环或If语句中只有一个语句时,它会让一个新玩家称之为"slut"或"slut"但一切正常。更清楚地说,当我有(playerName!="荡妇")但为了安全起见,我有(playerCame!="Slut")||(playerName!="Slut")时它就工作了,然后它就不工作了。
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main(){
//Declaration
setlocale(LC_ALL, "Swedish"); //Swedish characters
int dices;
int diceSum = 0;
int absoluteLowestSum = 999999999; //To get out the lowest sum I'm comparing to a big value
int diceRoll1 = 0;
int diceRoll2 = 0;
string playerName;
string playerLost;
//Plants a seed
srand((int)time(0));
//Instructions
cout << "Nu spelar vi ”Otur i tärning”: " << endl;
cout << "Hur många tärningskast ska göras per spelare?" << endl;
cin >> dices;
//If "slut"/""Slut" is written, while stops
while ((playerName != "Slut") || (playerName != "slut")){
cout << "Namnet på spelaren?";
cin >> playerName;
for (int i = 0; i < dices; i++){
if ((playerName != "Slut") || (playerName != "slut")){
diceRoll1 = rand() % 6 + 1;
diceRoll2 = rand() % 6 + 1;
cout << diceRoll1 << " " << diceRoll2 << " = " << diceRoll1 + diceRoll2 << endl;
diceSum = diceSum + diceRoll1 + diceRoll2;
}
}
if ((playerName != "Slut") || (playerName != "slut")){
while (diceSum < absoluteLowestSum){ //Give us the lowest sum
absoluteLowestSum = diceSum;
playerLost = playerName; //Give us the name who lost
}
cout << "Summan: " << diceSum << endl;
diceSum = 0; //Puts the sum to 0 when a new player enters his/her name
}
if ((playerName == "Slut") || (playerName == "slut")){
cout << playerLost << " hade mest otur och fick tärningssumman " << absoluteLowestSum << endl;
}
}
return 0;
}
您必须使用&&
而不是||
。它应该是
if ((playerName != "Slut") && (playerName != "slut"))
和
while ((playerName != "Slut") && (playerName != "slut"))
在所有具有!=
(playerName != "Slut"
和playerName != "slut"
)的if
s和while
条件下
相关文章:
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- 首要问题的答案让值班员搞错了
- 看起来is_nothrow_constructible_v()在MSVC中被破坏了,我错了吗
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 我的C++线程做错了什么?
- 如何正确使用 >=?(a+f()+c)>=0 错了吗?
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- 没有输出的合并排序我做错了什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 在C++中使用 AKS 素数测试计算双胞胎素数 我做错了什么?
- 指针相关的UE4崩溃.我的指针哪里错了?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- FFMPEG,C++,内存泄漏,我做错了什么?
- 我做错了什么?反向字符串 C++
- 我在这个课上做错了什么?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 理解C++内存顺序,我错了吗?