Reading multiple character arrays in C using scanf [duplicate] - c

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

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.

Why is fgets() not taking input in C program? [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 3 years ago.
I wrote a code to implement Julias Caesar Cryptography in C.
The problem is that my fgets() is not waiting for input and skipping to fputs().
#include<stdio.h>
int main(){
int size;
printf("Enter the size of the string: ");
scanf("%d",&size);
int key;
printf("Enter the key: ");
scanf("%d",&key);
char a[size];
printf("Enter the string: ");
fgets(a,size,stdin);
for(int i=0;i<size;i++){
a[i]+=key;
}
fputs(a,stdout);
return 0;
}
I used CodeBlocks 17.12 to compile this C program.
My OS is Windows 7 (32-bit)
Welcome to stackoverflow.
When you enter your answer, there is a newline char at the end (\n) in the buffer. When fgets() reads your input it reads the newline to. You can remove the newline, or use a regex to skip it, or fgets() once on the line so that you can use scanf() once more as suggested in this other answer that may help you.
And, please, remember to search in stackoverflow before posting a question!

C: program skips fgets and fflush doesn't really do the job [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Using fflush(stdin)
(7 answers)
Closed 3 years ago.
The program I was trying to create was supposed to show a menu and store book details (in a structure), delete and search them. However, it seems to ignore all fgets functions. The problem probably lies in the scanf function which I used at the beginning of the code. I tried to use fgets and then atoi, but it only made my program stop working right after choosing one of the options. fgetc does not really help either (but perhaps I used it incorrectly). I do not get any error messages though.
Here is a piece of the problematic code:
struct books
{
char title[20];
char author[20];
char year[10];
char pages[10];
};
int main()
{
struct books b[20];
int limit = 0;
int temp;
int choice;
do
{
printf("1 - Add a book\n2 - Delete a book\n3 - Search by year of publication\n4 - Search by author\n5 - Exit\n");
scanf("%d", &choice);
fflush(stdin);
if (choice == 1)
{
limit++;
printf("Title:\n");
fgets(b[limit].title, strlen(b[limit].title), stdin);
printf("Author:\n");
fgets(b[limit].author, strlen(b[limit].author), stdin);
printf("Year of publication:\n");
fgets(b[limit].year, strlen(b[limit].year), stdin);
printf("Number of pages:\n");
fgets(b[limit].pages, strlen(b[limit].pages), stdin);
printf("Added a book.\n");
}
Thank you all in advance.

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

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)

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