This question already has answers here:
How to read from stdin with fgets()?
(6 answers)
Closed 2 years ago.
void name(record *el)
{
char k[100];
printf("Name: ");
fgets(k,100,stdin);
printf("\n");
}
I'm trying to write a function that reads a line from console and then searches for it in a list. I'm having some problem with reading a whole line. The program just skips the fgets line. The same happens if i try with scanf("%[^\n]%*c").
You probably read an integer before calling the function and the '\n' was left on the input stream.
Try calling the function as the first command you do in main and you will see that it works.
If you read a number right before the call with scanf for example, you could add a '\n' in the parameter string before calling the fgets:
scanf("%d\n", &x);
This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 3 years ago.
I have recently been trying to do an exercise in c.
I want to read the input which is something like: "SET 0" (note that the actual text will be parsed later).
I tried fgets as it is like that:
char in[20];
//ok, this reads the first line, the first input is meant to be a number
scanf("%s",in);
if(isdigit(in[0])){
char array[]={'?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?'};
auto counter = atoi(in);
while(counter !=0){
fgets(in, sizeof(in),stdin);
For some reason, when i type eg "SET 0" and i am using fgets the in variable is empty(will print nothing).
I tried scanf but it will not read the number.
Any ideas/suggestions of what i can do?
Thanks in advance!
Do not mix fgets() with scanf() until you know why scanf()` is evil.
fgets() read the left over '\n' of the first line of user input which scanf() did not read.
This question already has answers here:
Reading a single character in C
(5 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 5 years ago.
I'm requesting an input inside a loop that looks like this:
for (i = 0; i <= 8; i ++)
{
scanf("%c",&a);
printf("%c",a);
printf("This is just for show\n");
}
The output on my console is, while having the user Input "A" followed by an "enter" to set the input:
A
AThis is just for show
This is just for show
Afterwards it´s awaiting a new input, so basically my char variable has once the value of "A" and once of "enter". Can you guys point me on the right track on how can only read in the only first input ("A") and don´t get the "enter" as my second input in the loop?
This question already has answers here:
If statement being ignored in main function [duplicate]
(2 answers)
Closed 7 years ago.
I am trying to get user input with fgets in C and am having trouble using the while loop.
Here is the code I am using:
char input[300];
fgets(input, 300, stdin);
while(strcmp("Quit", input) != 0) {
fgets(input, 300, stdin);
}
When I enter Quit the loop continues and does not terminate and I don't understand why this is so.
fgets() stores the new line character in input as well. Either remove it manually, or compare like this:
while(strcmp("Quit\n", input) != 0)
This question already has answers here:
How to prevent scanf causing a buffer overflow in C?
(6 answers)
Closed 8 years ago.
My program uses a scanf as such:
scanf ("%c", &symbol);
is there a way to print an error if the user enters in a string > one character? e.g "abc" as it messes with the program later on
Use a string buffer, fgets() into it, check if the second character is a \n.