Could you check this C language Code... The Output is not complete... it doesnt print the firstclas name n id?
my assignment is Write a complete C program that will meet the following requirement:
• Create student detail must contain each student’s name (as a string), id (as integer) and CGPA (double).
• Create 20 students whose names are student1, studnet1, and so on to student20 whose id and status is assigned randomly.
• Find all ‘first class’ student(s) and print their name and ID.


# include <stdio.h>
int main (void) {

typedef struct
{
char name[20];
double cgpa;
int id;
} Detail;

//This will create a new type for you to use...just like int or char...

//Then you'll need

int student_number = 2; //so we can count
Detail student_records[2]; //holds the students

int i;
int j; //Both for loops

for(i = 0; i < student_number; i++){
printf("\nEnter Student Name: ");
scanf ("%s", &student_records.name);

printf("Enter UserID : ");
scanf ("%d", &student_records.id);

printf("Enter Student CGPA: ");
scanf ( "%f", &student_records.cgpa);

}

//Then you need to loop through to check the data...
for(j = 0; j < student_number; j++){
//Check for the stuff you wanted
if( student_records.cgpa >= 3.6){
printf("The First Class Student Are : %s and his metric id is: %d", &student_records.name, &student_records.id);
}
else{
printf("No one is a first class"); }

}//end for loop


return 0;

}


THANKS IN ADVANCE

Basically your loop only gets the information of 2 students, not of all 20 like you suppose to.
Shouldn't your loop be

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

the same thing to check the GPA. You want to check all the 20 students to see which ones have the GPA higher tha 3.6 am I right?

First up, as a newbie, you need to learn how to post code correctly on these forums using code tags. You'll find that people will be more willing to help you out if you're prepared to show some effort in this area. Please read the information at the following link to learn how to use code tags:
http://www.daniweb.com/forums/thread93280.html

Now moving on to your code. In addition to the anomaly that Samyx has pointed out, here are some other things to consider:
1. When using scanf() with strings, the address-of operator (&) is not required. You're doing this when scanning input for the student names.

2. While on the subject of the address-of operator, you're using them in the for loop that prints out student details - these are not required.

3. You have declared two variables (i and j) for use as loop variables. Only one is required (drop j).

4. You have a bug in the code that prints out first-class student details. You use the variable j in your loop statement and then use the variable i to reference your array elements. As in point 3, drop the j variable and change the code here accordingly.

5. A minor point - you can probably lose the else clause - the requirement only asks that you print the details for those students that are "first-class".

6. Try to avoid the use of "magic" numbers - you should probably declare constants for the number of students and the GPA cut off mark.

commented: Very good suggestion. +17
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.