Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C Program - Help needed in editing user detail

    Dear all, i need some help in solving my programming problem.

    Below is the program that i have written and there is 2 part which i need help. What i need to do is as follows:

    Edit user detail:
    1) Enter user name
    2) Do a comparsion between the existing name which i enter earlier in myfile5.txt and the name that i just enter
    3) If valid user, show user detail and allow me to edit the detail

    View user detail:
    1) Enter user name
    2) Do a comparsion between the existing name which i enter earlier in myfile5.txt and the name that i just enter
    3) If valid user, show user detail

    What i'm not sure is how to do a strcmp between the existing user and the name that i enter and also how to extract the user detail

    Please help
    Thanks in advance
    west_dweller79

    #include <iostream>
    #include <fstream>
    #include <stdio.h>
    #include <string.h>


    struct DETAIL
    {
    char name[20];
    char tel_num[10];
    char sex[10];
    char age[10];
    };

    struct address
    {
    char house_num[10];
    char street[20];
    char town[20];
    char county[20];
    char post_code[10];
    };


    int main ()
    {

    int i;
    char name[20];

    struct DETAIL detail;
    struct address add;

    FILE * pFile;
    pFile = fopen ("myfile5.txt","at");

    a: printf("\n\nTo add new user, Press 1",i);
    printf("\nTo edit user, Press 2",i);
    printf("\nTo view user detail, Press 3", i);
    printf("\nTo exit, Press 4\n",i);
    scanf("%d",&i);

    if(i==1)
    goto b;
    else if(i==2)
    {
    printf("\nEnter user name : ");
    gets(name);
    gets(name);

    if(strcmp(detail.name,name)!=0)
    {
    printf("\nUser found!!");

    puts(detail.name,);
    gets(detail.tel_num);
    gets(detail.sex);
    gets(detail.age);
    gets(add.house_num);
    gets(add.street);
    gets(add.town);
    gets(add.county);
    gets(add.post_code);

    goto a;
    }
    else
    {
    printf("\nUser not found");
    exit(1);
    }
    }
    else if(i==3)
    {

    while(pFile)
    {

    }
    printf("\nPlease enter the user name: ");
    gets(name);

    if(strcmp(detail.name,name)!=0)
    {
    printf("\nUser found!!");

    gets(detail.name);
    gets(detail.tel_num);
    gets(detail.sex);
    gets(detail.age);
    gets(add.house_num);
    gets(add.street);
    gets(add.town);
    gets(add.county);
    gets(add.post_code);
    }
    else
    {
    printf("\nUser not found");
    exit(1);
    }
    }
    else if(i==4)
    {
    printf("\n\nExisting Program!!");
    exit(1);
    }
    else
    {
    printf("\nWrong number enter!!!");
    goto a;
    }



    b: if (pFile!=NULL)
    {
    printf("\n\nEnter the name: ");
    gets(detail.name);
    gets(detail.name);
    fprintf(pFile, "\n\nName:\t\t%-20.20s\n",detail.name);
    //fputs(detail.name,pFile);

    printf("Enter the telephone number: ");
    gets(detail.tel_num);
    fprintf(pFile, "Telephone:\t%-10.10s\n",detail.tel_num);
    //fputs (detail.tel_num,pFile);

    printf("Enter the sex: ");
    gets(detail.sex);
    fprintf(pFile, "Sex:\t\t%-10.10s\n",detail.sex);
    //fputs (detail.sex,pFile);

    printf("Enter the age: ");
    gets(detail.age);
    fprintf(pFile, "Age:\t\t%-3.3s\n\n",detail.age);
    //fputs (detail.age,pFile);

    printf("\nEnter the address as followed:\n");
    printf("House Number: ");
    gets(add.house_num);
    fprintf(pFile, "Address:\t%-5.5s",add.house_num);
    //fputs (add.house_num,pFile);

    printf("Street: ");
    gets(add.street);
    fprintf(pFile, "%-30.30s\n",add.street);
    //fputs (add.street,pFile);

    printf("Postal Code: ");
    gets(add.post_code);
    fprintf(pFile, "Postal Code:\t%-10.10s\n",add.post_code);
    //fputs (add.post_code,pFile);

    printf("Town: ");
    gets(add.town);
    fprintf(pFile, "Town:\t\t%-10.10s\n",add.town);
    //fputs (add.town,pFile);

    printf("County: ");
    gets(add.county);
    fprintf(pFile, "County:\t\t%-10.10s",add.county);
    //fputs (add.county,pFile);



    fflush(stdin);
    }

    goto a;



    //printf("\n\n\nName entered: %s\n\n", detail.name);
    //printf("Telephone number entered %s\n\n", detail.tel_num);
    //printf("Sex entered: %s\n\n",&detail.sex);
    //printf("Age entered: %s\n\n", detail.age);
    //printf("Address entered: \n%s %s \n%s \n%s \n%s\n\n", &add.house_num,
    // &add.street, &add.town, &add.county, &add.post_code);

    fclose (pFile);
    return 0;
    }

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, there are a few things that would need to be known about the text file and how its set up. But a simple string compare can be done with the '==' in an 'if' statement. But I would recomend a function call that would make all letters of the names either lowercase or uppercase as well as doing a crop of all whitespace surrounding the name.

    Next time you post code put it in the appropriate brackets for better viewing.

    Also, the "goto" function of C is really ugly and shouldn't be used unless really necessisary, you could have done this in a simple for loop...



    Jason


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •