首先是代码。
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char word[20];
char blank[20];
char guess[1];
char *pch;
int life=6;
cout<<"Enter your word : ";
cin>>word;
for(unsigned int i=0;i<strlen(word);i++){
blank[i]='_';
}
cout<<blank<<endl;
while(blank != word && life >= 0){
if(strcmp(word,blank) == 0){
cout<<"Congratulations you found the word. :)";
break;
}
if(life == 0)break;
cout<<"Your guess :";
cin>>guess[0];
pch=strchr(word,int(guess[0]));
if(pch == NULL){
cout<<"Your guess is wrong try again."<<endl;
cout<<life<<endl;
}
else if(pch != NULL){
while (pch!=NULL){
blank[pch-word]=guess[0];
pch=strchr(pch+1,int(guess[0]));
}
cout<<blank<<endl;
cout<<life<<endl;
life++;
}
--life;
}
if(life == 0)cout<<"Sadly you couldn't find the word. :(";
return 0;
}
因此,我打算编写一个子手代码。我设法在Eclipse中处理这个非常糟糕的书面代码。它多少可以完成工作,但是当尝试将其实现为代码块时,程序的这一部分似乎无法正常工作。
for(unsigned int i=0;i<strlen(word);i++){
blank[i]='_';
}
cout<<blank<<endl;
代码块给我这样奇怪的输出:
Enter your word : ankara
______B
Your guess ::
或此:
Enter your word : adana
_____rB
Your guess :
有人知道为什么代码块会给我不同的输出吗?对不起,我的英语不好。
使用C ++字符串而不是cstrings