
scanf problem (DATA/MEMORY CONCEPT) (newbie)
Dear C programmers,
I'm a newbie on C and I have just wrote a very short stack programme
in C. But the programme doesn't stop decently at scanf point. Here is my
programme:
/******************************************************/
#include <stdio.h>
void main (void)
{
int top = -1, stack[99], tmp;
char command;
do {
/*INPUT PROMPT*/
printf("\n\nPUSH (p)\nPOP (o)\nTOP (t)\nEXIT (x)\n\n$: ");
scanf("%c", &command);
switch(command) {
case 'p':
printf("\n\n[PUSH]\nENTER NUMBER: ");
scanf("%d", &tmp);
stack[++top] = tmp;
break;
case 'o':
printf("\n\n[POP]\nPOPPED NUMBER: %d", stack[top--]);
break;
case 't':
printf("\n\n[TOP]\nTOP POSITION: %d", top);
printf("\nTOP NUMBER: %d", stack[top]);
break;
default:
break;
}
} while (command != 'x');
return;
Quote:
}
/**********************************************************/
The output didn't stop after the input prompt at scanf at the first time
AFTER any command was finished and stop only when it was encountered
again the second time. i.e. example output:
PUSH (p)
POP (o)
TOP (t)
EXIT (x)
$: t
[TOP]
TOP POSITION: 0
TOP NUMBER: 12
PUSH (p)
POP (o)
TOP (t)
EXIT (x)
$: (<--- this is the point where scanf doesn't stop for me)
PUSH (p)
POP (o)
TOP (t)
EXIT (x)
$: _ (<--- this is the cursor...)
When i tried to use: int command & scanf '%c' then i can't get into the
switch at all
When i tried to use: int command & scanf '%d' then the programme doesn't
stop at any scanf at all can continue to scroll.
Can somebody please help me to clarify my concept on C and point me the
way?
Thank you very much.
Regards
John