2

I want to make a program which take Roll No and Full name as input and simply display it My code is . this code skip scaning value of n through gets function. Why this error occur and how to over come this?

 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int r;
 char n[30];
 printf("enter your roll no");
 scanf("%d",&r);
 printf("enter your full name");
 gets(n);
 printf("roll no is %d ",r);
 printf("name is %s ",n);
 getch();
 }

while the below code scan the first gets value and skips the second one.

#include<stdio.h>
#include<conio.h>
void main()
{
 int r;
 char n[30], f[30];
 printf("enter your roll no");
 scanf("%d",&r);
 printf("enter your full name");
 gets(n);
 printf("enter your full name of your father ");
 gets(f);
 printf("roll no is %d ",r);
 printf("name is %s ",n);
 printf("father name is %s ",f);
 getch();
 }
5
  • Use fgets instead of gets Oct 17, 2013 at 10:20
  • You could also use scanf("%s", r). Mind the overflows :) Oct 17, 2013 at 10:24
  • Welcome to Stack Overflow. Please keep in mind that when posting a question, it's essential to actually post some kind of question. What you've posted is a statement and a bit of code, but you've not indicated any kind of problem you're having nor any other form of question. That tends to get posts shut down.
    – mah
    Oct 17, 2013 at 10:28
  • This is the usual problem of scanf leaving the newline in the input buffer, there are probably plenty of duplicates. Oct 17, 2013 at 11:06

6 Answers 6

1

The code DOES NOT skip scanning the value of 'n'. I believe that when you run the program, you enter the Roll No and then press the ENTER key on your keyboard. This is the cause. As soon as you press the ENTER key, the escape sequence '\n' is saved in the array n. Your gets() command is executing perfectly.


In the second case, the variable 'n' stores the escape sequence and the next variable 'f' takes the string you enter next.


To make your code work just enter your scanf statement like this:-

scanf("%d ",&r);

Notice the space after %d.

Try this code-

#include<stdio.h>

int main(void)
{
    int r;
    char n[30], f[30];
    printf("Enter your roll no");
    scanf("%d ",&r);  // I have inserted a space after %d
    printf("Enter your full name");
    gets(n);
    printf("Enter your full name of your father ");
    gets(f);
    printf("\nRoll no is %d ",r);
    printf("\nName is %s ",n);
    printf("\nFather name is %s ",f);
    return 0;
}

TIP:- You must try not to use gets() and puts()

You can read more about it here.

1
  • hi azad, because of a space after %d, the compiler scans the '\n' command as the ENTER key, BUT it does not store it in any variable...thus your variable 'n' can be used. A CODING ADVICE:- Please use mnemonic names, it helps a lot in debugging and understanding the code as well.
    – nikoo28
    Oct 21, 2013 at 5:23
1

The simple solution for the problem is to add fflush(stdin); between scanf(); and gets();

#include<stdio.h>
#include<conio.h>
void main()
{
    int r;
    char n[30],fn[30];
    clrscr();
    printf("\nEnter roll ");
    scanf("%d",&r);
    fflush(stdin);
    printf("\nEnter name ");
    gets(n);
    printf("\nEnter father name ");
    gets(fn);

    printf("\n\nRoll %d",r);
    printf("\nname %s",n);
    printf("\nfather name %s",fn);
    getch();
}
1
0

Using scanf instead of gets will solve your problem:

scanf("%s", n); // Read in your name

Please note that when reading in any string like this you should use safe functions that are passed the length of the string (for example scanf_s from MSDN).

2
  • scanf will only read and store first part of name but i want to read full name including space Jan 24, 2014 at 16:30
  • Gee, you took your time to reply!!
    – noelicus
    Jan 25, 2014 at 10:11
0

I don't know why it gets skipped but what you could do to avoid any other confusion like fflush(stdin) or fgets etc etc.

Just use gets(string) on the next line. So when it skips the first gets command it goes onto the other one.

Try that Cheers, ;)

0

I just had the same problem two hours ago, but to solve this situation easily, all you have to fo is to add a "getchar()" after the "scanf()" and before the "gets()", so that the extra "\n" goes to the "getchar()" and you can type as you want in the next "gets()".

-1

I was also facing the same problem as mentioned above.. so with the help of the answers mentioned here and using hit and trial method, I found that when we press enter after giving input to any variable using scanf(), \n is stored in the next gets() function.. and next time it doesn't take any input from the keyboard.. so to avoid this just use getchar() in between the scanf() nd gets() nd also remember that getchar() takes only 1 character.. so don't give any extra input to scanf() as again this will be stored and will be used in gets() nd the problem will remain the same.... hope this will help.. thank u..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.