C++ 为什么我的代码只检查我的刽子手游戏(c+;+;)中单词的第一个字母?
这是我的刽子手游戏,我很抱歉在这个代码中犯了任何低级错误C++ 为什么我的代码只检查我的刽子手游戏(c+;+;)中单词的第一个字母?,c++,C++,这是我的刽子手游戏,我很抱歉在这个代码中犯了任何低级错误 #include <iostream> #include <Windows.h> #include <fstream> #include <time.h> int main () { system ("CLS"); system("COLOR FC"); std::cout<<"\t\t# #
#include <iostream>
#include <Windows.h>
#include <fstream>
#include <time.h>
int main ()
{
system ("CLS");
system("COLOR FC");
std::cout<<"\t\t# # # # "<<std::endl;
std::cout<<"\t\t# # ## # # #### ## ## ## # #"<<std::endl;
std::cout<<"\t\t# # # # ## # # # # # # # # # ## #"<<std::endl;
std::cout<<"\t\t####### # # # # # # # # # # # # # #"<<std::endl;
std::cout<<"\t\t# # ###### # # # # ### # # ###### # # #"<<std::endl;
std::cout<<"\t\t# # # # # ## # # # # # # # ##"<<std::endl;
std::cout<<"\t\t# # # # # # #### # # # # # #"<<std::endl;
Sleep (3000);
system ("CLS");
char word [128];
int a=0;
do
{
system ("CLS");
switch (a % 5)
{
case 0:
std::cout<<"loading..."<<std::endl;
break;
case 1:
std::cout<<"loading.."<<std::endl;
break;
case 2:
std::cout<<"loading."<<std::endl;
break;
case 3:
std::cout<<"loading.."<<std::endl;
break;
case 4:
std::cout<<"loading..."<<std::endl;
break;
}
Sleep (50);
++a;
}
while (a < 50);
system ("CLS");
std::cout<<"\t \t \t Welcome to my Hangman game"<<std::endl; // start of game
Sleep (2500);
system ("CLS");
std::cout<<"\t \t \t \t Here are the rules"<<std::endl;
Sleep (2500);
system ("CLS");
std::cout<<"\t You will have six lives in order to guess the letters within a word"<<std::endl;
Sleep (2500);
system ("CLS");
std::cout<<"\t \t After all of your lives have been used up you lose"<<std::endl;
Sleep (2500);
system ("CLS");
std::cout<<"\t \t \t \t Good luck"<<std::endl;
Sleep (2500);
system ("CLS");
int game; // press key to continue
std::cout<<"\t \t Do you wish to play"<<std::endl<<"\t \t Press 1 to play or press any other key to exit"<<std::endl;
std::cin>> game;
if (game = 1) // code for game
{
system ("CLS");
std::cout<<"\t \t You have chosen to play hangman \n"<<"\t \t Good luck"<<std::endl;
int randnum; //random number generator (co developed with jeremy dyer)
srand(time(NULL));
randnum = rand() % 10000 + 1;
std::ifstream ifs("words.txt"); //search text file for a word within the file
char temp[128];
while(!ifs.eof() && (randnum > 0))
{
ifs >> temp;
--randnum;
}
strcpy_s (word, temp); //temp to word not word to temp
int lives = 6; // set amount of lives
char guess; // declare guess
for(int i = 0; i < 25; i++) //checking letter input against the word
{
char c = word[i];
if(c == 0)
{
continue;
}
std::cout<< word; // help with testing (so i can see what word has been picked)
while ( lives >0)
{
std::cout<<"\t \t please enter your guess"<<std::endl;
std::cin>> guess;
if (guess == c)
{
system("CLS");
std::cout<<"that is correct"<<std::endl;
std::cout<<" you have "<< lives<<" remaining"<<std::endl;
}
else
{
system("CLS");
lives --;
std::cout<<" bad luck that is wrong"<<std::endl;
std::cout<<" you have "<< lives<<" remaining"<<std::endl;
}
}
if (lives <= 0)
{
system("CLS");
std::cout<<" Sorry you lose"<<std::endl<<" Please try again"<<std::endl;
std::cin.get();
return 0;
}
}
}
return 0;
}
#包括
#包括
#包括
#包括
int main()
{
系统(“CLS”);
系统(“彩色FC”);
std::cout本质上,您的代码是这样做的:
for letter in word:
while lives > 0:
read guess
if guess == letter:
...
else:
...
if lives = 0:
end game
end while
end for
您希望它改为这样做:
while lives > 0:
read guess
for letter in word:
if guess == letter:
...
end for
if no match was found:
lives = lives - 1
end while
当前,您正在最外层的循环中循环您的单词。您需要在每个猜测中循环整个单词。我喜欢调试器,它也会喜欢您。您好。欢迎使用堆栈溢出。您应该发布一个。有很多代码不需要诊断问题。您应该阅读猜测,然后在le上循环对不起,博洛夫,为了完成这项任务,我已经醒了好一阵子了,巴玛,你能不能把这一点扩大一下,正如我说的,我已经起床很久了,现在我几乎只靠咖啡因为生啊,我明白了,谢谢你,这是一个巨大的帮助:)谢谢你的帮助,我工作了一段时间现在,我检查了整个单词,但是如果我输入了一个不正确的值,然后尝试输入一个正确的值,它会说它仍然不正确。如果不查看代码,就不可能知道什么可能是错误的,但是如果您使用某种变量来跟踪猜测是否是错误的,那么可能您忘记了在每个新值上重置它猜猜看?谢谢你的回复,我重写了一些代码,发现了问题。非常感谢你的帮助