Thread: Problem with some code

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    Question Problem with some code

    Here is my code. The problem is when I run the Addrecords function the first time it works, but the next time I run it, it goes straight back to the main menu. I don't understand why!

    Can someone have a look and see if they can spot why....





    /* Header files defined */
    #include <stdio.h>

    /* Constants defined */
    #define makemax 50
    #define modelmax 50
    #define yearregmax 10
    #define regmarkmax 10
    #define mileagemax 10
    #define pricemax 10
    #define maxrecords 30

    /* Structed defined */
    struct carrecords
    {
    char make[makemax];
    char model[modelmax];
    char yearreg[yearregmax];
    char regmark[regmarkmax];
    char mileage[mileagemax];
    char price[pricemax];
    int used;

    }record[maxrecords];

    /* Variables defined */
    int i, k, count;
    char quit, option;
    int makel, modell, yearregl, regmarkl, mileagel, pricel;

    /* File Pointer defined */
    FILE *fp;


    /* Function to add records to that database */
    void addrecord(void)
    {
    if ((fp = fopen("car.data", "a+b"))==NULL) /* Opens the database file for writing to */
    {
    printf("\n\nCannot open file!\n\n"); /* Error statement if database file can not be opened */
    }

    else
    {
    char sync = getchar(); /* Captures carriage return from menu */
    fread(&record, sizeof(struct carrecords), maxrecords, fp); /* Reads database file into structure records */


    for (k=0;k <= maxrecords;k++)
    {
    if (record[k].used & 1) /* Searches through the structure for first available record */
    {
    ;
    }
    else
    {
    break; /* Breaks when it finds an available record. */
    }
    }

    for(i = k; quit != 'q'; i++) /* Starts at that available record to enter record data */
    {

    printf("\n\nEnter details for each car\n\n");

    printf("Make of Car: ");
    fgets(record[i].make, makemax, stdin);
    makel = strlen(record[i].make);
    record[i].make[makel - 1] = '\0';

    printf("Model of Car: ");
    fgets(record[i].model, modelmax, stdin);
    modell = strlen(record[i].model);
    record[i].model[modell - 1] = '\0';

    printf("Year of Registration: ");
    fgets(record[i].yearreg, yearregmax, stdin);
    yearregl = strlen(record[i].yearreg);
    record[i].yearreg[yearregl - 1] = '\0';

    printf("Registration Number: ");
    fgets(record[i].regmark, regmarkmax, stdin);
    regmarkl = strlen(record[i].regmark);
    record[i].regmark[regmarkl - 1] = '\0';

    printf("Car Mileage: ");
    fgets(record[i].mileage, mileagemax, stdin);
    mileagel = strlen(record[i].mileage);
    record[i].mileage[mileagel - 1] = '\0';

    printf("Price: ");
    fgets(record[i].price, pricemax, stdin);
    pricel = strlen(record[i].price);
    record[i].price[pricel - 1] = '\0';

    record[i].used = 1;

    printf("\nPress q to display records or Enter to enter more data:");
    scanf("%c", &quit);
    }
    }
    fwrite(&record, sizeof(struct carrecords), maxrecords, fp); /* Writes the structure to the database file */
    fclose(fp); /* Closes the file */
    main(); /* Goes back to the main menu */
    }

    void browserecord(void)
    {
    if ((fp = fopen("car.data", "r+b"))==NULL)
    {
    printf("\n\nCannot open file!\n\n");
    main();
    }

    fread(&record, sizeof(struct carrecords), maxrecords, fp);

    for(i=0; i<=maxrecords; i++)
    {
    printf("%s, %s, %s, %s, %s, %s\n", record[i].make, record[i].model, record[i].yearreg, record[i].regmark, record[i].mileage, record[i].price);
    }

    fclose(fp);

    }


    int main(void)
    {
    printf("\nCar Database\n");
    printf("===============================\n");
    printf("1. Add Records\n");
    printf("2. Browse Records\n");
    printf("3. Exit Program\n");
    printf("\n");

    while(1)
    {

    printf("Please enter an option 1,2 or 3: ");
    scanf("%s", &option);

    switch(option)
    {
    case '1':
    addrecord();
    break;
    case '2':
    browserecord();
    break;
    case '3':
    exit(0);
    break;
    default:
    printf("\n\nBad Input. Please try again!\n\n");
    break;

    }
    }

    }

  2. #2
    apparitionzero
    Guest
    i suggest using "return;" to exit a void function rather than calling another instance of "main()"

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Yeah calling main inside a function gave me loads of problems when I tried it, *shudders* never again!!!
    Last edited by C_Coder; 03-16-2002 at 01:47 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    6
    Yes thanks for that will use return instead of calling main.

    However it still doesn't solve my problem.
    Any more ideas?

    Thanks

    Zac

  5. #5
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    The problem is when I run the Addrecords function the first time it works, but the next time I run it, it goes straight back to the main menu.
    Is this still the problem? When I ran your code(changed the calls to main to return statements) Addrecords was called correctly each time, allowing me to enter as many car details as I wanted, well as many as maxrecords would allow.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    11

    car records

    Hello there,
    maybe I am being stupid but your code does not write to disk, you need to change the file open line to "a:\\cardata.bin,rb
    this will test for your file name on the drive you specify and create it if it is not already there.

    Change the a:\\ to c:\\ if you want it on your hard disk. Then add some data,and open the data file in notepad to verify that it contains data .

    I did this and the data is written to disk. However I could not view the records, I think some additional code is needed here, can you reply if you still want some help with this program.
    If its not 5.00p.m. on Friday its crap!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM