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();
 }
Azad Hussain
  • 141
  • 1
  • 6
  • 19
  • Use `fgets` instead of `gets` – digital_revenant Oct 17 '13 at 10:20
  • You could also use `scanf("%s", r)`. Mind the overflows :) – Johan Henriksson Oct 17 '13 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 '13 at 10:28
  • This is the usual problem of scanf leaving the newline in the input buffer, there are probably plenty of duplicates. – Matteo Italia Oct 17 '13 at 11:06
  • possible duplicate of [Does scanf() take '\n' as input leftover from previous scanf()?](http://stackoverflow.com/questions/1815986/does-scanf-take-n-as-input-leftover-from-previous-scanf) – Jonathan Leffler Dec 22 '13 at 21:29

6 Answers6

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.

Community
  • 1
  • 1
nikoo28
  • 2,961
  • 1
  • 29
  • 39
  • 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 '13 at 05: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();
}
Azad Hussain
  • 141
  • 1
  • 6
  • 19
  • 1
    [This causes undefined behavior and is *not* recommended.](https://stackoverflow.com/questions/2979209/using-fflushstdin) – Arthur Dent May 15 '18 at 03:55
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).

noelicus
  • 14,468
  • 3
  • 92
  • 111
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()".

Unheilig
  • 16,196
  • 193
  • 68
  • 98
MoUaD_3
  • 5
  • 1
-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..

mnille
  • 1,328
  • 4
  • 16
  • 20
Abhinav
  • 21
  • 6