Thread: help with small fortune program

  1. #1
    Registered User
    Join Date
    Sep 2007

    help with small fortune program

    hi all, i needed to write a small fortune program for class using random numbers. the only thing is that the only random number im getting is 2. thanks for the help!
    #include <stdlib.h>
    #include <stdio.h>
    int main()
        int randomNum = 0;
        char answer;
        printf ("Do you want to see your fortune?");
        scanf ("%c", &answer);
        if (answer == 'y' || answer == 'Y') {
              randomNum = rand() % 4 + 1;
               if (randomNum == 1 ) {
                   printf ("You will learn something today.\n\n");
               else if (randomNum == 2 ) {
                   printf ("You will enjoy a long and happy life.\n\n");
               else if (randomNum == 3 ) {
                  printf ("Opportunity knocks softly.  Can you hear it?!\n\n");
               else  {
                 printf ("You will be financially rewarded for your hard work.\n\n");
        else {
             printf ("You will be forever unlucky.\n\n");
           system ("PAUSE");
           return 0;

  2. #2
    Registered User
    Join Date
    Oct 2006
    check out the FAQ for random numbers, i think your missing one crucial thing.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    try seeding the random number generator. By default the random number generator is "seeded" with a constant value - this is good for debug purposes, because it means that you get the same sequence of random numbers each time you run the code. Unfortunately, it also means that simple programs that grab one or a few random number(s) will always get the same result each time.

    #include <time.h>
    Add the above code to your application, and it will grab the "current time" as a integer to seed the random number generator. Since time is different every time you run the code, it will make the random number be different each time too.

    I would prefer to see a switch-statement rather than multiple if-statements to select the message based on the random number.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2007
    ty ty for the help. i should have looked around to find that FAQ section, pretty much standard in forums to search for it i should have known better. thanks again, im sure ill have more stuff up here i'll need help with seeing as im about as novice as you can get to C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help with small program
    By cjohnman in forum C Programming
    Replies: 11
    Last Post: 04-14-2008, 09:59 AM
  2. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 10:52 AM
  3. Help writing small program
    By guitarhead2000 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2004, 12:42 PM
  4. A little Help with a small program
    By whtpirate in forum C Programming
    Replies: 7
    Last Post: 06-05-2003, 06:15 PM
  5. Very small program...Whats wrong???
    By SmokingMonkey in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2003, 09:09 PM