//preprocessor directives #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #d
ID: 3620651 • Letter: #
Question
//preprocessor directives
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#define MAXGUESSES 10
void Instructions();
int Play(int answer);
int GetGuess();
int CompareGuess(int guess, int answer);
int main()
{
int loopCounter, //used to count the number of times the while loop runs
maxNums, //stores the number of times the user wants to play
ans;//stores the current integer read from the file
char fname=' ', //stores the first inital of the user
lname = ' ';//stores the lastname initial of the user
//declare a pointer to a file
FILE *infileptr;
//call instructions function
Instructions();
//prompt and scan in user initials
printf("Enter your First Initial: ");
scanf(" %c", &fname);
printf("Enter your lastname initial: ");
scanf(" %c", &lname);
//connect to the file
infileptr = fopen ("answers.txt","r");
//INITIALIZE the loop counter
loopCounter=0;
printf("How many games do you want to play?");
scanf("%d", &maxNums);
while(loopCounter< maxNums)//TEST loop condition
{
fscanf(infileptr, "%d", &ans);
//printf("num is: %d ", ans);
Play(ans);
if(Play(ans)==1)
{
printf("************Excellent - you win!************* ");
}
loopCounter++;//UPDATE
}
fclose(infileptr);
//close the file pointer
return 0;
}
int GetGuess()
{
int guess;
printf("Enter guess between 0 and 100: ");
scanf("%d", &guess);
return guess;
}
int CompareGuess(int guess, int answer)
{
if(guess<answer)
{
printf("too low ");
return 0;
}
if(guess>answer)
{
printf("too high ");
return 0;
}
else
{
return 1;
}
}
//this function runs one game. It repeats for up to the number of games the user wants to play
//It stops running the game when the user has guessed the
//number or has run out of guesses
int Play(int answer)
{
//variable declarations
int hasWon;
int guessCount = 0;
while (guessCount < MAXGUESSES)
{
hasWon = CompareGuess(GetGuess(), answer);
if(hasWon == 1)
{
return 1;
}
else
{
guessCount=guessCount+1;//loser guessed wrong update guessCount
}
}
printf("You are out of your guesses, the correct answer was:%d ", answer);
return 0;//return a 0 here after the loop ran MAXGUESSES, the player loses
}
void Instructions()
{
printf("Welcome to Number Guess To begin, you will need to enter your initials You will then enter the number of games to play You have 10 chances to guess the number Let's begin: ");
}
I really need help, thank you
Explanation / Answer
please rate - thanks you had an extra call to play. when you read a character you leave the enter in the buffer, so when you read the next character the enter is read. you must flush the buffer of any enters//preprocessor directives
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#define MAXGUESSES 10
void Instructions();
int Play(int answer);
int GetGuess();
int CompareGuess(int guess, int answer);
int main()
{
int loopCounter, //used to count the number of times the while loop runs
maxNums, //stores the number of times the user wants to play
ans;//stores the current integer read from the file
char fname=' ', //stores the first inital of the user
lname = ' ';//stores the lastname initial of the user
//declare a pointer to a file
FILE *infileptr;
//call instructions function
Instructions();
//prompt and scan in user initials
printf("Enter your First Initial: ");
scanf("%c", &fname);
while ( getchar() != ' ');
printf("Enter your lastname initial: ");
scanf("%c", &lname);
while ( getchar() != ' ');
//connect to the file
infileptr = fopen ("answers.txt","r");
//INITIALIZE the loop counter
loopCounter=0;
printf("How many games do you want to play?");
scanf("%d", &maxNums);
while(loopCounter< maxNums)//TEST loop condition
{
fscanf(infileptr, "%d", &ans);
//printf("num is: %d ", ans);
//Play(ans); if(Play(ans)==1)
{
printf("************Excellent - you win!************* ");
}
loopCounter++;//UPDATE
}
fclose(infileptr);
//close the file pointer
return 0;
}
int GetGuess()
{
int guess;
printf("Enter guess between 0 and 100: ");
scanf("%d", &guess);
return guess;
}
int CompareGuess(int guess, int answer)
{
if(guess<answer)
{
printf("too low ");
return 0;
}
if(guess>answer)
{
printf("too high ");
return 0;
}
else
{
return 1;
}
}
//this function runs one game. It repeats for up to the number of games the user wants to play
//It stops running the game when the user has guessed the
//number or has run out of guesses
int Play(int answer)
{
//variable declarations
int hasWon;
int guessCount = 0;
while (guessCount < MAXGUESSES)
{
hasWon = CompareGuess(GetGuess(), answer);
if(hasWon == 1)
{
return 1;
}
else
{
guessCount=guessCount+1;//loser guessed wrong update guessCount
}
}
printf("You are out of your guesses, the correct answer was:%d ", answer);
return 0;//return a 0 here after the loop ran MAXGUESSES, the player loses
}
void Instructions()
{
printf("Welcome to Number Guess To begin, you will need to enter your initials ");
printf("You will then enter the number of games to play You have %d");
printf("chances to guess the number Let's begin: ");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.