我的代码在不同的平台上给出了不同的输出
My code is giving different outputs in different platforms
首先这是代码。
#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;
Codeblocks 给我这样奇怪的输出:
Enter your word : ankara
______B
Your guess ::
或者这个:
Enter your word : adana
_____rB
Your guess :
有谁知道为什么代码块给我不同的输出?
顺便说一下,请原谅我的英语不好。
使用 C++ 字符串代替 cstrings
#include <iostream>
#include <string>
using namespace std;
int main() {
std::string word;
char guess;
char *pch;
int life=6;
cout<<"Enter your word : ";
cin>>word;
std::string blank(word.size(), '_');
cout<<blank<<endl;
while(blank != word && can >= 0){
if(word == blan){
cout<<"Congratulations you found the word. :)";
break;
}
if(life == 0)break;
cout<<"Your guess :";
cin>>guess;
pch=strchr(word.c_str(),int(guess));
if(pch == NULL){
cout<<"Your guess is wrong try again."<<endl;
cout<<life<<endl;
}
else if(pch != NULL){
while (pch!=NULL){
blank[pch-word.c_str()]=guess;
pch=strchr(pch+1,int(guess));
}
cout<<blank<<endl;
cout<<life<<endl;
life++;
}
--life;
}
if(can == 0)cout<<"Sadly you couldn't find the word. :(";
return 0;
}
首先这是代码。
#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;
Codeblocks 给我这样奇怪的输出:
Enter your word : ankara
______B
Your guess ::
或者这个:
Enter your word : adana
_____rB
Your guess :
有谁知道为什么代码块给我不同的输出? 顺便说一下,请原谅我的英语不好。
使用 C++ 字符串代替 cstrings
#include <iostream>
#include <string>
using namespace std;
int main() {
std::string word;
char guess;
char *pch;
int life=6;
cout<<"Enter your word : ";
cin>>word;
std::string blank(word.size(), '_');
cout<<blank<<endl;
while(blank != word && can >= 0){
if(word == blan){
cout<<"Congratulations you found the word. :)";
break;
}
if(life == 0)break;
cout<<"Your guess :";
cin>>guess;
pch=strchr(word.c_str(),int(guess));
if(pch == NULL){
cout<<"Your guess is wrong try again."<<endl;
cout<<life<<endl;
}
else if(pch != NULL){
while (pch!=NULL){
blank[pch-word.c_str()]=guess;
pch=strchr(pch+1,int(guess));
}
cout<<blank<<endl;
cout<<life<<endl;
life++;
}
--life;
}
if(can == 0)cout<<"Sadly you couldn't find the word. :(";
return 0;
}