Homework Help on "C"

Discussion in 'Programmer's Corner' started by DanTheMan, Jul 26, 2009.

  1. DanTheMan

    Thread Starter Member

    Nov 16, 2008
    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)

    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;
    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 );
    if (guessNum==randNum)
    printf("\n\nCongratulations! You did it!\n");

    else if (guessNum > randNum)
    printf("Your guess is too high. Try again:");
    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!

  2. solpic


    Jul 23, 2009
    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.
  3. DanTheMan

    Thread Starter Member

    Nov 16, 2008
    Thank you very much! I'll try it on another machine.
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    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:

    Code ( (Unknown Language)):
    2. int main(void)
    3. {
    4.   int randResult;
    6.   start();
    7.   randResult = getRandNum();
    8.   results();
    9. }
    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.
  5. DanTheMan

    Thread Starter Member

    Nov 16, 2008
    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...
  6. someonesdad

    Senior Member

    Jul 7, 2009
    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).