Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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++;_C++ - Fatal编程技术网

C++ 猜字游戏C++;

C++ 猜字游戏C++;,c++,C++,我是编程新手,所以我创建了一个数字猜谜游戏,它工作得很好,但我似乎不能用猜字代码来结束。我的目标是在猜测的字符串正确时打印“恭喜”,但我尝试了许多方法,但仍然无法使其工作 #include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(0)); int i;

我是编程新手,所以我创建了一个数字猜谜游戏,它工作得很好,但我似乎不能用猜字代码来结束。我的目标是在猜测的字符串正确时打印“恭喜”,但我尝试了许多方法,但仍然无法使其工作

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(0));
    int i;
    const string wordList[17] = { "television",
        "computer", "keyboard", "laptop", "mouse", "phone", "headphones",
        "screen", "camera", "sound", "science", "programming", 
        "entertainment",
        "graphics", "intelligent", "memory", "remote" };

    string word = wordList[rand() % 17];

    for(i = 0; i < word.length(); i++)
    {
        if(word[i] == 'a' || word[i] == 'e' || word[i] == 'i' ||
           word[i] == 'o' || word[i] == 'u')
        {
            word[i] = '_';
        }
    }

    cout << word << endl;
    int n=0;
    string x;
    do
    {
        n++;
        cin >> x;
    }
    while(x!=word[i]);
    cout<<"Congratulations! You guessed the word!";

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
srand(时间(0));
int i;
const string单词列表[17]={“电视”,
“电脑”、“键盘”、“笔记本电脑”、“鼠标”、“手机”、“耳机”,
“屏幕”、“摄像机”、“声音”、“科学”、“编程”,
“娱乐”,
“图形”、“智能”、“内存”、“远程”};
string word=wordList[rand()%17];
对于(i=0;icout我想说你的大部分问题都归结到这一行:

while(x!=word[i]);
如评论所示,
word
是您修改过的单词,而不是单词列表。但是,
i
是错误的索引。因此,请保存您先前选择的单词索引:

size_t wordIndex = rand() % 17;
string word = wordList[wordIndex];
然后更改
do
循环条件:

while (x != wordList[wordIndex]);
我还建议你


你可以,但这可能不值得。请注意,
rand()
有缺点,并且存在更好的替代方案。

所显示的代码中有什么不正确的地方?请思考
word
中包含的字符。所有的元音怎么了?你会比较2
string
s吗?另外,当你将最新的字符与
word[i]
进行比较时,
i
来自哪里?你的编译器应该会给你一个
错误,而(x!=word[i])
所以我用了
char x
而不是
string x
现在它可以编译了,但是当我输入正确的单词时,即使它是正确的,它仍然会重复