Thjis is a programming question for "c"

Discussion in 'Homework Help' started by DanTheMan, Jul 26, 2009.

Thread Status:
Not open for further replies.
  1. DanTheMan

    Thread Starter Member

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

    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. Dave

    Retired Moderator

    Nov 17, 2003
Thread Status:
Not open for further replies.