Just check get_guess functioon is not working correctly and how do I read EOf to
ID: 3554244 • Letter: J
Question
Just check get_guess functioon is not working correctly and how do I read EOf to input to my array
#include <stdio.h>
#include <ctype.h>
#define MAXDIM 10
#define MAXCHAR 'F'
static int seed;
int rand();
void srand(int);
void initialize(char model[], int dimensions, char maxchar);
int match(char model[], char guess[], int dimensions);
int non_blank();
int get_guess(char guess[], int dimensions, char maxchar, int try);
/*This will return a different random nonnegative integer on
// each successive call.*/
int rand() {
seed = (seed * 131071 + 524287) % 8191;
return seed;
}
/* Call this just once in the entire program, with the seed.*/
void srand(int s) {
seed = s;
}
/* Initialize "model" to contain a random pattern of "dimensions"
* letters, in the range 'A' to "maxchar".
*/
void initialize(char model[], int dimensions, char maxchar)
{
int counter = 0;
int random = 0;
while(dimensions--) {
random = rand() % (maxchar - 64);
model[counter++] = random + 65;
}
}
/* doCompare "model" to "guess"i, assuming each contains "dimensions"
* characters. Print out the number of exact and inexact matches.
* Returns the number of exact matches.
*/
int match(char model[], char guess[], int dimensions)
{
int exact = 0, count = dimensions, tmp;
int i = 0;
int j = 0;
while(count-- ) {
if(model[j] == 0) {
printf(" Exact 4 and 0 Inexact ");
return exact = 4;
}
if(model[count] == guess[count]) {
exact++;
tmp = dimensions;
while(count >= tmp--) {
i = tmp--;
model[tmp] = model[i ];
guess[tmp] = guess[i];
}
}
}
printf("Exact: %d and %d Inexatc ), exact, dimensions - exact");
return exact;
}
/* Return the next non_blank character from the input. Return EOF if
* you hit EOF while trying to find the next nonblank character
*/
int non_blank(void)
{
int tmp = getchar();
while(tmp == 32) {
tmp = getchar();
}
return tmp;
}
/* Input the user's guess, putting it into "guess". Assume that
* there will be "dimensions" characters in the guess, in the range
* 'A' to "maxchar". Assume that this is the "try"th attempt on the
* user's part. If a guess with the wrong format or characters is
* entered, make the user try again until a valid guess is entered.
* After each guess, read and discard any extra characters up to and
* including the end of line. Return true if a guess is obtained,
* false if EOF is hit while trying to get a good guess.
*/
int get_guess(char guess[], int dimensions, char maxchar, int try)
{
int count = 0, tmp = 0, flag =1,dim = dimensions + 1, exact = 0 ;
try++;
printf("%d. Enter your guess:", try);
while(dim-- ) {
guess[count++] = non_blank();
}
int i = 0;
for(; i <= dimensions; i++) {
printf("%d ", guess[i]);
}
do {
if(guess[count] == EOF) {
try = 0;
}
if(guess[count] > maxchar || guess[count] < 'A') {
printf(" Bad entry. Try again:");
dimensions = dim+1;
count = 0;
while(dim --)
guess[count++];
count++;
}
} while(count--);
return try;
}
int main() {
int dimensions, seed;
char maxchar;
char model[100], again, guess[100];
int count = 0;
int read;
double avg;
int try = 0;
int game = 0;
printf("Enter maxchar, dimensions, and seed =>");
read = scanf("%c %d %d", &maxchar, &dimensions, &seed);
maxchar = toupper(maxchar);
if(read < 3)
printf("Bad initial values ");
else if(dimensions == 0 || dimensions > MAXDIM || maxchar > MAXCHAR)
printf("Bad initial values ");
else {
srand(seed);
initialize(model, dimensions, maxchar);
do {
game++;
try = get_guess(guess, dimensions,maxchar, try);
printf("Another game [Y/N]? ");
scanf("%c", &again);
again = toupper(again);
} while(again == 'N');
}
return 0;
}
Explanation / Answer
Did't undestand what you want exactly.
If your question is only about EOF, to identify the end of file character use '' instead of EOF.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.