I have a problem with scanf, when I run program scanf just gets [space], so I'm confused.
***
1 EnQ
2 DeQ
3 Print
4 Exit
enter your choice: 1
enter the char:
1 EnQ
2 DeQ
3 Print
4 Exit
.... Help me please. -
#include <stdio.h>
-
#include <string.h>
-
-
int n = 0;
-
char Q[8];
-
int tmp;
-
char *H = Q;
-
char *T = Q;
-
int count = 0;
-
-
-
-
int main(){
-
int choice;
-
-
menu:
-
printf("\n1 EnQ\n 2 DeQ\n 3 Print\n 4 Exit\n");
-
printf("Enter your choice:");
-
scanf("%d", &choice);
-
switch(choice){
-
case 1:{
-
EnQ();
-
goto menu;
-
}
-
case 2:{
-
DeQ();
-
goto menu;
-
}
-
case 3:{
-
Print();
-
goto menu;
-
}
-
case 4:
-
return 0;
-
}
-
return 0;
-
}
-
-
-
int EnQ(){
-
if(count == 0){
-
printf("Enter the Char:");
-
scanf("%c", &Q[n]);
-
n++;
-
count++;
-
} else if(count == 7)
-
printf("The array is full");
-
else if(n == 7){
-
n = 0;
-
printf("Enter the char:");
-
scanf("%c", &Q[n]);
-
count++;
-
T = Q[n];
-
n++;
-
}else{
-
printf("Enter the char:");
-
scanf("%c", &Q[n]);
-
n++;
-
count++;
-
}
-
tmp = n;
-
return 0;
-
}
-
Because stdin is line buffered (normally), that means that no data is passed to your program until the user presses enter so in input a number you have to type digits and then the return key. All the digits plus the newline that is generated by the return key are entered into stdins input buffer.
When you call scanf("%d", &choice); it reads the digits and leaves the newline because that is what you told it to do. So when you call scanf("%c", &Q[n]); the newline character '\n' is still in the input buffer and no more data is actually requested form the user.
IMO the best way round this is to split reading data and converting data into 2 separate steps. All data is entered in printable characters scanf tries to merge the 2 functions of reading data and converting data. If instead you read input from the user and then convert the data read you can get round this problem by always reading a line at a time; a simplistic implementation of this is -
char inbuf[100];
-
int value;
-
-
fgets(inbuf, sizeof inbuf, stdin); // Read a line include '\n' from stdin
-
value = atoi(inbuf);
-
5 2536
You should use break; I.L.O. goto menu;as the ending statement in each case:
I don't think that this is the problem.
I tried to compile it, and it does not even compile, even after I inserted dummy functions for Deq and Print. Maybe it is some old executable left after previous successful builds.
ps - apparently it scans the end-of-line as the character that is left after readingthe integer. You may just discard all non-printables and read one more char again.
did exactly as you said, now program is working, but i still wonder why it didn't work at first. Thanks anyway.
Banfa 9,065
Expert Mod 8TB
Because stdin is line buffered (normally), that means that no data is passed to your program until the user presses enter so in input a number you have to type digits and then the return key. All the digits plus the newline that is generated by the return key are entered into stdins input buffer.
When you call scanf("%d", &choice); it reads the digits and leaves the newline because that is what you told it to do. So when you call scanf("%c", &Q[n]); the newline character '\n' is still in the input buffer and no more data is actually requested form the user.
IMO the best way round this is to split reading data and converting data into 2 separate steps. All data is entered in printable characters scanf tries to merge the 2 functions of reading data and converting data. If instead you read input from the user and then convert the data read you can get round this problem by always reading a line at a time; a simplistic implementation of this is -
char inbuf[100];
-
int value;
-
-
fgets(inbuf, sizeof inbuf, stdin); // Read a line include '\n' from stdin
-
value = atoi(inbuf);
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: B Thomas |
last post by:
Hi,
I was reading O'Reilly's "Practical C programming" book and it warns
against the use of scanf, suggesting to avoid using it completely .
Instead it recomends to use using fgets and sscanf....
|
by: Jonathan Ng |
last post by:
Hi,
I was wondering if there was a way to include the white spaces in a string.
Currently, I am using:
scanf("%s", &input);
However, this doesn't include the 'space' character or any other...
|
by: Vinicius |
last post by:
Hello,
the following code does not work:
"
....
void main(void)
{
char option;
printf("Choose an option: ");
|
by: sreelal |
last post by:
Dear all,
plz see the following prograam.
main()
{
int a,b;
scanf("%d %d ",&a,&b);
|
by: Till Crueger |
last post by:
Hi,
I have a little problem with the following code:
#include <stdio.h>
int main(void) {
char input='\0';
while(input!='q') {
printf("Menu\n");
fflush(stdout);
|
by: Vig |
last post by:
Is scanf or any other function capable of reading numbers in the format
1.2345d-13 where 'd' serves the same role as 'e' usually does in scientific
notation? This operation is iterated through...
|
by: Lalatendu Das |
last post by:
Dear friends,
I am getting a problem in the code while interacting with a nested
Do-while loop
It is skipping a scanf () function which it should not. I have written
the whole code below. Please...
|
by: iwinux |
last post by:
Hi.
Before I use scanf(), I must malloc the memory for it, like this:
//Start
char * buffer;
buffer = malloc(20);
scanf("%s", &buffer);
//End
|
by: stathisgotsis |
last post by:
Hello everyone,
Trusting K&R2 i thought until recently that spaces are ignored in
scanf's format string. Reading arguments to the contrary confused me a
little. So i now ask:
Is...
|
by: sophia |
last post by:
why scanf("%d. %d. %d",&d,&m,&y);
is NOT reading the i/p 22 4 1972 correctly,
whereas it is reading the i/p 22. 4. 1972 correctly ?.
Is it because of the dot in the format string of scanf ?
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |