fgets will be ignored probably due to other scanf's [duplicate] - c

This question already has answers here:
Why scanf("%d", [...]) does not consume '\n'? while scanf("%c") does?
(4 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 6 years ago.
The program won't compile fgets in the teste1 function. Or at least it isn't working properly, it won't let me type the string, the program will end right after it prints "Nome do cliente".
If I disable the other scanf's in the function, it will run without any issue.
How do I make fgets to work?
#include <stdio.h>
void teste1(){
char teste[50];
printf("Nome do cliente\n");
fgets(teste,50,stdin);
}
void teste2(){
teste1();
}
void teste3(){
int opc1,opc2;
printf("\nSeleccione a área desejada\n1- Clientes\n2- Concessionários\n3- Carros de demonstração\n");
scanf("%d",&opc1);
printf("\nSeleccione a área desejada\n1- Inserir\n2- Alterar\n3- Remover\n4- Consultar\n");
scanf("%d",&opc2);
teste2();
}
int main()
{
teste3();
}

The Enter key you pressed for the last scanf input will be left in the input buffer as a newline. The fgets function will read this newline and think it is finished.

Related

Program not reading fgets at all [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Using scanf and fgets in the same program?
(4 answers)
Closed 5 months ago.
#include <stdio.h>
int main(void) {
int numWords;
char str[100];
printf("Enter how many words:\n");
scanf("%d", &numWords);
printf("Enter %d words:", numWords);
fgets(str, 100, stdin);
return(0);
}
This program does not read fgets at all. It will print the text and scan what what the user enters into the numWords, but will not read scanf. I have tried putting a newline character after scanf but then the "Enter %d words:" will not print. I would like everything to print in order.
This is just the beginning of a more complex program, not the whole thing.

In this super easy program written in C won't let me read a variable of type "char"? [duplicate]

This question already has answers here:
can not read character from user [duplicate]
(2 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
This easy code in C won't work, and I don't get it why. If I read only "n", or only "ch" separately, it works, otherwise if I try to read them both, it won't let me read "ch". What happens and how could I make it work?
#include <stdio.h>
int main()
{
int n;
char ch;
printf("n=");
scanf("%d",&n);
printf("ch="); //when i press Build and Run it won't let me read "ch"??? why?
scanf("%c",&ch);
return 0;
}
When you read in a number using %d, a newline is left in the input buffer. When you then read a character with %c, it reads that newline immediately so you don't get prompted for more input.
Unlike the %d format specifier, which discards any leading whitespace, the %c format specifier does not.
Add a leading space before %c to consume any leftover whitespace:
scanf(" %c",&ch);

String cannot be entered [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
After printing "Second text", fgets expect from me to enter a string but a program is always being stopped. That happens when I try to enter a char by scanf or getchar. What's happening?
#include <stdio.h>
int main()
{
char c[100],cc;
int x;
printf("First text\n");
scanf("%d",&x);
printf("Second text\n");
fgets(c,100,stdin);
//scanf("%c",&cc);
//cc=getchar();
printf("\n %s %d",c,x);
}
You probably press "enter" after having entered a number; scanf will then read the number, but will leave a '\n' (i.e. the newline represening "enter") in the buffer; This will be treated as an "empty" line then by gets. (BTW: use fgets instead of gets).
To overcome this enter the number and the text seperated by a space in a single line (i.e. without a newline in between).

scanf() function doesnt work program in c [GNU/LINUX] [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 5 years ago.
Today i have been coding a came across with this code:
Code
#include <stdio.h>
main() {
char letra1;
char letra2;
printf("Primera letra: ");
scanf("%c", &letra1);
printf("Segunda letra: ");
scanf("%c", &letra2);
}
When I execute the code the first scanf() executes well but the second even didn't execute and close the program and I don't know why.
Execution
> ej3
Primera letra: A
Segunda letra:
Thanks for your time guys.
You should use a scanf format string " %c" to skip any pending whitespace characters, including the newline the user typed after the first letter. As posted, the second scanf() reads the \n that is pending in the input stream, so it does not wait for user input.

Using scanf function [duplicate]

This question already has answers here:
Why 2nd scanf doesn't work in my program?
(3 answers)
Closed 8 years ago.
I am try the code below.But when I input the integer,then program does not ask for character.Program execute the printf line.How should i avoid above problem?.
#include <stdio.h>
void main()
{
char a[5];
int p;
printf("data\n");
scanf("%d",&p);
scanf ("%c",&a);
printf("--> %c %d\n",a,p);
}
Put a space in scanf like this:
scanf (" %c",&a);
^-------note
So that the trailing newline is eaten up. Once you hit enter after giving the integer input - there is a trailing newline character in the buffer which the second call to scanf reads. Also main as per ISO should return int
Also this statement is incorrect:
char a[5];
printf("--> %c %d\n",a,p);
You are reading a char and printing an array. You simply need:
char a;
printf("--> %c %d\n",a,p);
If you want to read (or take input) array of chars then use fgets. For char a[5] do something like:
fgets (a, 5 , stdin)
Since fgets is buffer safe.

Resources