While loop comparisons of strings/characters with fgets [duplicate] - c

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)

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.

Reading entire line from console [duplicate]

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);

question about reading an input using fgets and scanf in c [duplicate]

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.

Reading multiple character arrays in C using scanf [duplicate]

This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Loop skips a scanf statement after the first time
(1 answer)
Closed 4 years ago.
char a[100],b[100],c[100];
scanf("%[^\n]",a);
printf("%s",a);
scanf("%[^\n]",b);
printf("%s",b);
The compiler seems to be reading the first read but skips the second read. Why is this happening?
Because of un handled Enter
Use fgets()
Try this :-
char a[100], b[100], c[100];
fgets(a, 100, stdin);
printf("%s", a);
fgets(b, 100, stdin);
printf("%s", b);

Stop fgets after 5 seconds in C [duplicate]

This question already has answers here:
Set a timeout for reading stdin
(3 answers)
Closed 6 years ago.
Hi I am making a program that prompts the user using fgets such as.
char* option = malloc(64);
fgets(option, 60, stdin);
I'm looking for a way to stop this fgets and print something out if the user takes to long to input a command.
Thanks
use
fflush(stdin);
gets(option);
it will work :)

Resources