gpt4 book ai didi

c - 基本控制台应用程序中的循环和 tolower 问题

转载 作者:行者123 更新时间:2023-11-30 15:05:11 25 4
gpt4 key购买 nike

我目前是一名学习编码的学生。我们的作业要求我们做一个猜数字的游戏。讲师给出了一份需要填写的大纲,以帮助我们完成该项目。

运行此程序时,它只接受一次猜测,并打印四次答案为“@”,并且针对所选游戏的数量执行此操作。

老实说,我无法弄清楚我做错了什么。我把源代码放在下面。如有任何帮助,我们将不胜感激。

对于带有文件 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;
}
}

最佳答案

您遇到的问题是这样的:

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

首先,GetTheGuess 函数调用与您的原型(prototype)声明不匹配。它也与实际的功能实现不匹配。而您实际上并没有得到猜测。您也不调用 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

您当然需要更改原型(prototype)声明以匹配实际定义:

char GetTheGuess(char guess);

关于c - 基本控制台应用程序中的循环和 tolower 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39968998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com