Reading entire line from console [duplicate] - c

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

Related

C program won't use printf after scanf [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 2 days ago.
I am new to C and am writing a very simple C program which just provides a input and repeats it back to you. The code shows the first print f which is a $ and lets you input something but will not print the text back to you. Here is the code:
char input[50];
printf("$");
scanf("%s\n", input);
printf("\n %s", input);
I thought it might have been a compile issue but nothing changes. I use make token which is the name of the file.
Remove the "\n" in the scanf() format string. The trailing "\n" instructs scanf() to match any number of white space characters and it will only know when it's done when encountering a non-white space character. Meanwhile you expect it to return after reading the first "\n". Consider using fgets() instead.
#include <stdio.h>
int main() {
char input[50];
printf("$");
scanf("%s", input);
printf("\n %s", input);
}
and example session:
$abc
abc

Is it possible to use scanf() and getchar() in the same program to get input? [duplicate]

This question already has answers here:
The program doesn't stop on scanf("%c", &ch) line, why? [duplicate]
(2 answers)
Closed 1 year ago.
I'm struggling on a question proving scanf() and getchar() can both retrieve a character from the input.
However, when I try to put them inside the same program, only the first function is running properly. The latter is discarded completely.
#include <stdio.h>
char letter;
int main()
{
printf("I'm waiting for a character: ");
letter = getchar();
printf("\nNo, %c is not the character I want.\nTry again.\n\n",letter);
printf("I'm waiting for a different character: ");
scanf("%c",&letter);
printf("Yes, %c is the one I'm thinking of!\n",letter);
return(0);
}
output
I have tried switching the places of those two functions but it is of no use.
Can someone help me find the issue and provide a way to fix this? The only requirement is that the program takes input twice, once by the getchar() function and once via scanf()
The second read attempt just reads whitespace (the end of line character, since you pressed enter after the first letter). Simply replace it with this:
scanf(" %c", &letter);
The space before % will tell scanf to read the next non-whitespace character.

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.

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.

How to stop a program from storing more than a single character into a char variable? [duplicate]

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.

Resources