Thjis is a programming question for "c"

Status
Not open for further replies.

Thread Starter

DanTheMan

Joined Nov 16, 2008
10
I posted this in the Programmer's Forum as well. Sorry for the double post.

I'm writing a program that generates a random number between one and 20. The user has 5 attempts to guess the correct number. After each guess you are to tell the user if the guess is Greater than, less than, or equal to the random number. If equal, no more guesses to be made.

I have written a source code and I can compile it successfully. For some reason the random number is only coming up as "7". Can you think of what my error may be and what the solution is? Here's my mess:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void start(void);
int getRandNum (void);
int results (void);

int main (void)
{
start();
getRandNum();
results();

return 0;
}
/* ================== start ==================
This function prints initialmessage..
*/

void start(void)
{
printf("I am thinking of a number between 1 and 20.\n");
printf("Can you guess what it is?\t\t");
return ;
}
/* ================== getRandNum ==================
This function generates a random number between 1-20.
*/
int getRandNum (void)
{
int randNum;
int randRange;
time_t t;
srand(time(&t));
randRange = (20-1)+1;
randNum=rand()% randRange + 1;
printf("randNum is %d", randNum);
return randNum;
}


/* ================== results ==================
This function prints the end message.
*/
int results (void)
{
int randNum=7;
int guessNum;
int trialCount=0;
int success=0;

while ((guessNum != randNum)&&trialCount<5)
{
scanf ("%d", &guessNum );
trialCount++;
if (guessNum==randNum)
{
printf("\n\nCongratulations! You did it!\n");
success=1;
}

else if (guessNum > randNum)
printf("Your guess is too high. Try again:");
else
printf("Your guess is too low. Try again:");
}//End of while

if (success==0)
printf("\n\nSorry. The number was %d.\nYou should have gotten it by now.\nBetter luck next time!\n",randNum);

return 0;
}

Thanks so much!

Dan
 
Status
Not open for further replies.
Top