delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/20/17:31:28

From: "Donn Mielcarek" <donn AT srl DOT radix DOT net_nospam>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Scanf function
Date: Mon, 20 Oct 1997 13:38:40 -0400
Organization: Systems Research Labs
Lines: 38
Message-ID: <62g4u9$4vv$1@news1.radix.net>
References: <62fp85$nq4$1 AT power DOT ci DOT uv DOT es>
NNTP-Posting-Host: donn.srl.radix.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Marsel wrote in message <62fp85$nq4$1 AT power DOT ci DOT uv DOT es>...
> Hi djgpp users,
>
> I have writen this code
>
>#include <stdio.h>
>main()
>{
> char c;
> int n;
> printf("Write an integer: ");
> scanf("%d",&n);
> printf("Write a character: ");
> scanf("%c",&c);
> printf ("The integer is: %d and the character is: %d \n",n,c);
> }
>
>Why the secong scanf function does not wait until the caracter is
introduced
>?. the c variable always contain the caracter \010


This one always gets new C programmers.  When you typed in the
integer, you probably also pressed the return key (i.e. newline).   You
need to take care of reading that.  Another scanf in there will do it:
    int newline;
    scanf("%d",&n);
    scanf("%c",&newline);

   etc...

----------------------------------------------------------------------
Donn Mielcarek                                      donn AT srl DOT radix DOT net
----------------------------------------------------------------------



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019