Program not reading fgets at all [duplicate] - c

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.

Related

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!

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

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

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