Problems with Structs

On the keyboard type sequence records containing data of a medical examination of schoolchildren: Full name, date of birth, height and weight.Print out entered data in a table sorting them by names of the students alphabetically.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
struct data{

   char name [20];
   int age;
   int weight

};


int main(){

struct data stuff;



for(int i = 0; i < 20; i++){

  scanf("%c", stuff.name[i]);
}

scanf("%i", stuff.age);

scanf("%i", stuff.weight);


//do i use fflush() here?



}



anyway im absolutely puzzled as to how i do this.....i've been searching for solutions all day and this is the second thread i've made. if anyone can help, please do. thank you.
Last edited on
bump. anyone, please.
you need an 'array' of struct data . each student is a struct data.
im sorry, but i have a really hard time understanding. the assignment calls for the user entering the name of the students into the same struct and their information and then printing it out in a table like form all sorted....i believe.
closed account (zb0S216C)
data::name is an array of 20 characters - only 19 are used to store the name (the 20th is the null-character). As twoencore suggested, you'll need to instantiate data n amount of times. Where n is the amount of students. For example:

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

struct data
{
    char name[20];
    int age;
    int weight;
};

int main()
{
    struct data Students[NO_OF_STUDENTS]; // 20 students.
    int Counter = 0;
  
    for(; Counter < NO_OF_STUDENTS; ++Counter)
    {
        Students[Counter].name[19] = '\0'; // Set the end of the string.
        scanf("%19s", Students[Counter].name); // Extract 19 characters.

        // An so on...
    }
}

Wazzak
Last edited on

[\code]

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <conio.h>


#define NO_OF_STUDENTS 20

struct data
{
char name[20];
int age[20];
int weight[20];
};

int main()
{
struct data Students[NO_OF_STUDENTS]; // 20 students.
int Counter = 0;

for(; Counter < NO_OF_STUDENTS; ++Counter)
{
Students[Counter].name[19] = '\0'; // Set the end of the string.
scanf("%19s", Students[Counter].name); // Extract 19 characters.
}

for(; Counter < NO_OF_STUDENTS; ++Counter)
{
Students[Counter].age[19] = '\0'; // Set the end of the string.
scanf("%19d", Students[Counter].age); // Extract 19 characters.
}

for(; Counter < NO_OF_STUDENTS; ++Counter)
{
Students[Counter].weight[19] = '\0'; // Set the end of the string.
scanf("%19d", Students[Counter].weight); // Extract 19 characters.
}



}

[/code]


yes now what? im sorry, i just need assistance and i dont know programming very well, and i dont have time! :|

Last edited on
closed account (zb0S216C)
You only need 1 loop. The // An so on... comment I left was an indication to you. It translates to: "Extract data::age & data::weight".

Wazzak
Last edited on
well ok i stuffed them into one loop. this is difficult.
Last edited on
now what?
closed account (zb0S216C)
You need to update your code so I know where you stand. update the code in your previous post.

Wazzak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct data
{
char name[20];
int age[20];
int weight[20];
};

int main()
{
struct data Students[NO_OF_STUDENTS]; // 20 students.
int Counter = 0;

for(; Counter < NO_OF_STUDENTS; ++Counter)
{
Students[Counter].name[19] = '\0'; // Set the end of the string.
scanf("%19s", Students[Counter].name); // Extract 19 characters.
Students[Counter].age[19] = '\0'; // Set the end of the string.
scanf("%19d", Students[Counter].age); // Extract 19 characters.
Students[Counter].weight[19] = '\0'; // Set the end of the string.
scanf("%19d", Students[Counter].weight); // Extract 19 characters.  /i can put getchar or fflush, for clearing memory.
}

return 0;
}





hi, here is the updated code. please help me with this. i dont have much time!! :(
hello!
You're not really listening to what Framework is telling you.

name is an array of 20 chars, because it's a string. A name is more than one char, thus it's an array.
Age and weight are just a number. One number. You made those into an array, which is unnecessary.

Then, you set the last character of each array to '\0', which is the termination character for C-strings. Only name is a C-string, the rest is an array of numbers (and should only be a number, so it's wrong doubley).


So, the thing you need is an array of data[]. You already have that. Student[3] will signify the 4th student. Each student has a name (char[20]), age(int) and weight(int). The data struct is correct, don't change it.
but what do i do now??
please! can anyone help!? if i dont change anything, then what do i do? i dont have any time, and this is the last assignment i have to do and thats it. im sorry, but im not getting any responses! :/
hello, can anyone reply? please.
Summarize what Gaminic has said in the last post and then post your updated code based on what he said, then i will help you with the next step. You have to try we cant just do it for you, where is the learning in that.
Topic archived. No new replies allowed.