C Language, scanf issue with char's [duplicate] - c

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
I am using c to create a very basic programme. I using scanf to get input from the console but when I use it with a char it just seems to 'skip' it.
Here is the code:
#include <stdio.h>
#include <string.h>
int main(){
char name[20];
char yn;
printf("Welcome to Benjamin's first C programme! \n\n");
do{
printf("What is your name? \t");
scanf("%s", name);
printf("\n");
printf("Is your name %s [y/n]?", name);
scanf("%c", &yn);
printf("\n");
} while(yn != 'y');
}

Add space at the begining of the string format in the second scanf()
scanf(" %c", &yn);
This space will catch the newline that you type after the first input

Your apparent skipping is because %s specifier matches a string of characters, until a whitespace or a newline character. In this case, it is a newline. The problem is that it leaves it in the input stream, and the next reading matches it. So, in your yn variable, there will always be the newline character. In order to prevent this, insert a space before %c specifier, to skill all blanks (whitespace, newline, tab):
scanf(" %c", &yn);
Also note that for the same reason, your program will not work if I insert my full name (i.e. I insert spaces).

As an alternative, try using getchar()
Replace:
scanf("%c", &yn);
With:
getchar();//eat the '\n' from <return>
yn = getchar();
For the record I like the other answers better, just offering this as an alternative...

Related

This C program keeps skipping the line without taking the value from user [duplicate]

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

Why am I unable to enter an input for my last scanf? [duplicate]

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

Scanf does not work with multiple inputs on different lines [duplicate]

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

When I try to put an intentional letter that it hasn't got any option it output 3 errors, but It may be put one error [duplicate]

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

Unexpected behavior of scanf , asks two variable values at the same time [duplicate]

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

Resources