Thread: FINISHED - lottery program

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    FINISHED - lottery program

    heyho im finished with my first complete program in C.
    heres the source code:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    #define AUTHOR "threadhead"
    #define SEED 49
    #define FILENAME "lotto.txt"
    FILE *file;
    
    int remember(int val, int *saved, int size);
    int write(int input[5]);
    int menu(void);
    int case3(void);
    void case2(void);
    void case1(void);
    int rando(void);
    
    
    /*-------------------------------------------------------*/
    
    int main()
    {
        srand(time(NULL));
        switch(menu())
        {	
    	case 1:
    	{
    	    case1();
    	    break;
    	}        
    
    	case 2:
            {
                case2();
                break;
            }
     	case 3:
    	{
    	    case3();
    	    break;
        	}
        }
        return 0;
    }
    
    /*-------------------------------------------------------*/
    
    int menu()
    {
        int choice=0;
        printf("\n\n%s wrote this program mails go to [email protected]\n", AUTHOR);
        puts("welcome to your lottery\n");
        puts("--------------------------\n\n");
        puts("1. show all your lottery numbers\n");
        puts("2. write down some of your lottery numbers\n");
        puts("3. generate some lottery numbers by random\n");
        puts("choose: ");
        scanf("%d", &choice);
        return choice;
    }    
    
    /*-------------------------------------------------------*/
    
    int rando()
    {
        return ((rand()%SEED)+1); 
    }
    
    /*-------------------------------------------------------*/
    
    void case1()
    {
        int input[5], i;
        file = fopen(FILENAME, "r");
        if (file == NULL)
            puts("an error occured");
        else
        {
            for(i=0;i<6;i++)
            {
                fscanf(file, "%d", &input[0]);
                fscanf(file, "%d", &input[1]);
                fscanf(file, "%d", &input[2]);
                fscanf(file, "%d", &input[3]);
                fscanf(file, "%d", &input[4]);
                fscanf(file, "%d", &input[5]);
            }
            fclose(file);
            printf("first number: %d\n", input[0]);
            printf("second number: %d\n", input[1]);
            printf("third number: %d\n", input[2]);
            printf("fourth number: %d\n", input[3]);
            printf("fifth number: %d\n", input[4]);
            printf("sixth number: %d\n", input[5]);
        }
    }
    
    /*-------------------------------------------------------*/
    
    void case2()
    {
        int input[5];
        puts("enter your 6 numbers: ");
        scanf("%d", &input[0]);
        scanf("%d", &input[1]);
        scanf("%d", &input[2]);
        scanf("%d", &input[3]);
        scanf("%d", &input[4]);
        scanf("%d", &input[5]);
        write(input);
    }
        
    /*-------------------------------------------------------*/
    
    int case3()
    {
        int i, a;
        int saved[6];
        i=0;
        while (i<6)
        {
            a = rando();
            if ( !remember(a, saved, 6))
            {
                saved[i++] = a;
            }
        }
        puts("your numbers are: ");
    
        for (i=0;i<6;i++)
        {
            printf("%d ", saved[i]);
        }
        printf("\nyour bonus number is: \n%d\n", rando());
        return 0;
    }
    
    /*-------------------------------------------------------*/
    
    int write(int input[5])
    {
        file = fopen (FILENAME, "w+"); //open file for write access
        if (file == NULL)
        {	
    	puts("an error occured!");
    	return 1;	
        }
        else
        {
    	   fprintf(file, "%d ", input[0]);
    	   fprintf(file, "%d ", input[1]);
    	   fprintf(file, "%d ", input[2]);
    	   fprintf(file, "%d ", input[3]);
    	   fprintf(file, "%d ", input[4]);
    	   fprintf(file, "%d ", input[5]);
    	   fclose(file);
        }
        return 0;
    }
    /*-------------------------------------------------------*/
    
    int remember(int val, int *saved, int size)
    {
        int i;
        for(i=0;i<size;i++)
        {
            if (saved[i]== val)
            {
                return 1;
            }
        }
    
        return 0;
    }
    i didnt have time to complete the source code with comments, i hope you'll
    find your way yourself.

    this is a lotto program what serves you with random generated lotto numbers.
    you can enter your own ones and safe them to a file, once done you can call these
    values back again.

    thats all the program does.
    remember:
    if you win with the numbers generated by this program, i want you to share
    the price you won with me.

    any feedback is welcome!

    threadhead

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    You might want to give your function names more descriptive names then just case3. Just a thought.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    My dos compiler

    Compiling...
    c:\msvc\bin\cb1.c
    c:\msvc\bin\cb1.c(23) : warning C4135: conversion between different integral types
    c:\msvc\bin\cb1.c(23) : warning C4761: integral size mismatch in argument : conversion supplied
    Linking...
    Creating browser database...
    CB1.EXE - 0 error(s), 2 warning(s)

    My Win32 console compiler
    Link Error : Duplicate COMDAT section: _write in
    files main.c, unistd.win32.obj

    don't think it cared too much for the use of globals.



    A few suggestions / comments:

    1) it would be nice if your user were given the chance to enter numbers after choosing option 1 if the file didn't exist. The program quit with an error message.

    2) it would be nice to loop back to the main menu to get the lotto gen numbers after the user enters his or her six values instead of the program quitting.

    3) it would be nice if the user were given the chance to use option 3 again if he or she didn't like the numbers instead of the program exiting.

    4) I entered 1, 2, 3, 4, 5, and 6 as my six numbers, but your program's option 1 recalled them as 1, 2, 3, 4, 5, and 7 though the numbers were written correctly to the file.

    Not bad at all....
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A few comments for you to think over

    >>case1()
    Better function names, please!

    >>if (file == NULL)
    >> puts("an error occured");
    Use perror()

    >>scanf("%d", &choice);
    >>fscanf(file, "%d", &input[0]);
    Check the return codes!

    >>#define SEED 49
    This may be named SEED, but isn't what it's used for. Again, name you variables appropriately, something like HIGHEST_NUMBER might have been a better choice.

    >>return((rand() % SEED) + 1);
    This isn't very random. Read this

    >>for (i = 0; i < 6; i++)
    >>{
    >>fscanf(file, "%d", &input[0]);
    >>fscanf(file, "%d", &input[1]);
    What is the point of looping 6 times, overwriting the same variables?

    >>printf("first number: %d\n", input[0]);
    Use a loop here!

    >>scanf("%d", &input[0]);
    Use a loop here, too.

    >>fprintf(file, "%d ", input[0]);
    And another loop needed.

    >>FILE *file
    Why is this variable global? If you have no good reason for it to be so, make it local instead.

    >>int input[5];
    >>puts("enter your 6 numbers: ");
    Err... do you see a slight bug within these two lines? I do!

    >>puts("1. show all your lottery numbers\n");
    Maybe you realised already, but puts() outputs a newline of its own, no need for you to do one as well, unless you want to that is.

    You can also write the remember function like this:
    Code:
    while (size--)
        if (*(saved++) == val) return 1;
    return 0;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    thanks for all your replys.
    ill keep ronins and hammers suggestions in mind to
    enhance my program.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>hammers suggestions in mind to enhance my program.
    You mean "fix" not enhance. It has some buffer overflows that need to be sorted out!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    well ok.
    i didnt say but meant that.

  8. #8
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    i fixed all the problems so far.
    but i didnt get off the segmentation fault.

    how can i change this? (how can i open a file in write/read access area?)

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program almost finished
    By alex1067 in forum C Programming
    Replies: 3
    Last Post: 04-05-2008, 10:03 PM
  2. Lottery game
    By geetard in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2005, 12:50 AM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM