C program won't run correctly; new to programming

June 17, 2013 at 09:05:55
Specs: Firefox
I'm having a hard time finding the errors in my program. I thought I set it up completely, but it won't run. The point of the program is to create a game that prompts the user to guess a letter, and then depending on that letter the program will announce that the user's guess is either before, after, or equal to the solution. I'm not sure what makes the solution, so I picked q, however we had to compile a list of random unsorted letters in the alphabet. How would I logically be able to guide my user to pick a letter that comes before 'q' even if it doesn't in my list?

Below is my code. What's wrong with it?
#include <stdio.h>
#include <ctype.h>

#define MAX_GUESSES 5

/*to play the guessing game, first select how many games (from 1 to 4)
that you'd like to play; in each game the program will ask you to guess
(maximum of 5 guesses) a letter (in lowercase); after you've entered an
arbitrary letter, the program will ask you to guess another letter until
you've run out of your max guesses (5); when the game is over, the
program will end. Enjoy!*/
void Instructions();

int main()
{
//declare variables
int numOfGames;
int t;

char letter;

//declare pointer, pointer's location, and using the file
FILE *infileptr;
infileptr = fopen("inputLet.txt", "r");

//provides the user with instructions
printf("How to play the game:\nSelect the number of games you'd like to play (at a max of 4 games).\nYou will be asked to guess a lowercase letter (at a max of 5 guesses).\nWhen the max number of guesses has been reached, or the letter has been guessed correctly the game will end.\nEnjoy the game!\n");

//prompts the user for the number of games desired to play
printf("\nHow many games would you like to play (max 5)? \n");
scanf("%d", &numOfGames);

//play out the number of games of lesser or equal value to the max number of games
for(t=1; t<=numOfGames; t++)
{
void Instructions();
{
//displays the number of games desired by the user
printf("\nYou've chosen to play %d game(s)\n\n", numOfGames);
}
//runs the games with the user
printf("Let's play game %d\n", t);
printf("Guess a letter: %c", letter);
scanf(" %c", &letter);
fscanf(infileptr, " %c", &letter);
}
//gives the command to close the file
fclose(infileptr);

/*this function checks for the max correct/incorrect guesses
for one game. If the players guesses correctly, a 1 is returned,
if incorrect a 0 is returned (or if game ends)*/
int PlayGuess(char solution);
{
int numOfGuesses;
int winOrLose;
numOfGuesses = 0;
winOrLose = 0;
numOfGuesses++;

while(numOfGuesses++<MAX_GUESSES && winOrLose==0)
{
//prompts the user to guess a letter
char GetLetter();
{
char guess;
guess = tolower(guess);
printf("Guess a letter: \n", guess);
scanf(" %c", &guess);
return guess;
}

//compares the user's guess and the solution in the file, and will notify if user's guess matches or not
int CompareLetters(char guess, char solution);
{
int q;
int guess;
int solution;
solution = q;

if(guess<solution)
{
printf("The letter you are trying to guess is before: %c\n", letter);
printf("Your guess is: %c\n");
scanf(" %c", &guess);
return 0;
}
else if(guess>solution)
{
printf("The letter you are trying to guess is after: \n", letter);
printf("Your guess is: %c\n");
scanf(" %c", &guess);
return 0;
}
else if(guess==solution)
{
printf("You guessed correctly!");
return 1;
}
}
}
}
return 0;
}


See More: C program wont run correctly; new to programming

Report •

#1
June 18, 2013 at 07:34:29
cc1plus: warnings being treated as errors
In function 'int main()':
Line 64: warning: too many arguments for format
Line 79: warning: too few arguments for format
Line 80: warning: format '%c' expects type 'char*', but argument 2 has type 'int*'
Line 85: warning: too many arguments for format
Line 86: warning: too few arguments for format
Line 87: warning: format '%c' expects type 'char*', but argument 2 has type 'int*'

:: mike


Report •

#2
June 18, 2013 at 10:21:18
Also, you have a tendency to use uninitialized variables. I assume solution = q; should be
solution = 'q';

How To Ask Questions The Smart Way


Report •

#3
June 18, 2013 at 21:14:17
I am about clueless with C, but:
"I'm not sure what makes the solution, so I picked q, however we had to compile a list of random unsorted letters in the alphabet. How would I logically be able to guide my user to pick a letter that comes before 'q' even if it doesn't in my list?"

This makes me think you might have 'slipped a cog' interpreting the assignment (we all know it's homework, Lol!). I might be totally wrong, but here's my interp:

For your solution, you don't need a collection of 26 randomly sorted alphas, you only need one random selection from the set of 26 alphas. This is your "solution".

The user's entry is tested against a set of 26 alphabetically-ordered items (a-z). If the solution scans zero, they win, if negative, it is too low, if positive, it is too high (in the alphabet).

Since I suck at C, I sometimes use another language to get the logic right, then reverse-engineer/convert. (is that "cheating"?)

Also, I see no reference anywhere in your code to "random". For a game like this, I would assume it would crop up somewhere in your initial code (generating the random "solution" value). Or maybe I missed that, myself being "C-challenged".



Report •

Related Solutions

#4
June 21, 2013 at 13:34:49
Thank you all! I do think I'm not comprehending what exactly it is that my professor wants. I'll give your suggestions and try and see if it works

Report •

Ask Question