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.
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.
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.
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 datan amount of times. Where n is the amount of students. For example:
#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...
}
}
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! :|
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!! :(
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.
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! :/
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.