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);
}
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 5 months ago.
I wrote a code to create a glossary of the most basic C instructions, their function and some definition (explained in a very simplistic way).
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main(void)
{
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main(void)
{
char dv = '"';
char choice;
int lsel;
printf("%s", "C GLOSSARY\nInstructions' syntax list, their function and definitions\n\n----------------------------------------------\n");
printf("%s", "\nType:\n- 'L' to display the list of instructions currently \n covered in this glossary.");
printf("%s", "\n- 'E' to exit the program\n");
do
{
scanf("%c", &choice);
choice = toupper(choice);
if(choice != 'L')
printf("ERROR: invalid option. Please, try again: ");
} while(choice != 'L');
if(choice == 'E')
return 0; //the code continues...
}
It works, but the message ERROR: invalid option. Please, try again: is displayed two times! Do you know why? If you want to try the code: https://github.com/AndreMarinelli/C-glossary
Thank you :)
Change scanf("%c", &choice); to scanf(" %c", &choice);.
The space in the format specifier allows scanf() to consume any amount of leading whitespace in the input.
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.
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.
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.
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.