Well, for some reason the program sticks to only one function at a time. I can't get it to run through the whole code, right now it runs to int CONT (void); , then loops back to the scanf(), and ends when the number is guessed.
Before I included the redundant IF statement before int CONT (void), the program skipped it and went to int GES (void) and then looped directly to scanf(), exiting the entire program if the number is guessed. So I can get one or the other to work, but not both. Shouldn't the program move on down the line after one function is executed?
Code:
/*============================================================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
int z;
int v = 0;
int r = 0;
char go = 'y';
/*============================================================================================================*/
int main (void)
{
/*================================================*/
/*Function 1- Display header.*/
print_head(); /*Call header function from game.h*/
/*================================================*/
/*Function 2- Generate number and read guess.*/
while(go == 'y' || go == 'Y')
{ /*Start the game.*/
z = 1 + rand() %1000; /*Generate random number between 1 and 1000.*/
/*Read guess from the user and assign to v.*/
printf("I have a number between 1 and 1000.\n Can you guess my number?\n Please type your first guess:");
go = 't'; /*Force start block to end loop.*/
} /*Close start of game.*/
while(go == 't')
{ /*Open the game's main body.*/
scanf("%i", &v); /*Read the user's guess and store at v.*/
/*================================================*/
/*Function 3- Call CONT from game.h*/
if(z != v || z == v)
{
int CONT(void);
}
/*================================================*/
/*Function 4- Call GES from game.h IF the guess is incorrect.*/
if(v != z)
{
int GES(void);
}
/*================================================*/
/*Function 5- Ask the user to restart the game or end the program.*/
if(v == z) /*Open restart block.*/
{
printf("Would you like to play again?(y/n):");
scanf("%c", &go);
if(go == 'y' || go == 'Y') /*Use input "Yes" to re-enter the start of the game.*/
{
v = 0;
r = 0;
}
} /*Close restart block.*/
/*================================================*/
} /*Close the game's main body.*/
/*================================================*/
return 0;
} /*Close the main function.*/
/*============================================================================================================*/
Definitions:
Code:
#include <stdio.h>
#include "game.h"
/*================================================================================================*/
/*Define function print_head.
print_head- Display header.
*/
void print_head(void)
{
printf("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
printf("\nProgram by: ");
printf(NAME);
printf("\nDate: ");
printf(DATE);
printf("\n");
printf(TITLE);
printf("\n");
printf("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
printf("\n");
return;
}
/*================================================================================================*/
/*
Define function CONT.
CONT- Gather the number of guesses and evaluate the user.
*/
int CONT(void)
{
++r; /*Count the number of guesses and store the tally at "r"*/
if(v == z) /*Once the user has guessed the correct number, begin evaluation.*/
{
printf("Excellent! You guessed the number!"); /*Inform user of success.*/
printf("\nIt took you %i tries to guess correctly.\n", r); /*Display counted attempts.*/
if(r > 10)
{
printf("You should be able to do better!\n");
}
if(r == 10)
{
printf("Aha! You know the secret!\n");
}
if(r < 10)
{
printf("Either you know the secret or you got lucky!\n");
}
} /*Close the evaluation.*/
return 0;
}
/*================================================================================================*/
/*Define function GES.
GES- Run the user's input against the random number generated.
*/
int GES(void)
{
if(v > z)
{
printf("Too high, try again.\n");
}
if(v < z)
{
printf("Too low, try again.\n");
}
return 0;
}