scanf problem (DATA/MEMORY CONCEPT) (newbie) 
Author Message
 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



Sun, 29 Sep 2002 03:00:00 GMT  
 scanf problem (DATA/MEMORY CONCEPT) (newbie)


Quote:

>     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:

<snip>

you might want to take a look at the comp.lang.c FAQ, which may be
found at:-
    http://www.eskimo.com/~scs/C-faq/top.html

Particularly interesting might be:-

FAQ 11.12 "Can I declare main as void, to shut off these annoying
``main returns no value'' messages?"

FAQ 11.15 "The book I've been using always uses void main()"

FAQ 12.4 "My program's prompts and intermediate output don't always
show up on the screen"

FAQ 12.20 "Why does everyone say not to use scanf? What should I use
instead?"

and just curious; in the title of this thread what does "DATA/MEMORY
CONCEPT" mean?

--
"Perilous to us all are the devices of an art deeper than we possess
ourselves."
                Gandalf The Grey (discussing Windows NT)

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 29 Sep 2002 03:00:00 GMT  
 scanf problem (DATA/MEMORY CONCEPT) (newbie)

Quote:

>void main (void)

Warning : main() should return int.

Quote:
>  scanf("%c", &command);

   ^^^^^^^

Try using getchar(), instead.
You need to flush the keyboard buffer, after each call to scanf().

just use :

c = getchar() ;

if ( c != '\n' )
{
   while ( ( d = getchar() ) != '\n' ) ;
   /* flushes the input buffer */

Quote:
}

However, remember this isn't a portable solution.


Sun, 29 Sep 2002 03:00:00 GMT  
 scanf problem (DATA/MEMORY CONCEPT) (newbie)

Quote:
>You need to flush the keyboard buffer, after each call to scanf().

int c;

Quote:
>c = getchar() ;

>if ( c != '\n' )
>{
>   while ( ( d = getchar() ) != '\n' ) ;

   while ( ( c = getchar() ) != '\n' ) ;

Quote:
>   /* flushes the input buffer */
>}

>However, remember this isn't a portable solution.

Why ?

And what about

#include <stdio.h>

void flushin(void)
{
int c;
   while ((c=getchar())!='\n' && c!= EOF)
   {
   }

Quote:
}

The good solution is in fact to use fgets().

--
-hs- "Stove"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"Really?  When run on my machine, a printed copy of the C FAQ leaps
from the monitor and whacks me over the head.." -- Chris Mears CLC



Sun, 29 Sep 2002 03:00:00 GMT  
 scanf problem (DATA/MEMORY CONCEPT) (newbie)

Quote:

>Why ?

Read the FAQs for more informations.

Quote:
>int c;
>   while ((c=getchar())!='\n' && c!= EOF)
>   {
>   }
>}

This is a good solution in the case the input buffer doesn't contain
only '\n'.


Mon, 30 Sep 2002 03:00:00 GMT  
 scanf problem (DATA/MEMORY CONCEPT) (newbie)
Thank you you all =)


Quote:
> 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;
> }
> /**********************************************************/

> 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



Tue, 01 Oct 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Newbie problem w/ scanf

2. newbie problem with scanf

3. Newbie Array/Scanf Problem

4. Newbie - while loop & scanf problems

5. tcxl toolbox, innovative data concepts

6. Data Entry Forms - Need Help With General Concepts !

7. AddNew concept with System.Data.OleDb

8. newbie malloc problem: free memory allocated to structures

9. problem appending data to Memory mapped File in Unix

10. NEWBIE: paste data, string-problem

11. memory problems need boolean data type

12. SCANF() NEWBIE

 

 
Powered by phpBB® Forum Software