No problem cablu I am sure grateful you took the time to look at my code. Every input is helpful.
I decided to understand how the menu works that it would be good to try it out in a simple guessing game. In this game the computer picks a random number in a given range and allow a certain number of guesses for the user to find the answer.
Here the menu allows the user to input the desired difficulty and then run the game function. I know the input function is not right yet but I am not far enough along in my studies to make bullet proof input yet. I want to understand each function before going to much further.
I purposely broke it down in as many functions as I could to get practice in setting up and calling functions.
I have compile and run it and it seems to work as I would expect. Here is the Menu in the game.
It's amazing how small the main function is.
Code:
/* A guessing game from James Tuvell
In this game the computer picks a random number in
a certain range and the user will have a specified number
of tries to find that number
*/
#include <stdio.h>
#include <time.h>
int Menu (void);
void Instructions (int lownumb, int highnumb, int guesses );
int MenuInput (void);
int GameInput (void);
int Randnumb ( int lownumb, int highnumb );
void Game ( int lownumb, int highnumb, int guesses );
int main (void){
// declare and initialize variables
int answer;
// main program loop
do {
// jump to menu
answer = Menu ();
/* On the menu selection jump to the Game function
with the apropriate settings for that game
difficulty.
*/
switch (answer){
case '1' : Game ( 1, 20, 5); break;
case '2' : Game ( 1, 50, 10); break;
case '3' : Game ( 1, 99, 20); break;
}
}
while ( answer != 'q' );
return 0;
}
/* This is the menu function it only prints
* the menu. It returns no value
*/
int Menu (void){
// menu function
int answer;
int numb;
printf ( "\n\n" );
printf ( "Chose you level of dificulty \n");
printf ( "1 - Easy between 1 and 20 with 5 guesses\n");
printf ( "2 - Medium between 1 and 50 with 10 guesses\n");
printf ( "3 - Hard between 1 to 99 with 20 guesses\n");
printf ( "q -to quit\n\n");
answer = MenuInput ();
return answer;
}
/* This prints the instructions for the game
based on the choice made from the menu
*/
void Instructions ( int lownumb, int highnumb, int guesses ){
// print instruction for game
printf ( "\n" );
printf ( "I will think of a number " );
printf ( "between %d and %d\n", lownumb, highnumb );
printf ( "you have %d guesses to find it.\n\n", guesses );
}
/* This gets user input and returns the answer
* it also will clear out the stdin buffer before
* returning
*/
int MenuInput (void){
int answer;
answer = getchar ();
while ( getchar () != '\n' );
return answer;
}
/* This is the input for the game
I still need to check for errors
Have to add something to keep
letters from dumping my buffer
*/
int GameInput (void){
int answer;
scanf ( "%d", &answer );
while ( getchar () != '\n' );
return answer;
}
/* This is the random number generator that gets
a range from the user and generates a random
number in that range. It uses the time to initialize
to prevent duplicate numbers
*/
int Randnumb ( int lownumb, int highnumb ){
int randnumb;
// initialize random number generator
srand ( time (NULL) );
randnumb = ( rand () % ( highnumb-lownumb ) + lownumb) ;
return randnumb;
}
/* This function is the game function. It takes in the range
of the number to be guessed and the number of guesses
that the user is alowed.
*/
void Game ( int lownumb, int highnumb, int guesses ){
// declare variables
int numb_to_guess;
int guess;
int digit;
int i;
// print instructions
Instructions ( lownumb , highnumb, guesses );
// chose a number for user to guess
numb_to_guess = Randnumb ( lownumb, highnumb );
// Set the number of digits for user input
digit = 2;
// Main game loop
for ( i = 0 ; i < guesses ; i++) {
printf ( "Enter your guess:");
guess = GameInput ( );
if ( guess > numb_to_guess ){
printf ( "Your too high try again!\n" );
}
if ( guess < numb_to_guess ) {
printf ( "Your too low try again!\n");
}
if (guess == numb_to_guess ) {
printf ( "You Win!!!\n");
return;
}
}
printf ("You loose!!! it was %d\n",numb_to_guess);
}