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!
Related
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.
This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 1 year ago.
CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
struct mobile{
char N[10];
int ram, pixel, price;
}B[5];
int min;
char trash;
for(int i = 0; i < 5; i++){
printf("Enter Mobile Name: ");
fgets(B[i].N, sizeof(B[i].N), stdin);
printf("Enter features (ram/camera pixels/price): ");
scanf("%d%d%d", &B[i].ram, &B[i].pixel, &B[i].price);
printf("\n");
}
}
The program is not accepting value for name of mobile second time. It prints Enter mobile name but don't take value then print Enter features and ask for value. I tried adding a second scanf above printf("\n"); but didn't work. Help please. Thanks.
Remove \n from buffer
scanf leaves a newline in the buffer, which is then read by fgets. The other problem is, that you aren't dividing the user input using a delimiter so I would put a space or a slash between the type specifiers %d:
scanf("%d/%d/%d\n", &B[i].ram, &B[i].pixel, &B[i].price);
The input should then be something like this:
Enter specs (ram/pixels/price): 8/12/500
The trailing character \n is now being read, but it isn't stored in any variable.
Remove \n from fgets() input
This doesn't cause your problem, but I would also remove the trailing \n from the fgets() input, because it's probably not supposed to be part of the phone's name.
#include <string.h>
fgets(B[i].N, sizeof(B[i].N), stdin);
B[i].N[strcspn(B[i].N, "\n")] = '\0';
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
I am a newcomer to programming. My English is bad so I used google translate to ask this question:
I'm practicing using input/output in C language. I wrote a C program that takes a file called sample.txt and writes a few lines of words in it. I feel that the program that I made is correct but I found an error when compiling the program.
#include <stdio.h>
int main(){
FILE *fptr;
int n,I;
char text[100];
fptr=fopen("sample.txt","w");
printf("Number of line? ");
scanf("%d",&n);
printf("\n");
for( i = 1; i <= n; i++){
printf("Line %d: ", I);
fgets(text, sizeof(text), stdin);
fputs(text, fptr);
}
fclose(fptr);
return 0;
}
This is the output :
enter image description here.
enter image description here
Why line 1 and 2 are not separate like the others? I've checked several times and I don't know what went wrong.
As already pointed out by people in comments, you need to handle the newline left by scanf so that it doesn't get consumed by fgets() afterwards. You can either add a getchar() after scanf to handle this:
printf("Number of line? ");
scanf("%d",&n);
getchar();
Or you can replace usage of scanf for taking input with fgets() followed by a sscanf:
char input[10];
...
fgets(input, 10, stdin);
sscanf(input, "%d", &n);
When I made this changes, I was able to see desired output:
c-posts : $ ./a.out
Number of line? 3
Line 1: Foo
Line 2: Bar
Line 3: Pong
c-posts : $ cat sample.txt
Foo
Bar
Pong
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.
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
I have a function that asks the user for user variable and user number.
void numReplace(char infix[50])
{
char userVar;
int userNum;
printf("Please enter the variable you want to change\n");
scanf("%c", &userVar);
printf("Please enter the replacement value for the variable\n");
scanf("%d", &userNum);
printf("%c %d", userVar, userNum);
int i=0;
char chrr;
infix[50] = '\0';
while((chrr=infix[i++])!='\0')
{
if (chrr == userVar){
chrr = userNum;
}
}
}
when running the program I should be asked the userVar and userNum. However the output is:
Please enter the variable you want to change
Please enter the replacement value for the variable
1
1
It only takes in one variable, I don't see a problem with my codes. Can someone help me?
Try adding a getchar(); after every call to scanf().
It's a workaround to the dangling newline character after you press <enter> when using scanf().
See this FAQ for more info.
This uservar may have received the \n you entered last time. If you have input before this input, please accept the newline with getchar().