Thread: Stuck On Program

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    1

    Angry Stuck On Program

    Hey can you help me with this program? I cant get it to work it will not display the final part.
    You can e-mail me on [email protected].

    /*Robert Parker*/
    /*29/01/02*/
    /*Berner Mountain Bike Club Marathon*/

    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>

    typedef struct
    {
    int age;
    char forname[30];
    char surname[30];
    char cat[1];
    float time;

    }competitor_structure;

    typedef struct
    {
    int last_no;
    int age;
    char forname[30];
    char surname[30];
    char cat[1];
    float time;

    }last_no_structure;

    FILE *competitor_file;
    FILE *last_no_file;
    int choice;
    competitor_structure competitor_array[1];
    last_no_structure last_no_array[1];
    int num_records = 1;
    int num_records1 = 1;
    int competitor_no;
    float average_time=0;
    float average_jev=0;
    float average_fun=0;
    float average_ex=0;
    float numof;
    float jevnum=0;
    float exnum=0;
    float funum=0;
    float jevtime=0;
    float extime=0;
    float funtime=0;

    time_t now;

    void intro();
    void menu();
    void add();
    void report();
    void competitortime();
    void race_report();
    void end();

    int main()
    {
    intro();

    do
    {
    menu();
    }while (choice!=5);
    system("cls");
    printf("\n\n\n\nThank you for using this program designed and implemented by Robert Parker\n\n\n\n");
    printf("\n\n\t\tPlease Have A Nice Day\n\n");
    system("pause");
    }
    void intro()
    {
    printf("\n\n\n\n\t\tWelcome to the Berner Mountain Bike Marathon Race Program");
    printf("\n\n\n\n\n\t\tProgram created by Robert Parker\n\n");
    printf("\n\t\t");
    system("PAUSE");
    }

    void menu()
    {
    system("cls");
    printf("\n\n\n\n\t\t\t\t\tMAIN MENU\n");
    printf("\n\n\t\t1 - Add Competitor to file");
    printf("\n\n\t\t2 - Produce Competitors Report");
    printf("\n\n\t\t3 - Enter Finished Results");
    printf("\n\n\t\t4 - Produce Finished Report");
    printf("\n\n\t\t5 - Exit Program");
    printf("\n\n\t\tPlease Enter Choice ");

    scanf("%i", &choice);
    fflush(stdin);
    switch(choice)
    {
    case 1: add() ;break;
    case 2: report() ;break;
    case 3: competitortime() ;break;
    case 4: race_report() ;break;
    case 5: break;
    default:
    {
    printf("\n\n\t\t Not A Valid Choice (1 to 5 Only): ");
    system("pause");
    menu();
    }
    }

    }

    void add()
    {

    competitor_file = fopen("c:\\competitors.bin","ab");

    system("cls");

    printf("\n\n\t\tAdd New Records To File");

    printf("\n\tForname ? ");
    fflush(stdin);
    gets(competitor_array[num_records].forname);

    printf("\n\tSurname ? ");
    fflush(stdin);
    gets(competitor_array[num_records].surname);

    printf("\n\tAge ? (18 to 99 only) ");
    scanf("%i", &competitor_array[num_records].age);

    printf("\n\tCatagory ? J(Juvenille) S(Standard) E(Expert) ");
    fflush(stdin);
    gets(competitor_array[num_records].cat);

    fwrite(&competitor_array[num_records],sizeof(competitor_structure),1,competitor_file);
    fclose(competitor_file);

    }


    void report()

    {
    /* open file for reading */
    competitor_file = fopen("c:\\competitors.bin","rb");

    /* clear the screen */
    system("cls");

    /* display headings */
    printf("\n\t\t\t\tCompetitor Reports");
    printf("\n\n\n ID NO\tFORNAME\t\t\t\tSURNAME\t\t\tAGE\tCATAGORY");
    /* read first record into array */
    num_records=0;
    fread(&competitor_array[1],sizeof(competitor_structure),1,competitor_file);
    /* read rest of records from file and place in array of records */
    while(!feof(competitor_file))
    {
    /* display record */
    printf("\n%6i %-30s %-30s %4i %-1s\n\t",num_records,competitor_array[1].forname,competitor_array[1].surname,
    competitor_array[1].age,competitor_array[1].cat);
    fflush(stdin);
    num_records++;
    fread(&competitor_array[1],sizeof(competitor_structure),1,competitor_file);
    }/* end of while */

    /* pause to allow user to view book details */
    system("pause");

    /* close file */
    fclose(competitor_file);
    }/* end of book_details */



    void competitortime()
    {
    /*This is the file paths*/
    competitor_file = fopen("c:\\competitors.bin","rb");
    last_no_file = fopen("c:\\competitortime.bin","ab");


    /*This is reading records from the racedetails file*/
    fread(&competitor_array[1],sizeof(competitor_structure),1,competitor_file);

    /*This is not the end of file marker*/
    while (!feof(competitor_file))
    { system("cls");
    printf("\n\tForename\t\t Surname\n");
    printf("\n\t%-30s\t%-20s\n",competitor_array[1].forname, competitor_array[1].surname);



    printf("Please enter race time for this Competitor\n");

    scanf("%f",&last_no_array[1].time);
    /*This is saving the files to the time_file*/

    fwrite(&last_no_array[1], sizeof(last_no_structure),1,last_no_file);
    /*This is writing to file*/

    num_records++;

    fread(&competitor_array[1], sizeof(competitor_structure),1,competitor_file);
    /*This is reading to file*/
    }

    fclose(competitor_file);
    fclose(last_no_file);
    /*This is closing both files*/


    system("pause");
    }




    void race_report()
    {

    last_no_file = fopen("c:\\competitortime.bin","rb");
    competitor_file = fopen("c:\\competitors.bin","rb");
    /*This is opening the time_file*/

    system("cls");

    printf("\n\nThis is a full Competitor report with race times\n");



    fread(&last_no_array[1], sizeof(last_no_structure),1,last_no_file);

    float total_time=0;
    /*This is for storing the total time*/

    while (!feof(last_no_file))

    {
    printf("\n%6i %-20s %-30s %.2f\n\n", competitor_array[1].forname,
    competitor_array[1].surname,
    last_no_array[1].time);
    /*This is calalatuing the total time*/
    total_time=total_time+last_no_array[1].time;
    if ((last_no_array[1].cat[0]=='J')||(last_no_array[1].cat[0]=='j'))
    {
    jevtime = jevtime + (last_no_array[1].time);
    jevnum+1;
    }


    if ((last_no_array[1].cat[0]=='E')||(last_no_array[1].cat[0]=='e'))
    {
    extime = extime + (last_no_array[1].time);
    exnum+1;
    }


    if ((last_no_array[1].cat[0]=='S')||(last_no_array[1].cat[0]=='s'))
    {
    funtime = funtime + (last_no_array[1].time);
    funum+1;
    }


    fread(&last_no_array[1],sizeof(last_no_structure),1,last_no_file);
    /*This is reading the file*/
    }



    average_time=0;

    /*This calaulating the average time*/

    printf("\n total_time = %.2f com_num = %i",&total_time,&num_records);
    printf("\n funtime = %.2f funum = %i",&funtime,&funum);
    printf("\n extime = %.2f exnum = %i",&extime,&exnum);
    printf("\n jevtime = %.2f jevnum = %i",&jevtime,&jevnum);

    average_time = total_time/num_records;
    average_fun = funtime/funum;
    average_ex = extime/exnum;
    average_jev = jevtime/jevnum;

    printf("\n\n\n\n");
    printf("The average time is %.2f\n\n",&average_time);
    printf("the average J time was %.2f\n\n\n",&average_jev);
    printf("the average S time was %.2f\n\n\n",&average_fun);
    printf("the average E time was %.2f\n\n\n",&average_ex);
    fclose(last_no_file);
    fclose(competitor_file);
    system("pause");
    system("cls");
    }
    Last edited by Robert Parker; 05-03-2002 at 04:12 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Mmm,
    Apart from the include of iostream, I couldn't see anything which was C++ about it.

    It's also broken as follows.
    1. don't use fflush(stdin)
    Look - it's been discussed before
    http://www.cprogramming.com/cboard/s...der=descending

    2. don't use gets
    There is no protection against buffer overflow
    Use the fgets() function instead - but be aware that fgets stores newlines.

    3. char cat[1];
    If you want to hold a 1 char string, this needs to be
    char cat[2];
    Remember the \0 which marks the end of the string

    4. competitor_array[1].forname
    You seem to be using index 1 an awful lot. Two things
    - arrays start at index 0
    - I would have expected a loop variable, which would be used to increment through each array index.

    Enough for now...

  3. #3
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  4. #4
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Im really tired to compile right now so...

    Could you post what errors the compiler had when attempting to compile? As well as descriptions, line numbers etc? Then I'll see if I can fix it. Good luck.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    1

    This is college work fool

    Bob i hopev u know that this is college work

    Krusty

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Could you please use the tags

    Could you please use the code- tags so I can read your code.

    Thanx
    C++
    The best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. somebody help me with this array program
    By hieugene in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2006, 05:53 AM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM