Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

mind boggling stuff...!!

  • 13-03-2002 7:07pm
    #1
    Closed Accounts Posts: 64 ✭✭


    can some1 please please please PLEASE tell me why the delete part of this wont work??!? i got the rest of it working all right but that one part is annoying me and i'm getting the feeling that it's something simple and i just havent been able to spot it!! n e ideas n e 1!?


    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>

    #define CUSTFILE "C:\\My Documents\\College stuff\\Customer.txt"

    int readFromFile ();
    int countRecords (FILE*);
    void menu ();
    void addRecords ();
    void writeRecords ();
    void listRecords ();
    void editRecords ();
    void deleteRecords ();
    void viewRecord ();

    typedef struct {

    int cust_no;
    char cust_name [40];
    char cust_address [50];
    } customers ;


    customers cust_records [100];
    int totalRecords = 0;

    ////////////////////////////////////////////////////////////////////////////////

    void main ()
    {
    totalRecords = readFromFile ();
    menu ();
    writeRecords ();

    }

    ////////////////////////////////////////////////////////////////////////////////

    void menu ()
    {
    int choice ;


    do
    {
    printf("\n\n\n1. Add Customer: ");
    printf("\n2. List Customers: ");
    printf("\n3. Edit Records: ");
    printf("\n4. Delete Records: ");
    printf("\n5. View a Customer Record: ");
    printf("\n6. Quit: ");
    printf("\n\nEnter Choice:" );
    scanf("%d", &choice);
    switch (choice)
    {
    case 1: addRecords ();
    break;
    case 2: listRecords ();
    break;
    case 3: editRecords ();
    break;
    case 4: deleteRecords ();
    break;
    case 5: viewRecord ();
    break;
    case 6:
    break;
    default: printf("Please enter 1 to 5" );
    break;
    }
    }while (choice !=6);
    }

    ////////////////////////////////////////////////////////////////////////////////

    void writeRecords ()
    {
    int curr_rec;
    FILE *custFile;

    if ((custFile = fopen (CUSTFILE, "w+b" )) == NULL)
    printf("Error: Opening customer file has failed");

    else
    for (curr_rec = 0; curr_rec <totalRecords; curr_rec ++)
    {
    if (cust_records [curr_rec].cust_no != NULL)

    fwrite(&cust_records [curr_rec], sizeof(customers), 1, custFile);

    }
    fclose(custFile);
    }

    ////////////////////////////////////////////////////////////////////////////////

    int readFromFile ()
    {
    FILE *custFile;
    int noRecords;
    int curr_rec;

    if ((custFile = fopen(CUSTFILE, "r+b")) == NULL)
    printf("Error: Opening Customer file has failed");

    else
    {
    noRecords = countRecords (custFile);

    fseek(custFile, 01, SEEK_SET); //sets pointer to beginning of file

    for (curr_rec =0; curr_rec<noRecords; curr_rec++)
    fread(&cust_records [curr_rec], sizeof(customers), 1,custFile);
    }

    fclose (custFile);
    return noRecords;

    }

    ////////////////////////////////////////////////////////////////////////////////

    int countRecords (FILE *custFile)
    {
    long current_position;
    int noRecords;

    fseek(custFile, 01, SEEK_END); //sets pointer to end of file

    current_position =ftell (custFile); //find current position
    noRecords =current_position /sizeof (customers); //find number of records

    return noRecords;
    }

    ////////////////////////////////////////////////////////////////////////////////

    void addRecords ()
    {
    char ans;
    do{
    printf("\n\nEnter Customer Number: ");
    scanf("%d", &cust_records [totalRecords].cust_no);
    fflush(stdin);
    printf("\nEnter Customer Name: ");
    gets(cust_records [totalRecords].cust_name);
    printf("\nEnter Customer Address: ");
    gets(cust_records [totalRecords].cust_address);
    totalRecords ++;
    printf("\nTotRec: %d", totalRecords);
    fflush(stdin);
    printf("\nDo you want to add another record: [Y/N]" );
    ans=getche();
    }while ((ans == 'Y') || (ans == 'y'));

    }

    ////////////////////////////////////////////////////////////////////////////////

    void listRecords ()
    {
    int i;

    for (i=0; i<totalRecords; i++)
    {
    printf("\n%d", cust_records .cust_no);
    printf("\n%s", cust_records .cust_name);
    printf("\n%s\n", cust_records .cust_address);
    }
    }

    ////////////////////////////////////////////////////////////////////////////////

    void viewRecord ()
    {
    int i;
    char query_name [40];
    char ans;

    fflush(stdin);
    printf("Enter customer name: ");
    gets(query_name);

    for (i=0; i<totalRecords; i++)
    {
    if (strcmp(query_name, cust_records . cust_name) == 0)
    {
    printf("\n<< Customer Record Found >>");
    printf("\n\n%s", cust_records . cust_name);
    printf("\n%s", cust_records . cust_address);
    printf("\n%d", cust_records .cust_no);
    printf("\n\nCorrect Customer Record [Y/N]:");
    ans=getche();
    if (ans=='Y' || ans == 'y')
    return;

    }
    }
    printf("\n\n<<No Customer Record Found >>");
    getch();
    }

    ////////////////////////////////////////////////////////////////////////////////

    void editRecords ()
    {
    int i;
    char query_name [40];
    char ans;

    fflush(stdin);
    printf("Enter customer name: ");
    gets(query_name);

    for (i=0; i<totalRecords; i++)
    {
    if (strcmp(query_name, cust_records .cust_name)==0)
    {
    printf("\n<< Customer Record Found >>");
    printf("\n\n %s", cust_records .cust_name);
    printf("\n %s", cust_records .cust_address);
    printf("\n %d", cust_records .cust_no);
    printf("\n\nCorrect Customer Record [Y/N]: ");
    ans=getche ();
    if (ans == 'Y' || ans == 'y')
    {
    printf("\n\n\nEnter new name: ");
    gets(cust_records .cust_name);
    printf("\nCustomer Address: ");
    gets(cust_records .cust_address);
    return
    ;

    }
    }
    printf("\n\n<<No Customer Record Found>>");
    getch();
    }

    ////////////////////////////////////////////////////////////////////////////////

    void deleteRecords ();
    {

    int i;
    char query_name [40];
    char ans;

    fflush(stdin);
    printf("Enter customer name: ");
    gets(query_name);

    for (i=0; i<totalRecords; i++)
    {
    if (strcmp (query_name, cust_records.cust_name) == 0)
    {
    printf("\n<<Customer Record Found>>");
    printf("\n\n%s", cust_records .cust_name);
    printf("\n%s", cust_records .cust_address);
    printf("\n%d", cust_records.cust_no);
    printf("\n\nCorrect Customer Record [Y/N]:");
    ans=getche();
    if (ans=='Y' || ans=='y')
    {
    cust_records .cust_no =NULL;
    strcpy(cust_records .cust_name, "\0");
    strcpy(cust_records .cust_address, "\0");
    return;
    }
    }
    }

    printf("\n\n<<No Customer Record Found >>");
    getch();
    }
    }


