I just want to enter of 5 students data in structure but this loop is not running.
pls help me out

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[20];
int age;
float marks;
} a[5];
main()
{
int i;
clrscr();
for(i=0; i<5; i++)
{
printf("Enter the name of student");
gets(a[i].name);
printf("Enter the age & marks of student");
scanf("%d", &a[i].age,&a[i].marks);
printf("Enter the marks of student");
scanf("%f", &a[i].marks);
}
for(i=0; i<5; i++)
{
puts(a[i].name);
printf("%d", a[i].age);
printf("%f", a[i].marks);
}
getch();
}

Recommended Answers

All 11 Replies

  1. You shouldn't really use gets().

  2. You're mixing scanf() and gets(). It's possible, but not recommended. Keep in mind that gets() looks for a newline (or eof) while scanf() tries to obtain the minimal and will keep the newline in the stream. (in this case) You could place a "fflush(stdin)" at the start of your loop although I believe technically this is undefined as you're applying it on an input stream. You could read characters until (and including) the newline left by scanf before your next gets() too, but the difference is probably not all that relevant to you at the moment..

    scanf("%d", &a[i].age,&a[i].marks);
    printf("Enter the marks of student");
    scanf("%f", &a[i].marks);

  3. This is a bit confusing and looks like the result of you having tried multiple things. I assume the first line is an error you can identify yourself as well.

As far as "loop not running" goes, you'd have to elaborate on what you mean by that exactly..

"Not running" is not helpful. But because you've stumbled on a very common problem of mixing formatting and unformatted input, it's easy to guess what the real problem is. Replace this line:

gets(a[i].name);

With this:

while (!gets(a[i].name) || !a[i].name[0])
    ;

The problem is that gets() will stop reading when it encounters a newline character, but scanf() will tend to leave whitespace (which includes a newline character) in the stream. Thus if you call scanf() followed by gets(), there's a good chance that the gets() call will appear to be skipped because it's terminating successfully on the newline character.

By looping over the gets() call as long as it either fails or results in an empty string, you avoid that particular error. The above loop won't work very well if gets() fails due to a stream error, but that's a very rare occurrence anyway.

Your code also has a bunch of bad practices, but I'll let others point them out and explain why they're bad as repeating the same thing over and over for years takes its toll. But here's the fixed code (without error handling, to keep it from getting too bulky), just for review sake:

#include <stdio.h>

#define MAX_NAME     20
#define MAX_STUDENTS 2

struct student {
    char name[MAX_NAME];
    int age;
    float marks;
} a[MAX_STUDENTS];

int main(void)
{
    int i;

    for (i = 0; i < MAX_STUDENTS; i++) {
        printf("Enter the name of student: ");
        fflush(stdout);

        while (!fgets(a[i].name, sizeof a[i].name, stdin) || a[i].name[0] == '\n')
            ;

        printf("Enter the age of student: ");
        fflush(stdout);
        scanf("%d", &a[i].age);

        printf("Enter the marks of student: ");
        fflush(stdout);
        scanf("%f", &a[i].marks);
    }

    for (i = 0; i < MAX_STUDENTS; i++) {
        fputs(a[i].name, stdout);
        printf("\t%d\n", a[i].age);
        printf("\t%f\n", a[i].marks);
    }

    return 0;
}

yeah tht was mine mistake scanf("%d", &a[i].age,&a[i].marks); in this line
it will be
scanf("%d", &a[i].age);
but still it is taking only two value

yeah tht was mine mistake scanf("%d", &a[i].age,&a[i].marks); in this line
it will be
scanf("%d", &a[i].age);
but still it is taking only two value

That was a mistake, but it's a benign mistake. If there are more variable arguments than format specifiers, the extra arguments will be ignored. So both of those calls to scanf() will only read the age. This is well defined behavior, though the code will still be confusing to readers because of the mismatch.

"Not running" is not helpful. But because you've stumbled on a very common problem of mixing formatting and unformatted input, it's easy to guess what the real problem is. Replace this line:[...]

this is also running only single time

this is also running only single time

It will read and display two students. If you made no changes to the code then you're doing something wrong with the input at runtime. There's also the possibility that your Turbo C compiler is doing something stupid, but that's very unlikely with such simple code.

Please give exact details about how you're running the program. What do you type, what output do you see, etc...

I just press enter after it for enter marks n it's terminated

It's not terminated, it's waiting for valid input. You told scanf() that you're going to type a float, and then you don't type a float. What were you expecting to happen?

while (!fgets(a[i].name, sizeof a[i].name, stdin) || a[i].name[0] == '\n')

In this line It is asking for valid name which I entered "Nitesh"

scanf("%d", &a[i].age);

In this Line It is asking for valid age which is a integer value I enetered "24"

Now I press Enter for execute next line where ur program will ask for valid marks thn whts wrong m doing here?

It is just terminating after pressing enter just after 24

& Error which is coming:
"Floating Point Formats Not Linked Abnormal Program Termination"

"Floating Point Formats Not Linked Abnormal Program Termination"

That's a fairly common Borland error. There's nothing wrong with the code, your compiler is just stupid in it's assumptions about the environment for unnecessary efficiency purposes.

Ideally you should go into your linker options and include floating point support so that the compiler doesn't have to make assumptions.

Click here for further information.

thnks buddy it's sorted out now

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.