Why Does My C Program Skip Input Fields?

AI Thread Summary
The discussion centers around a C programming issue where a user is unable to correctly input student names and scores in a loop. The original code uses `gets()` to read names, which leads to unexpected behavior when combined with `scanf()` for scores. The program prompts for the number of students, but when it asks for names, it simultaneously requests scores, causing confusion and incorrect output.Key points include the recommendation to replace `gets()` with `scanf("%s", name)` to properly capture names, although this method does not support multi-word names. Users suggest avoiding mixing `scanf()` and `gets()` due to potential issues with input handling. The conversation also touches on the importance of using the correct syntax with `scanf()` and mentions the risks of buffer overflow. Suggestions for improvement include using `sscanf()` for safer input handling and considering the use of structures for better data management. The discussion highlights the complexities of input handling in C and the need for careful coding practices to avoid common pitfalls.
Erised
Messages
3
Reaction score
0
Problem in C, please help :)

Hello all, So I'm learning C nowadays and I got stuck at this rather simple program. I have no idea what the problem in the program is so I hope someone can help.

The program is for reading the number of students in a class, likewise reading their names and scores in sci, math and english and then calculating the average and displaying the names and avg.#include<stdio.h>
#include<conio.h>

main()
{
char name[25];
float Sci, Math, Eng, average;
int n,i;
printf("Enter the number of students\n");
scanf("%d",&n);

for(i=0; i<n; i++)
{
puts("\nEnter name");
gets(name);
printf("\nScience=");
scanf("%f",&Sci);
printf("\nMath= ");
scanf("%f",&Math);
printf("\nEnglish= ");
scanf("%f",&Eng);
average=(Sci+Math+Eng)/3;
puts("\n\nName:");
puts(name);
printf("\nAverage Score= %f", average);
}
getch();
}

Now the problem is-
When i run the program, it first asks me to enter the no of students, after that it prints 'enter name' and 'science=' at the same time instead of waiting for me to enter the student's name in response to the 'enter name'.

So I only have the option of entering the scores and finally it displays 'Name:' and a blank line followed by the average.Another program with a for loop only for gets and puts works fine for a specified number of students works(I removed the score and average part), the problem comes in when i put the 'enter the number of students' statement and make the program for 'n' students instead of using a number there.

I'm using Dev C++ for programming, please help me out if anyone knows what the problem is. Okay I tested it on Turbo C++ IDE and it still shows the same problem.
 
Last edited:
Technology news on Phys.org
Hi...ı have changed ur gets statemant..please check ıt..ıt works now well...

Please chck code below..ı have modıfıed ıt..
Erised said:
Hello all, So I'm learning C nowadays and I got stuck at this rather simple program. I have no idea what the problem in the program is so I hope someone can help.

The program is for reading the number of students in a class, likewise reading their names and scores in sci, math and english and then calculating the average and displaying the names and avg.


#include<stdio.h>
#include<conio.h>

main()
{
char name[25];
float Sci, Math, Eng, average;
int n,i;
printf("Enter the number of students\n");
scanf("%d",&n);

for(i=0; i<n; i++)
{
puts("\nEnter name");
scanf("%s",&name);
printf("\nScience=");
scanf("%f",&Sci);
printf("\nMath= ");
scanf("%f",&Math);
printf("\nEnglish= ");
scanf("%f",&Eng);
average=(Sci+Math+Eng)/3;
puts("\n\nName:");
puts(name);
printf("\nAverage Score= %f", average);
}
getch();
}

Now the problem is-
When i run the program, it first asks me to enter the no of students, after that it prints 'enter name' and 'science=' at the same time instead of waiting for me to enter the student's name in response to the 'enter name'.

So I only have the option of entering the scores and finally it displays 'Name:' and a blank line followed by the average.


Another program with a for loop only for gets and puts works fine for a specified number of students works(I removed the score and average part), the problem comes in when i put the 'enter the number of students' statement and make the program for 'n' students instead of using a number there.

I'm using Dev C++ for programming, please help me out if anyone knows what the problem is. Okay I tested it on Turbo C++ IDE and it still shows the same problem.
 
Yeah, the above post got it.

puts, and gets are intrinsic functions, and unless you put a mechanism to pause the compiler will code in the next lines of instruction for the machine to operate.

scanf, which normally interprets from the terminal, will automatically pause for input. It's part of its defined function.

There's a funct. that pauses, but I can't recall it right now. And I don't have my reference handy.

And the hacks are a lurchin' and I can't even pull off a decent search. Dreaded dead-end beatniks at the beck and call, but that's my prob. I guess.

gets, though, is really designed to pull from another data stream than the std kbd input. Like a data file. Where it really shines, in its concise handling, and speed thereof.
 
@brk235,
Shouldn't it be scanf("%s",name); without the ampersand

Anyway, this wouldn't allow me to enter a proper full name like 'John Smith'. When I executed the code, it let me enter the name but not the scores in the first loop :-/

The output looked like this:

Enter the number of students:
3

Enter Name
John Smith

Science=
Math=
English=

Name:
John

Average=some garbage value

Enter name
(doesn't let me type the string)
Science=
20
Math=
20
English=
20[/U]

Name:
Smith (from the string that I entered the first time)

Average =20.000
Enter Name
___

Thanks for helping..
 
@delta_moment

Is system("pause"); the command you're talking about? Hmm it doesn't work. The gets(name); isn't being executed by the compiler. It just skips that statement:/

But a simple program like the one below works just fine.
main()
{
char name[25]
int i;
for(i=0;i<3,i++)
{
puts("Enter name");
gets(name);
puts("Name:");
puts(name);
}
getch();
}

Thanks for helping..
 
Could be your compiler, or configuration, or platform.

You may have to point the gets statement at the std. input. I don't know.

There's so many variables these days.

From looking at the code in the latter post, the problem appears when you combine scanf and gets. I'd just say you're throwing the compiler.
It's easy to do, I do it all the time. Just remember, that when a C program is compiled, it's converted to machine language, and the memory/drive/CPU access is all dynamic. Among many other data transfers. And, it's easy to trip out the computer, or yourself.

Some compilers will catch things for you and make assumptions, others will not.
I'm not sure, but since the combination seems to be what's throwing things, for now, I'd just avoid it, and stick to one or the other.
 
This works:

Code:
#include<stdio.h>

main()
{
        char name[25];
        float Sci, Math, Eng, average;
        int n, i;
        printf("Enter the number of students:  ");
        scanf("%d",&n);

        for(i=0; i < n; i++)
        {
                printf("\nEnter name: ");
                scanf("%s", name);

                printf("\nScience: ");
                scanf("%f", &Sci);

                printf("\nMath: ");
                scanf("%f", &Math);

                printf("\nEnglish: ");
                scanf("%f", &Eng);

                average=(Sci+Math+Eng)/3;
                puts("\n\nName:");
                puts(name);
                printf("\nAverage Score = %f\n", average);
        }
}

Whoever said you don't need to use an ampersand when using scanf with an array is correct, as 'number' is already a pointer to number[0]. Ampersands says the address of n, which gives scanf the address of n. Scanf is a bad function to use to, as it's possible to overflow buffers. Try using sscanf as you can specify input length, format etc.

http://www.cplusplus.com/reference/clibrary/cstdio/sscanf.html

Code:
Enter the number of students:  12

Enter name: Karl

Science: 13.1

Math: 11.1

English: 2.2Name:
Karl

Average Score = 8.800000

The next thing to learn, would be to use a struct to do the same thing.
 
Last edited:
Back
Top