C++ 程序将不会在消息语句发出时运行
我正在制作一个游戏,我希望它只有在玩家同意的情况下运行,并说“玩”,但它不会运行。这是我的密码:C++ 程序将不会在消息语句发出时运行,c++,C++,我正在制作一个游戏,我希望它只有在玩家同意的情况下运行,并说“玩”,但它不会运行。这是我的密码: #include <iostream> #include <stdlib.h> using namespace std; int main() { int balance = 500; char start; cout << "Welcome to Vegas! Your starting balance is 500 dollars
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int balance = 500;
char start;
cout << "Welcome to Vegas! Your starting balance is 500 dollars lets play! " << endl;
cout << "Type PLAY to begin !" <<endl;
cin >> start;
while(start == 'PLAY') {
cout << "Your beginning numbers are!" << endl;
}
}
#包括
#包括
使用名称空间std;
int main()
{
国际收支平衡=500;
字符启动;
coutstart
是一个字符,而不是字符数组。在start
改变它,也改变<代码>“播放”<代码> >代码>播放< <代码>。考虑使用<代码> STD::String < /Cord>。
您可以做的示例:
int balance = 500;
char start[256];
bool started = false;
cout << "Welcome to Vegas! Your starting balance is 500 dollars lets play! " << endl;
cout << "Type PLAY to begin !" <<endl;
cin >> start;
if (!strcmp(start, "PLAY")) { // #include <cstring>
started = true;
}
while(started) {
cout << "Your beginning numbers are!" << endl;
}
char
是单个字符,类似于'p'
。如果您希望用户输入完整字符串,则需要使用std::string
。此外,您可能只需检查用户是否输入了您想要的字符串,而不是在游戏结束时,除非您希望在游戏结束时再次提示
更正代码为:
std::string start;
// ...
std::cin >> start;
if (start == "PLAY") {
// play the game
}
将char start;
更改为char start[100];
和while(开始=='PLAY')
到while(开始=='PLAY')
这是最后的代码
int balance = 500;
char start[100];
cout << "Welcome to Vegas! Your starting balance is 500 dollars lets play! "<< endl;
cout << "Type PLAY to begin !" <<endl;
cin >> start;
while(!strcmp(start,"PLAY")) {
cout << "Your beginning numbers are!" << endl;
}
int balance=500;
字符开始[100];
cout您试图接受一个字符串作为输入,但使用一个字符进行存储。
使用
字符开始[大小]
此外,你确定与“播放”比较的方式正确吗?对字符串使用“播放”。最好使用字符串函数
int balance = 500;
char start[100];
cout << "Welcome to Vegas! Your starting balance is 500 dollars lets play! "<< endl;
cout << "Type PLAY to begin !" <<endl;
cin >> start;
while(!strcmp(start,"PLAY")) {
cout << "Your beginning numbers are!" << endl;
}