Thread: Updating Records

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    Question Updating Records

    Is it possible you could edit this function to update this particular record.

    Record
    Code:
    struct Student
    {
      int ID;
      char LastName[32]; 
      char FirstName[32];
      int DateOfBirth; 
      char Address[32];
      char TelephoneNumber[11]; 
      char ProgramPursued[32];   
    };
    
    struct Student students[10]={
    {123,"Hall", "Rayon", 160687, "43_Olympic_ Court", "898-0497", "Computer"},
    {234,"Spencer", "Sochelle", 220587, "29_Decent_Village", "528-5214", "Business"},         
    {345,"Dobson", "Dwayne", 890583, "263_Far Park Blvd", "457-2014", "Computer"},
    {456,"Clarke", "Dave", 110181, "87_Rasta_Village", "354-5874", "Law"},
    {567,"Currie", "Nickeisha", 230491, "26_Waterground_rd", "898-6578", "Nurse"},
    {678,"Blackwood", "Mariann", 10490, "3_St_Johns_road", "256-1458", "Art"},
    {789,"Hall", "Wesley", 101086, "3_Bayfarm_road", "236-1028", "Science"},
    {891,"Hall", "Bouki", 251285, "67_pimpim_drive", "887-9910", "Engineer"},
    {912,"Paul", "Shawn", 231180, "25_glasco_Close", "567-1996", "Law"},
    {101,"Able", "Frank", 100591, "1_Camp_road", "528-1235", "Business"},
    };

    Update Students
    Code:
    void UpdateStudents(Student students[])
    {
         char name[10];
         int position;
         int change;
         
      printf("Enter the First name of the person record you want to edit: ");
      scanf("%d", &name);
        for (int i=0;i<10;i++)
        {
            if (name==students[i].FirstName)
    {
                    printf("Enter Index position of desired changes[0-6]:");
                    fflush(stdin);
                    scanf("%d", &position);
                    
                    printf("What do you want to change it to:");
                    fflush(stdin);
                    scanf("%d", &change);
                    
                    Student students[position]=change;
                    }
                    }
                    
    
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) String comparison is done via strcmp(string1,string2) not ==.

    2) Don't fflush(stdin) EVER! Whoever taught you that has no idea what he is doing.

    3) Ident your code properly.

    4)Student students[position]=change; <----- Does not make any sense student = int???

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    could u show me how to allow the user to update the record. i.e if the user want to change something from the record he will be able to then append the new entry with the what the user edit

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If by position you mean the actual member of the struct (whether it's name, ID or whatever) then you are on the right track.

    1st: Make the changes I suggested above.

    2nd: Not sure what "int change" is in your logic.

    3rd: You will need to make a switch statement to switch around your position and consider what field needs editing in each of its cases.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    its kinda complicated. could u give me an example

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Look, if you are looking for someone to do the work for you that's not going to happen. You have to make an effort.

    The first instructions I posted should be PRETTY CLEAR. Remove the last line in your code, do some indentation so that the code blocks are nicely organized and {} are in their right places, read up strcmp() on GOOGLE and remove the fflush lines.

    What is complicated about all of this?

    Writing code is like building a house. You do it brick by brick, you don't just move the brand new sofa inside when you don't even have the roof completed.
    Last edited by claudiu; 04-09-2010 at 11:42 PM.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    3rd: You will need to make a switch statement to switch around your position and consider what field needs editing in each of its cases.


    This is what i dont understand. And im not asking u to do my work, i am a young student in C Programming and i just saw a practise question and attempt it. So your hep is very much appreciated.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    Question

    Code:
    void UpdateStudents(Student students[])
    {
       int choice;
    printf("What do you want to change?\n");
    printf("1. Change ID number\n 2. Change Last Name\n 3. Change First Name\n 4. Change Date of Birth\n 5. Change Address\n 6. Change Telephone number\n 7. Change Program\n");
    scanf("%d", &choice);
    switch (choice) {
        case 1:
            printf("Input new ID number: ");
            scanf("%d", &students[i].ID);
            break;
            
         case 2:
            printf("Input new Last Name: ");
            scanf("%d", &students[i].LastName);
            break;
            
         case 3:
            printf("Input new First Name: ");
            scanf("%d", &students[i].FirstName);
            break;
            
         case 4:
            printf("Input new Date of Birth: ");
            scanf("%d", &students[i].DateOfBirth);
            break;
            
          case 5:
            printf("Input new Address: ");
            scanf("%d", &students[i].Address);
            break; 
            
           case 6:
            printf("Input new Telephone Number: ");
            scanf("%d", &students[i].TelephoneNumber);
            break;  
                  
           case 7:
            printf("Input new Program: ");
            scanf("%d", &students[i].ProgramPursued);
            break;  
                        
        default:
            printf("Bad input\n");
            break;
    }  
    
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And what do you believe i to be in this function?

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok. Your framework should be something like this(from my understanding of what you want to do):

    Code:
    /* somewhere at the beginning of the function */
    int change_int;
    char change_str[32];
    
    /* .............. now inside your if */
    if (strcmp(name,students[i].FirstName) == 0)
    {
                    printf("Enter Index position of desired changes[0-6]:");
                    scanf("%d", &position);
                    
                    printf("What do you want to change it to:");
    
                    switch(position)
                    {
                       /* case 0 means the user wants to modify student.id, so read an int value */
                       case 0: scanf("%d",change_int);
                                    student[i].id = change_int;
                                    break;
                       /* case 1 means the user wants to modify student.lastname so read a string */
                       case 1: scanf("%s",change_str);
                                    strcpy(student.lastname,change_str);
                                    break;
                        /* so on and so forth for all of the fields */
    
                        default: break;
                    }           
                   
    }

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, not bad nuddy, yours is actually better because you are not overcomplicating it with additional temporary storage variables.

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    However make sure that this is inside your for, otherwise you are not modifying the position that holds the name that you want.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    could u put mines in the for loop just to give me an example of what it will do

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Here it is. Note that you are printf-ing strings as %d instead of %s so I changed that for you.

    Code:
    void UpdateStudents(Student students[])
    {
      int choice;
      char name[32];
      printf("Enter the First name of the person record you want to edit: ");
      scanf("%s", &name);
        for (int i=0;i<10;i++)
        {
            if (strcmp(name,students[i].FirstName) == 0)
            {
             printf("What do you want to change?\n");
             printf("1. Change ID number\n 2. Change Last Name\n 3. Change First Name\n 4. Change  Date of Birth\n 5. Change Address\n 6. Change Telephone number\n 7. Change Program\n");
             scanf("%d", &choice);
             switch (choice) {
                 case 1:
                  printf("Input new ID number: ");
                  scanf("%d", &students[i].ID);
                  break;
            
                case 2:
                 printf("Input new Last Name: ");
                 scanf("%s", &students[i].LastName);
                 break;
            
                case 3:
                 printf("Input new First Name: ");
                 scanf("%s", &students[i].FirstName);
                 break;
            
                case 4:
                 printf("Input new Date of Birth: ");
                 scanf("%d", &students[i].DateOfBirth);
                 break;
            
                case 5:
                 printf("Input new Address: ");
                 scanf("%s", &students[i].Address);
                 break; 
            
                case 6:
                 printf("Input new Telephone Number: ");
                 scanf("%s", &students[i].TelephoneNumber);
                 break;  
                  
                case 7:
                 printf("Input new Program: ");
                 scanf("%s", &students[i].ProgramPursued);
                 break;  
                        
                default:
                 printf("Bad input\n");
                 break;
              }
            /* break from for loop */
            break;
          }  
       }
    }
    Last edited by claudiu; 04-10-2010 at 12:20 AM.

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    How can i write what the user change back to the file "record.txt"

    Code:
    void UpdateStudents(Student students[])
    {
       int choice;
    for (int i=0;i<10;i++)
        {
    
    printf("What do you want to change?\n");
    printf("1. Change ID number\n 2. Change Last Name\n 3. Change First Name\n 4. Change Date of Birth\n 5. Change Address\n 6. Change Telephone number\n 7. Change Program\n");
    scanf("%d", &choice);
    switch (choice) {
        case 1:
            printf("Input new ID number: ");
            scanf("%d", &students[i].ID);
            break;
            
         case 2:
            printf("Input new Last Name: ");
            scanf("%d", &students[i].LastName);
            break;
            
         case 3:
            printf("Input new First Name: ");
            scanf("%d", &students[i].FirstName);
            break;
            
         case 4:
            printf("Input new Date of Birth: ");
            scanf("%d", &students[i].DateOfBirth);
            break;
            
          case 5:
            printf("Input new Address: ");
            scanf("%d", &students[i].Address);
            break; 
            
           case 6:
            printf("Input new Telephone Number: ");
            scanf("%d", &students[i].TelephoneNumber);
            break;  
                  
           case 7:
            printf("Input new Program: ");
            scanf("%d", &students[i].ProgramPursued);
            break;  
                        
        default:
            printf("Bad input\n");
            break;
    }  
    
    } }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Records and Files
    By nuddy in forum C Programming
    Replies: 13
    Last Post: 04-16-2010, 11:29 PM
  3. Replies: 10
    Last Post: 03-03-2010, 01:33 AM
  4. reading a file into a block
    By mickey0 in forum C++ Programming
    Replies: 19
    Last Post: 05-03-2008, 05:53 AM
  5. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM