Structure and Function

This is a discussion on Structure and Function within the C Programming forums, part of the General Programming Boards category; Hello guys, I already done the coding for this exercise but I still can't find out how to get the ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    Structure and Function

    Hello guys, I already done the coding for this exercise but I still can't find out how to get the correct names for "Types" and also for "continue". I hope you guys can help me with a simple and understandable answer. Thanks in advance.

    The coding that I have done
    Code:
    #include <stdio.h>
    
    struct people
    {
        char name[20], types[20];
        int age;
    }; // need to put semicolon here
    
    
    int main()
    {
        struct people m[20];
        int i = 0; //declare i = 0 if not error will be occured
        char y, n, adults, kids, teenagers, old_citizen;
    
    
        for (i = 0; i < 5; i++)
        {
            printf("Enter name : ");
            scanf("%s", &m[i].name); // i changed %c to %s. I got error if i put %c
            printf("Age        : ");
            scanf("%d", &m[i].age);
        }
    
    
        if (m[i].age >= 1 && m[i].age <= 12){
        printf("kids"); // i used printf instead of strcpy because i got error if i put strcpy. I don't know how to solve it.
        }
        else if (m[i].age >= 13 && m[i].age <= 23){
        printf("teenagers");
        }
        else if(m[i].age >= 24 && m[i].age <= 50){
        printf("adults");
        }
        else if (m[i].age >= 51 && m[i].age <= 80){
        printf("old citizen");
        }
    
    
        printf("\n---------------------------------------");
        printf("\nList:");
        printf("\nName \t Age \t Types");
    
    
        for (i = 0; i < 5; i++)
        {
        printf("\n%s \t%d \t%c", m[i].name, m[i].age, m[i].age);
        }
        {
        printf("\nWould you like to continue? [Y] for Yes, [N] for no :");
        scanf("%s", &y, &n);
        }
        while(getchar()== 'y' && getchar()!= 'n');
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    Sorry, but your question is as unclear as mud. You are assuming we know what your homework exercise is, and we don't.

    In terms of getting "correct names for Types", the general principle is to keep it simple. Name your user-defined types (structs, etc) in a way that is consistent with what they represent. Similarly with members of structs, and any variables.

    For example, if a struct type will represent information about a single person, name it "struct person" rather than a "struct people". If you need to represent multiple people (say an array of 20 struct person's), than maybe name that array as "struct person Person[20]" (since each element Person[0], Person[1], etc will represent a single person).

    You need to use a similar principle for naming your "types". What types of people (or whatever) do you need to represent? How are they different? How are they described? Are they distinct? For example, is a "child" one type of person, and a "adult" another type? If so, can you specify an enumerated type (with values named "child", "adult", ...) to represent that?


    The other thing to watch: don't try to write code before you understand what you're trying to do. The way you have structured and commented your code, it is obvious you are just typing things in to see if they work (and, on a quick look, I can see that code will not even compile, let alone do anything useful). At one level, that is alright - when a compiler complains, you learn something about the C programming language (its grammar, syntactic rules, etc). At another level it is all wrong - it is you who needs to understand what a person is, and what a "type of person" is, and how you want to represent those concepts --- before you type in any code. A C compiler only looks at the grammar and syntactic rules, and complains if you get THOSE wrong, and is completely ignorant about concepts like "person". So, if YOU don't know what a "person" represents, and the compiler can't know, then how can you expect to write code that works?
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,194
    Judging from the comment about strcpy, it seems you might be wanting to do this:

    Code:
    if (m[i].age >= 1 && m[i].age <= 12){
    		strcpy(m[i].types, "kids");
        }
        else if (m[i].age >= 13 && m[i].age <= 23){
    		strcpy(m[i].types, "teenagers");
        }
        else if(m[i].age >= 24 && m[i].age <= 50){
    		strcpy(m[i].types, "adults");
        }
        else if (m[i].age >= 51 && m[i].age <= 80){
    		strcpy(m[i].types, "old citizen");
        }
    Also this if..else if... sequence should probably be moved to inside your first for loop, otherwise m[i] will refer to m[5] which is undefined.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    scanf("%s", &m[i].name);
    That should be:
    Code:
    scanf("%s", m[i].name);
    The ampersand does not belong in this case as "name" represents an array which is treated as an address in this context.





    Code:
    printf("\n---------------------------------------");
    printf("\nList:");
    printf("\nName \t Age \t Types");
    
    for (i = 0; i < 5; i++)
    {
        printf("\n%s \t%d \t%c", m[i].name, m[i].age, m[i].age);
    }
    Here it looks like you want to print out the type and not the age as the third column of your table which means you should be using the %s format specifier and actually use m[i].types instead of m[i].age that second time.





    Code:
    {
        printf("\nWould you like to continue? [Y] for Yes, [N] for no :");
        scanf("%s", &y, &n);
    }
    while(getchar()== 'y' && getchar()!= 'n');
    Ok, this is all kinds of wrong. I think my brain just BSOD'ed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  2. Structure to Function
    By neptunusmaris in forum C Programming
    Replies: 2
    Last Post: 06-12-2009, 09:23 AM
  3. Structure to/from function
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-17-2007, 11:55 AM
  4. Problem with structure function
    By gell10 in forum C++ Programming
    Replies: 8
    Last Post: 06-06-2004, 11:35 AM
  5. Structure and Function
    By DocDroopy in forum C Programming
    Replies: 9
    Last Post: 08-03-2002, 11:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21