Fgets not working properly and something unusual happens with it [duplicate] - c

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years ago.
I want to get rid of buffer overflow and I am using fgets instead of scanf to limit the characters. But whenever I enter something with scanf before fgets, it doesn't work correctly anymore.
This code works correctly
#include <stdio.h>
int main()
{
char name[10];
printf("Who are you? \n");
fgets(name,10,stdin);
printf("Good to meet you, %s.\n",name);
return(0);
}
This code does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?

Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.

Related

Why my Second scanf() isn't working in C++ [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 6 months ago.
I tried to use a string instead of a char but the string works fine, I'm still confused why can't I use a char for the 2nd scanf
#include <stdio.h>
#include <stdlib.h>
void main()
{
char customer_code, service;
printf("\n\nConsumer Type: ");
scanf("%c", &customer_code);
printf("\nis this your first time using our services? [Y/N] ");
scanf("%c", &service);
}
try it
#include <stdio.h>
#include <stdlib.h>
int main()
{
char customer_code, service;
printf("\n\nConsumer Type: ");
scanf("%c", &customer_code);
getchar();
printf("\nis this your first time using our services? [Y/N] ");
scanf("%c", &service);
}

Cannot understand how loop is running here [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years ago.
I want to get rid of buffer overflow and I am using fgets instead of scanf to limit the characters. But whenever I enter something with scanf before fgets, it doesn't work correctly anymore.
This code works correctly
#include <stdio.h>
int main()
{
char name[10];
printf("Who are you? \n");
fgets(name,10,stdin);
printf("Good to meet you, %s.\n",name);
return(0);
}
This code does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?
Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.

Fgets don't let me input in a loop [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years ago.
I want to get rid of buffer overflow and I am using fgets instead of scanf to limit the characters. But whenever I enter something with scanf before fgets, it doesn't work correctly anymore.
This code works correctly
#include <stdio.h>
int main()
{
char name[10];
printf("Who are you? \n");
fgets(name,10,stdin);
printf("Good to meet you, %s.\n",name);
return(0);
}
This code does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?
Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.

Using fgets after scanf [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years ago.
I want to get rid of buffer overflow and I am using fgets instead of scanf to limit the characters. But whenever I enter something with scanf before fgets, it doesn't work correctly anymore.
This code works correctly
#include <stdio.h>
int main()
{
char name[10];
printf("Who are you? \n");
fgets(name,10,stdin);
printf("Good to meet you, %s.\n",name);
return(0);
}
This code does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?
Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.

scanf fails to read a string in C [duplicate]

This question already has answers here:
Reading string from input with space character? [duplicate]
(13 answers)
Closed 4 years ago.
I have a simple C program as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100],b[100];
char *ret;
printf("Enter the string\n");
scanf("%s",a);
printf("Enter the substring to be searched\n");
scanf("%s",b);
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}
When I execute the following program, scanf to read the string into b is not waiting for me to enter the substring and the print statement that prints the substring not found is being printed on the console. I tried to give %sand tried in the scanf statement and removed \n from the printf statements and nothing changed the way it executed the program. It would be great if someone solves this simple problem. Thanks in advance.
You can use scanf ("%[^\n]%*c", variable); with this scanf will read the whole line, instead of stopping when a space is reached.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100];
char b[100];
char *ret;
printf("Enter the string\n");
scanf ("%[^\n]%*c", a);
printf("Enter the substring to be searched\n");
scanf ("%[^\n]%*c", b);
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}
Also you can use fgets
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100];
char b[100];
char *ret;
printf("Enter the string\n");
fgets(a,100,stdin);//100 is the size of the string, you could use sizeof()
printf("Enter the substring to be searched\n");
fgets(b,100,stdin);//100 is the size of the string, you could use sizeof()
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}
try to use fgets instead of scanf, probably the reason is that the spaces are treated as delimiters, and the parts before the space are treated as a and the part right after the space will be treated as b. Therefore the programme did not prompt you for another input.
For your information: Reading string from input with space character?

Resources