关于清理缓存的问题
写了一个关于hangman 的作业,但是遇到了一些缓存问题,在选择完难度后第一个input无论输入什么都会是not a letter。求指教如何解决
#include <stdio.h>
#include<string.h>
#include"words.h"
#define MAX 100
void hangman(int missCounter,char difficulty)
{
int deathlevel;
if(difficulty=='h'){
deathlevel=missCounter+4;
}
else{
deathlevel=missCounter;
}
switch(deathlevel)
{
case 1:
printf("\n\n\n\n\n\n\n");
printf("=======\n");
break;
case 2:
printf("|\n|\n|\n|\n|\n|\n");
printf("=======\n");
break;
case 3:
printf("_____");
printf("\n|\n|\n|\n|\n|\n|\n");
printf("=======\n");
break;
case 4:
printf("_____");
printf("\n| |\n|\n|\n|\n|\n|\n");
printf("=======\n");
break;
case 5:
printf("_____");
printf("\n| |\n| O\n|\n|\n|\n|\n");
printf("=======\n");
break;
case 6:
printf("_____");
printf("\n| |\n| O\n| /\n|\n|\n|\n=======\n");
break;
case 7:
printf("_____");
printf("\n| |\n| O\n| / \\\n|\n|\n|\n=======\n");
break;
case 8:
printf("_____");
printf("\n| |\n| O\n| /|\\\n| |\n|\n|\n=======\n");
break;
case 9:
printf("_____");
printf("\n| |\n| O\n| /|\\\n| |\n| /\n|\n=======\n");
break;
case 10:
printf("_____");
printf("\n| |\n| O\n| /|\\\n| |\n| / \\\n|\n=======\n");
break;
}
}
void arrangeWord(char missword[]){
int i;
int j;
int inverse;
int n= strlen(missword);
for(i = 0;i <n-1;i++){
for(j =n- 1;j > i;j--){
if(missword[j-1] > missword[j]){
inverse = missword[j-1];
missword[j-1] = missword[j];
missword[j] =inverse;
}
}
}
}
void displayRound(int round, char *word,char *guesses,char difficulty,int missCounter){
char printword[MAX]={'\0'};
char missword[MAX]={'\0'};
int n=0,n1=0,n2=0,n3=0;
printf("\nRound %d\n",round);
hangman(missCounter,difficulty);
for(n=0;word[n]!='\0';n++){
printword[n]='-';
}
printword[n]='\0';
for(n=0;guesses[n]!='\0';n++){
for(n1=0;word[n1]!='\0';n1++){
if(guesses[n]==word[n1]){
printword[n1]=word[n1];
printword[n1]=printword[n1]+'A'-'a';
}
}
}
n=0;
printf("Word: %s\n",printword);
for(n=0;guesses[n]!='\0';n++){
for(n1=0;word[n1]!='\0';n1++){
if(guesses[n]==word[n1]){
n2++;
break;
}
}
if(n2==0){
missword[n3]=guesses[n];
n3++;
}
else
n2=0;
}
missword[n3]='\0';
arrangeWord(missword);
printf("Misses: %s",missword);
}
void getGuess(char *guesses){
char inputletter='a';
int n=0;
int guesschecker=0;
while(n<n+1){
n=0;
guesschecker=0;
printf("\nGuess: ");
scanf("%c",&inputletter);
while('\n'!=getchar()){
}
printf("1111%c222",inputletter);
if(inputletter>='A'&&inputletter<='Z')
inputletter=inputletter-'A'+'a';
for(n=0;guesses[n]!='\0';n++){
if(inputletter==guesses[n]){
guesschecker=1;
break;
}
}
if(guesschecker==1){
printf(" You already guessed '%c'! Try again.\n",inputletter);
continue;
}
if((inputletter>='a'&&inputletter<='z')||inputletter=='\'')
;
else{
printf("1111%c222",inputletter);
printf(" Not a letter! Try again.");
continue;
}
break;
}
guesses[n]=inputletter;
guesses[n+1]='\0';
}
int numCorrectLetters(char word[MAX], char guesses[MAX]){
int n=0,n1=0;
int n2=0;
for(n=0;guesses[n]!='\0';n++){
for(n1=0;word[n1]!='\0';n1++){
if(guesses[n]==word[n1])
n2++;
}
}
return n2;
}
int numMissedLetters(char *word, char *guesses){
int n=0,n1=0;
int n2=0;
int n3=0,n4=0,n5=0;
for(n=0;guesses[n]!='\0';n++){
n4=n2;
for(n1=0;word[n1]!='\0';n1++){
if(guesses[n]==word[n1])
n2++;
}
if(n2==n4)
n5++;
}
return n5;
}
char selectlevel(){
int c=0;
char difficulty;
while(c<c+1){
printf("Select the difficulty level (e)asy or (h)ard:");
scanf("%c",&difficulty);
while('\n'!=getchar()){
}
if(difficulty=='H'||difficulty=='E')
difficulty=difficulty-'A'+'a';
if(difficulty=='h'||difficulty=='e')
break;
else
printf("Invalid input! Try again.\n");
}
return difficulty;
}
void main(){
char playagain;
int wincon=0,losecon=0;
do{
char *w;
int allcount,misNumber;
int n=0,correctCounter=0,missCounter=0;
int round=0,changecount=0;
char word[MAX]={'\0'};
char guesses[MAX]="\0";
char difficulty;
int c=0;
difficulty=selectlevel();
if(difficulty=='h')
misNumber=6;
else
misNumber=10;
initWords();
w=getRandomWord();
for(n=0;w[n]!='\0';n++)
word[n]=w[n];
allcount=n;
word[n]='\0';
for(n=0;n<n+1;n++){
displayRound(round,word,guesses,difficulty,missCounter);
round++;
if(missCounter==misNumber){
for(changecount=0;w[changecount]!='\0';changecount++){
if(w[changecount]>='a'&&w[changecount]<='z')
w[changecount]=w[changecount]+'A'-'a';
}
losecon++;
printf("\nYou LOSE! The word was %s.",w);
break;
}
if(correctCounter==allcount){
printf("\nYou WIN! Congratulations!");
wincon++;
break;
}
getGuess(guesses);
correctCounter=numCorrectLetters(word,guesses);
missCounter=numMissedLetters(word,guesses);
}
printf("Your WIN/LOSS record is %d/%d",wincon,losecon);
while(c<c+1){
printf("Do you want to play again? (y/n) ");
scanf("%c",&playagain);
while('\n'!=getchar()){
}
if(playagain=='Y'||playagain=='N')
playagain=playagain-'A'+'a';
if(playagain=='y'||playagain=='n')
break;
else
printf("Invalid input! Try again.\n");
}
}while(playagain!='n');
printf("Bye Bye!");
}