Homework Help on "C"

Thread Starter

DanTheMan

Joined Nov 16, 2008
10
Not sure if I should post this here or in HomeWork Help, but I figured you guys would know the answer for sure. Sorry for such a basic question, but this is my first programming class.

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
 

solpic

Joined Jul 23, 2009
25
If you are carrying around your compiler instead of installing it on a machine, like putting it on a USB and moving it from machine to machine, this often happens. If it is that same situation, there is no solution.
 

Mark44

Joined Nov 26, 2007
628
Solpic is steering you wrong. The problem is your code. In the main function, you have calls to start(), getRandNum(), and results(). You are calling getRandNum() as if it were a void function (i.e., a function that doesn't return a result). getRandNum() dutifully calculates a random number, but when it returns this value, you aren't storing into any variable.

You need something like this:

Rich (BB code):
int main(void)
{
  int randResult;

  start();
  randResult = getRandNum();
  results();
}
Any time you have a function that returns a value, you have to call the function as I did getRandNum. Your results function returns a value that doesn't seem useful: it returns 0 in all cases. You might as well declare it as a void function and call it as I did in the code above.

The other problem is your results() function. Instead of having randNum in this function always be 7 as your code does, define results() to have a parameter of type int, and pass the value returned by getRandNum() to results(), and get rid of the local variable randNum, since you won't need it.

Those are the most obvious shortcomings in your code. There might be some others, but my findings should get you going.
 

Thread Starter

DanTheMan

Joined Nov 16, 2008
10
YES! It worked!!! Thank you SO much for your help! Your explanation makes a lot of sense.
I spent a lot of time trying to figure out why it wasn't running correctly, but I was missing the point. I can finally sleep in peace tonight...
 

someonesdad

Joined Jul 7, 2009
1,583
A couple of suggestions:

Post your code with {code} before it and {/code} after it (replace { by [ and } by ]) to make it easier to read. The default formatting strips off all indentation.

Put main at the bottom of the file. Put other functions' code above where they first get called. Then you can eliminate your function declarations at the beginning of the file. Why? Less code to maintain -- later, when you change a function's signature, you only have to change it in one place, not two. You'll find you very rarely need function declarations (except for include files so you can call an external function).
 
Top