Comments

  • Registered Users, Registered Users 2 Posts: 2,281 ✭✭✭DeadBankClerk


    upload customer.txt please.
    i have the project open in vcpp


  • Registered Users, Registered Users 2 Posts: 310 ✭✭Cerberus


    At a quick glance...
    in your delete function code you have a semi colon after the function name
    void deleteRecords ();
    {


    and as well I think you have 1 too many closing braces at the end of the function.
    I would have thought the compiler would pick up this but maybe not.


  • Closed Accounts Posts: 64 ✭✭wee_lady


    yeah i thought the same but the reason they are there is because it was putting up error messages for them! so when i put them there and commented out the whole delete function it ran no problems! i'm soooo confused!!


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭Zab


    You missing a curly bracket from the end of editRecords()...or actually from close to the end. That's why you need the semicolon and the extra curly bracket in deleteRecords().

    Zab.


  • Closed Accounts Posts: 64 ✭✭wee_lady


    woo hoo! thank u so very much! knew it had to be something simple! it's working now though! thanks ur a life saver!!


  • Advertisement
  • Closed Accounts Posts: 64 ✭✭wee_lady


    well just as i thought i had it all sorted!! i've now got all my options working perfectly and when i list the customers it's showing them the way it should etc etc! BUT........when i break out of the program and go back into it again, enter the list option its showing everything ok but it's got the first letter of everything cut off! and it's also showing customer number as a long string of numbers! for example where it should show the number 1 it's showing 1667322368! how can i fix this??


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭Zab


    Why are you fseek()ing to to an offset of 1?


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    hahahahah good man zab, spotting the off by one error.

    should be zero and also you should be seeking back to the start of the file in the function that counts the number of records, your read file function shouldnt have to fix things caused in other functions, thats strongly coupled code so it is, and you dont want that.


  • Closed Accounts Posts: 64 ✭✭wee_lady


    and the mad thing is....this is code my lecturer gave me in class! when it started going all wrong on me i went back over the code she gave us and it's identical! it's MAD MAD MAD!!! but thanks i'll try wot u suggessted!


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    maybe she deliberatley give it to you that way. To see if you might learn something about programming and debugging.


  • Advertisement
  • Closed Accounts Posts: 64 ✭✭wee_lady


    nah she mite be bad but she's not THAT bad! o well sure we'll not worry about it now that it's sorted! thanks all!


Advertisement