2016-10-11 67 views
0

我目前是学习代码的学生。我们的任务要求我们做一个猜数字游戏。教员给了一个大纲来填补这个项目的帮助。在基本控制台应用程序中循环和托管的问题

当运行这个程序时,它只接受一个猜测,打印出答案是“@”四次,并且它为所选择的游戏量做了这个。

我真的无法弄清楚我做错了什么。我把源代码放在下面。任何帮助表示赞赏。

对于带有文件letterList.txt的fopen,我在同一个目录中有一个文本文档,每个文本都在不同的行上。

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h> 
#include <ctype.h> 
#define MAXGUESSES 5 



//this function provides instructions to the user on how to play the game 
void LetterGuessRules(); 

//this function runs one game. 
//input: character from the file, void return type 
//all other functions to Play one round of a game 
//are called from within the GuessTheLetter function 
void GuessTheLetter(char); 

//this function prompts the player to make a guess and returns that guess 
//this function is called from inside the GuessTheLetter() function described above 
char GetTheGuess(); 

//this function takes two arguments, the guess from the player 
//and the solution letter from the file. 
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match 
//This function also lets the user know if the guess comes alphabetically before or after the answer 
int CompareLetters(char, char); 

int main() 
{ 
//declare additional variables 
//declare FILE pointer 
FILE *inPtr; 
int numGames = 0, i = 0; 
char letter;//letter from file 
//display game rules 
LetterGuessRules(); 

//Ask and get number of games to play 
printf("\nHow many games would you like to play?\n"); 
scanf("%d", & numGames); 
//connect to the file HINT: use fopen 
inPtr = fopen("letterList.txt", "r"); 

      //this for loop will allow the player to play more than one game 
      //without recompiling 
for (i = 0; i < numGames; i++) 
{ 
    //get a solution letter from file - use fscanf 
    fscanf(inPtr, " %c", &letter); 

    //change the solution to lowercase 
    letter = tolower(letter); 

    //call the GuessTheLetter function and pass it the solution 
    GuessTheLetter(inPtr); 


} 

//close file pointer 
return 0; 
} 

//this function runs one game. 
//input: character from the file, void return type 
//all other functions to Play one round of a game 
//are called from within the GuessTheLetter function 
//this function lets the user know if they have won or lost the game 
void GuessTheLetter(char solution) 
{ 
int win = 0; 
int numGuesses = 0; 
char playerguess = 0; 
//declare additional variables 

while (numGuesses < MAXGUESSES && win == 0) 
{ 
    //get a guess from the user by calling the GetTheGuess function 
    GetTheGuess("playerguess"); 
    //change the guess to lowercase 
    playerguess = tolower; 
    printf(" %c", playerguess); 
    //win = call the function to compare the guess with the solution 
    win = CompareLetters(solution, playerguess); 
    numGuesses++;//count the number of guesses so far 
} 
//use conditions to let the user know if they won or lost the round of the game 
if (win == 0) 
{ 
    printf("\nSorry, you lost that round."); 
} 
else 
{ 
    printf("Congratulations, you won that round"); 
} 
} 

//this function provides instructions to the user on how to play the game 
void LetterGuessRules() 
{ 
printf("Guess a letter by typing it in then pressing enter.\nThe Game will tell you where your guessed letter is in the alphabet\ncompared to the location of the correct letter."); 
} 

//this function prompts the player to make a guess and returns that guess 
//this function is called from inside the GuessTheLetter() function described above 
char GetTheGuess(char guess) 
{ 
printf("\nWhat is your guess?\n"); 
scanf(" %lf", &guess); 
return guess; 
} 

//this function takes two arguments, the guess from the player 
//and the solution letter from the file. 
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match 
//This function also lets the user know if the guess comes alphabetically before or after the answer 
int CompareLetters(char solution, char guess) 
{ 
if (solution == guess) 
{ 
    return 1; 
} 
else 
{ 
    return 0; 
} 
} 
+0

多少个字符是有文件'中letterList .txt'?它是否至少会有'numGames'字符? –

+0

另外,请不要*用新行开始*输出,而用*结束*。这是因为写入'stdout'的输出(这是'printf'写入的内容)是默认行缓冲的。这意味着换行符将刷新缓冲区并实际写入控制台。如果你有一个领先的换行符,它将打印*前一行*,而不是当前行。 –

+0

'GetTheGuess()'返回一个值。 'playerguess'看起来像是为了保持价值。您在'GuessTheLetter'中对'tolower'的呼叫与所有其他呼叫者看起来有很大不同...... – John3136

回答

1

你的问题是这样的:

//get a guess from the user by calling the GetTheGuess function 
GetTheGuess("playerguess"); 
//change the guess to lowercase 
playerguess = tolower; 

首先在GetTheGuess函数调用的不匹配,你有原型声明。它也不符合实际的功能实现。而你其实并没有得到的猜测。您也不要致电tolower,您将函数的指针分配给变量playerguess

要纠正它,它应该看起来像

//get a guess from the user by calling the GetTheGuess function 
playerguess = GetTheGuess(playerguess); // Note: Not passing a string, and use the returned value 
//change the guess to lowercase 
playerguess = tolower(playerguess); // Note: actually call the tolower function 

当然,你需要改变原型声明,以符合实际的定义:

char GetTheGuess(char guess); 
+0

在调用 –

+0

@ M.M时甚至没有原型声明在'main'函数之上有一个'GetTheGuess();'。 –

+1

这是一个非原型声明,因为它有空的参数列表。因此,调用'GetTheGuess(“playerguess”);' –

相关问题