heyho im finished with my first complete program in C.
heres the source code:
i didnt have time to complete the source code with comments, i hope you'llCode:#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 threadhead@gmx.net\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; }
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



LinkBack URL
About LinkBacks




