现在我的游戏正确计算了我有多少正确但是如果不匹配的字符的大约等于字符串的长度它不会计数。我已经尝试修改这段代码一段时间了,但它仍然无法修复。这是我的代码:
/*includes and defines*/
#include <stdio.h>
#include <string.h>
#define SIZE 50
/*prototype definitions*/
int compareString(char *, char *);
int main(void){
char word[SIZE];
char input[SIZE];
char guess[SIZE];
int count = 0;
int wrong = 0;
int incorrect = 0;
int right = 0;
int len = 0;
printf("Please enter the word you would like to have to guess.\nThen hand your computer over to the person you would like to have play:");
fgets(word, SIZE, stdin);
len = strlen(word);
printf("Please guess one letter for the %d letter word!\n", len - 1);
do{
fgets(input, SIZE, stdin);
for(count = 0; count < len - 1; count++){
if(input[0] == word[count]){
printf("that letter is in the %d spot\n", count + 1);
++right;
}
/*I know the problem lies here but i'm not sure how to fix it I've tried not using len-1 and just using len, I've tried not resetting the amount wrong. Everything!*/
else if (input[0] != word[count]) {
++wrong;
if(wrong == len - 1){
++incorrect;
}
wrong = 0;
}
}
}while(incorrect < 6 && right < len - 1);
return 0;
}
我知道问题出在这里,代码设置玩家的错误。但是,我不确定如何解决它。我试过不使用len-1而只是使用len,我已经尝试不重置错误的数量。
else if (input[0] != word[count]) {
++wrong;
if(wrong == len - 1){
++incorrect;
}
wrong = 0;
}
答案 0 :(得分:0)
问题在于您尝试增加incorrect
的方式。
只需使用wrong
作为布尔值,您将用它来决定是否需要增加incorrect
以下是解决方案:
#include <stdio.h>
#include <string.h>
#define SIZE 50
/*prototype definitions*/
int compareString(char *, char *);
int main(void){
char word[SIZE];
char input[SIZE];
char guess[SIZE];
int count = 0;
int wrong = 1;
int incorrect = 0;
int right = 0;
int len = 0;
printf("Please enter the word you would like to have to guess.\nThen hand your computer over to the person you would like to have play:");
fgets(word, SIZE, stdin);
len = strlen(word);
printf("Please guess one letter for the %d letter word!\n", len - 1);
do{
fgets(input, SIZE, stdin);
wrong = 1;
for(count = 0; count < len - 1; count++){
if(input[0] == word[count]){
printf("that letter is in the %d spot\n", count + 1);
wrong = 0;
++right;
}
/*I know the problem lies here but i'm not sure how to fix it I've tried not using len-1 and just using len, I've tried not resetting the amount wrong. Everything!*/
}
if(wrong) {
incorrect++;
}
}while(incorrect < 6 && right < len - 1);
return 0;
}