Why Does My C Program Skip Input Fields?

In summary, the problem is that the gets and puts functions are intrinsic functions and the compiler will code in the next lines of instruction for the machine to operate.
  • #1
Erised
3
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
  • #2
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.
 
  • #3
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.
 
  • #4
@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..
 
  • #5
@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..
 
  • #6
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.
 
  • #7
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:

FAQ: Why Does My C Program Skip Input Fields?

What is C programming and why is it important?

C programming is a high-level programming language that is widely used in the development of various software and applications. It is important because it is a powerful and efficient language that allows for the creation of complex programs and systems.

I am new to C programming, where do I start?

If you are new to C programming, it is recommended to start with a basic understanding of the language's syntax and structure. There are many online tutorials and resources available to help you get started, and it is also beneficial to practice writing simple programs to gain a better understanding of the language.

How do I approach solving a C programming problem?

The best approach to solving a C programming problem is to break it down into smaller, more manageable tasks. Start by understanding the problem and identifying the key steps needed to solve it. Then, use your knowledge of the language to write code that addresses each step, testing and debugging as you go.

What are some common challenges in C programming?

Some common challenges in C programming include managing memory, handling errors, and dealing with pointers. As C is a low-level language, it requires more attention to detail and can be prone to errors if not carefully managed. Practice and experience can help overcome these challenges.

How can I improve my skills in C programming?

The best way to improve your skills in C programming is to practice regularly and challenge yourself with new and more complex problems. Reading code written by others and learning from their techniques can also be beneficial. Additionally, staying updated with the latest advancements and techniques in C programming can help you become a better programmer.

Back
Top