Thread: Need help reding in struct

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    23

    Need help reding in struct

    Ok so this is what i have so far... Its about done for what i can see but im having problems when it gets to the reading in gender. Im assuming that i have syntax problems with readin in the name and or age. Please help anything would be nice.

    Sorry about the spacing im sure there was a BB code to fix it... If an Admin or Mod can edit it that would be fine.


    Code:
    #include
    <stdlib.h>
    
    #include
    <stdio.h>
    
    #include
    <string.h>
    
    
    #define
     pause system("pause")
    
    
    typedef
    struct {
    
        
    char yourName[25];
    
        
    int    age;
    
        
    char gender;
    
    } RECORD1;
    
    
    typedef
    struct {
    
        
    char yourName[25];
    
        
    int    age;
    
        
    char gender;
    
    } RECORD2;
    
    
    typedef
    struct {
    
        
    char yourName[25];
    
        
    int    age;
    
        
    char gender;
    
    } RECORD3;
    
    
    typedef
    struct {
    
        
    char yourName[25];
    
        
    int    age;
    
        
    char gender;
    
    } RECORD4;
    
    
    typedef
    struct {
    
        
    char yourName[25];
    
        
    int    age;
    
        
    char gender;
    
    } RECORD5;
    
    
     
    
    //void displaySquare(RECORDS *birthday)
    
    //{
    
    //    printf("%02i / %02i / %04i \n" , birthday->month , birthday->day , birthday->year);
    
    //    
    
    //}
    
    
     
    
     
    
     
    
     
    
     
    
    //DISPLAY 3th record
    
    void
     printEmployee(RECORD3 *emp) {
    
    
        printf(
    "Bankers name    : %s.\n", emp->yourName);
    
        printf(
    "Bankers age     : %i.\n", emp->age);
    
        printf(
    "Bakers gender  : %c\n", emp->gender);
    
    
    }
    
    
     
    
     
    
    int
     main()
    
    {
    
        RECORD1 teacher;
    
        RECORD2 student;
    
        RECORD3 banker;
    
        RECORD4 tester;
    
        RECORD5 gamer;
    
        
    int choice = 0;
    
        
    int number = 0;
    
        
    int numberSquared = 0;
    
    
        
    /*strcpy(student.yourName, "Justin");
    
        student.age = 19;
    
        student.gender = 'M';*/
    
    
     
    
        
    do{
    
            printf(
    "What would you like to do?\n");
    
            printf(
    "1)Diplay the square of a number\n");
    
            printf(
    "2)Enter 5 records\n");
    
            printf(
    "    A.Name\n");
    
            printf(
    "    B.Age\n");
    
            printf(
    "    C.Gender\n");
    
            printf(
    "3)Diplay the 3th record\n");
    
            printf(
    "4)quit the program\n");
    
            scanf(
    "%i" , &choice);
    
    
     
    
            
    //error checking
    
            
    while(choice < 1 || choice > 4)
    
            {
    
                printf(
    "Sorry that is not a choice please enter a valid choice: ");
    
                scanf(
    "%i" , &choice);
    
            }
    
    
            
    if(choice == 1)
    
            {
    
                printf(
    "Enter a number to square: ");
    
                scanf(
    "%i" , &number);
    
    
     
    
                numberSquared = number * number;
    
    
                printf(
    "The number %i squared is %i " , number , numberSquared);
    
            }
    
    
            
    if(choice == 2)
    
            {
    
                printf(
    "Enter in the name of the teacher: ");
    
                scanf(
    "%s" , &teacher.yourName);
    
    
                printf(
    "Enter in the age of the teacher: ");
    
                scanf(
    "%i" , &teacher.age);
    
    
                printf(
    "Enter in the gender of the teacher: ");
    
                scanf(
    "%c" , &teacher.gender);
    
    
                system(
    "cls");
    
    
                printf(
    "Enter in the name of the student: ");
    
                scanf(
    "%s" , &student.yourName);
    
    
                printf(
    "Enter in the age of the student: ");
    
                scanf(
    "%i" , &student.age);
    
    
                printf(
    "Enter in the gender of the student: ");
    
                scanf(
    "%c" , &student.gender);
    
    
                system(
    "cls");
    
    
                printf(
    "Enter in the name of the banker: ");
    
                scanf(
    "%s" , &banker.yourName);
    
    
                printf(
    "Enter in the age of the banker: ");
    
                scanf(
    "%s" , &banker.age);
    
    
                printf(
    "Enter in the gender of the banker: ");
    
                scanf(
    "%c" , &banker.gender);
    
    
                system(
    "cls");
    
    
                printf(
    "Enter in the name of the tester: ");
    
                scanf(
    "%s" , &tester.yourName);
    
    
                printf(
    "Enter in the age of the tester: ");
    
                scanf(
    "%i" , &tester.age);
    
    
                printf(
    "Enter in the gender of the tester: ");
    
                scanf(
    "%c" , &tester.gender);
    
    
                system(
    "cls");
    
    
                printf(
    "Enter in the name of the gamer: ");
    
                scanf(
    "%s" , &gamer.yourName);
    
    
                printf(
    "Enter in the age of the student: ");
    
                scanf(
    "%i" , &gamer.age);
    
    
                printf(
    "Enter in the gender of the student: ");
    
                scanf(
    "%c" , &gamer.gender);
    
    
                system(
    "cls");
    
            }
    
    
            
    if(choice == 3)
    
            {
    
                
    //Show 3th record
    
                printEmployee(&banker);
    
            }
    
    
            
    
            pause;
    
            system(
    "cls");
    
            
    return 0;
    
            
    
        }
    while(choice !=4);
    
    
     
    
     
    
     
    
        pause;
    
        system(
    "cls");
    
        
    return 0;
    
            
    
    }
    //END MAIN
    
    
     
    
     
    
     
    
     
    
     
    
     
    
    Last edited by JustinH; 03-11-2012 at 12:23 AM.

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    Help please

  3. #3

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    46
    I didn't get a chance to read it all the way through, but from what I see, there is no reason that you should be recreating your RECORD struct since they are all the same.

    Declaring a typedef is not the same as declaring a variable, you are simply creating the contents of your RECORD struct.
    You only need to declare the typedef once, but you can declare as many RECORD variables as you'll need in your program:
    Code:
    typedef struct{
          char *yourName;
          int age;
          char gender;
    } RECORD;
    
    int main (void)
    {
         RECORD teacher, student, banker, tester, gamer;
    
         return 0;
    
    }
    Another thing you should do is since there are more than a couple of choices the user could enter, you'd be better off using a switch statement.
    It'd make your code a lot easier to read and possibly a bit faster

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    You think don't I didn't know that? That's not the question I had. The problem was in the %c when reading in a char,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. reding errors from the compiler
    By joseCarlos in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2010, 03:22 PM
  3. reding file and changing parts
    By Wackot in forum C Programming
    Replies: 1
    Last Post: 10-12-2010, 02:54 PM
  4. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  5. Wierd buffer problem reding characters
    By thenrkst in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 07:13 PM