This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 4 years ago.
In the code below, after the scanf() for newMIn it skips the gets() for newLname. I've tried relocating it to other parts but it still skips, I've also tried using getchar() instead of scanf() but it still skips the next immediate function. All other variables are strings, and this function is part of a larger program.
void getINF(char *newStID, char *newLname, char *newMIn,char *newFname, char *newHomeAdd, char *newCourse, char *newMumNam,char *newPopsNam ){
printf("Input Student ID:\n");
gets(newStID);
printf("Input First Name:\n");
gets(newFname);
printf("Input Middle Initial:\n");
scanf(" %c", &newMIn);
printf("Input Last Name:\n");
gets(newLname);
printf("Input Home Address:\n");
gets(newHomeAdd);
printf("Input Course:\n");
gets(newCourse);
printf("Input Name of Mother:\n");
gets(newMumNam);
printf("Input Name of Father:\n");
gets(newPopsNam);
printf("\n\n%s\n",newStID);
printf("%s\n",newFname);
printf("%c\n",newMIn);
printf("%s\n",newLname);
printf("%s\n",newHomeAdd);
printf("%s\n",newMumNam);
printf("%s\n",newPopsNam);
printf("%s\n",newCourse);
return;
}
When using the %c token with scanf, it will not consume any extra whitespace of newline characters. As such, the newline character after pressing enter is left in the input buffer and then consumed by the following gets call.
Use this popular method to clear the input before the next newline:
int c;
while ((c = getchar()) != '\n' && c != EOF);
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
the program doesnt stop on scanf("%c", &ch) line. why does it happens sombody can please explain this to me
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}
Let's say you input 2 when you're reading for num. The actual input stream will be 2\n (\n is the newline character). 2 goes into the num, and there remains \n, which goes into ch. To avoid this, add a whitespace in format specifier.
scanf(" %c", &ch);
This will ignore any whitespaces, newlines or tabs.
The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement
scanf("%c", &ch);
executed then it reads that \n left behind by the previous scanf.
To eat up this \n you can use a space before %c specifier. A space before the %c specifier is able to eat up any number of white-space characters.
scanf(" %c", &ch);
^ a space
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
the program doesnt stop on scanf("%c", &ch) line. why does it happens sombody can please explain this to me
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}
Let's say you input 2 when you're reading for num. The actual input stream will be 2\n (\n is the newline character). 2 goes into the num, and there remains \n, which goes into ch. To avoid this, add a whitespace in format specifier.
scanf(" %c", &ch);
This will ignore any whitespaces, newlines or tabs.
The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement
scanf("%c", &ch);
executed then it reads that \n left behind by the previous scanf.
To eat up this \n you can use a space before %c specifier. A space before the %c specifier is able to eat up any number of white-space characters.
scanf(" %c", &ch);
^ a space
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
the program doesnt stop on scanf("%c", &ch) line. why does it happens sombody can please explain this to me
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}
Let's say you input 2 when you're reading for num. The actual input stream will be 2\n (\n is the newline character). 2 goes into the num, and there remains \n, which goes into ch. To avoid this, add a whitespace in format specifier.
scanf(" %c", &ch);
This will ignore any whitespaces, newlines or tabs.
The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement
scanf("%c", &ch);
executed then it reads that \n left behind by the previous scanf.
To eat up this \n you can use a space before %c specifier. A space before the %c specifier is able to eat up any number of white-space characters.
scanf(" %c", &ch);
^ a space
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
the program doesnt stop on scanf("%c", &ch) line. why does it happens sombody can please explain this to me
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}
Let's say you input 2 when you're reading for num. The actual input stream will be 2\n (\n is the newline character). 2 goes into the num, and there remains \n, which goes into ch. To avoid this, add a whitespace in format specifier.
scanf(" %c", &ch);
This will ignore any whitespaces, newlines or tabs.
The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement
scanf("%c", &ch);
executed then it reads that \n left behind by the previous scanf.
To eat up this \n you can use a space before %c specifier. A space before the %c specifier is able to eat up any number of white-space characters.
scanf(" %c", &ch);
^ a space
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
the program doesnt stop on scanf("%c", &ch) line. why does it happens sombody can please explain this to me
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}
Let's say you input 2 when you're reading for num. The actual input stream will be 2\n (\n is the newline character). 2 goes into the num, and there remains \n, which goes into ch. To avoid this, add a whitespace in format specifier.
scanf(" %c", &ch);
This will ignore any whitespaces, newlines or tabs.
The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement
scanf("%c", &ch);
executed then it reads that \n left behind by the previous scanf.
To eat up this \n you can use a space before %c specifier. A space before the %c specifier is able to eat up any number of white-space characters.
scanf(" %c", &ch);
^ a space