C/Array of Structure
Expert: Narendra - 10/11/2004
Question-------------------------
Followup To
Question -
Dear Sir,
I am finding a problem while i am declareing an array of structure. At the time of scaning the elements, the program treminates and giving the "floating point" related problem. The structure that i have defined is:
struct student
{
int roll_no;
char name[50];
float age;
float per_marks;
} stu[10];
After taking input in stu[0].name, the program terminates.
Please tell me what is the reason behind it and what is the solution of it? I am waiting for your reply.
With Regards
Raj Kumar Gupta
Answer -
> After taking input in stu[0].name, the program terminates.
If you show the code of reading data, I can try to help you.
Without that I don't see any problem with your structure.
In the scanf(), check if you are sending the address of stu[0].age ?
-ssnkumar
Dear Sir,
The code is:-
#include<stdio.h>
#include<conio.h>
struct test
{
int x;
char name[100];
float marks;
};
int main(void)
{
int i;
struct test p[2];
for(i=0;i<2;i++)
{
clrscr();
printf("Enter The Roll No. : ");
scanf("%d",&p[i].x);
fflush(stdin);
printf("Enter The Name : ");
scanf("%[^\n]s",p[i].name);
fflush(stdin);
printf("Enter The percentage marks: ");
scanf("%f",&p[i].marks);
}
for(i=0;i<2;i++)
printf("\nRoll No of \"%s\" is %d, getting %5.3f % marks.",p[i].name,p[i].x,p[i].marks);
getch();
return 0;
}
I am running this program with Borland 3.1 Compiler
AnswerI have changed your code. It works now.
Here is the code:
#include <stdio.h>
#include <conio.h>
struct test
{
int x;
char name[100];
float marks;
};
int main(void)
{
int i;
struct test p[2];
for(i = 0; i < 2; i++)
{
clrscr();
printf("Enter The Roll No. : ");
scanf("%d", &p[i].x);
fflush(stdin);
printf("Enter The Name : ");
scanf("%s", p[i].name);
fflush(stdin);
printf("Enter The percentage marks: ");
scanf("%f", &p[i].marks);
}
printf("\n");
for(i = 0; i < 2; i++)
{
printf("Roll No of \"%s\" is %d, getting %5.3f marks.\n", p[i].name, p[i].x, p[i].marks);
}
getch();
return 0;
}
I have got one very important suggestion for you. Indent your code properly and keep it as simple as possible.
This will help you to debug the code easily and fix problems. Also number of problems become less.
Hope this helps.....
-ssnkumar