Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #6957

Re: printf & scanf order

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!74.125.64.80.MISMATCH!postnews.google.com!news2.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail
NNTP-Posting-Date Sun, 26 Jun 2011 01:06:09 -0500
Message-ID <4E06CC51.5204@mindspring.com> (permalink)
Date Sun, 26 Jun 2011 02:06:09 -0400
From pete <pfiland@mindspring.com>
Reply-To pfiland@mindspring.com
Organization PF
X-Mailer Mozilla 3.04Gold (WinNT; I)
MIME-Version 1.0
Newsgroups comp.lang.c
Subject Re: printf & scanf order
References <88abc4aa-ecdb-4136-98a8-112a78916d6e@28g2000pry.googlegroups.com>
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
Lines 71
X-Usenet-Provider http://www.giganews.com
NNTP-Posting-Host 4.154.219.253
X-Trace sv3-XyoxYD7MWImbgVZ0RkUU2ggZuMziRQbvBu7HnqpWW4UZIFXU782h6gKT47/CWTe/GGhh6cxy2TXNwFy!zYkSCAwHvQa1fmZkXcW6jrX7c879rHlhep+U8whYwzAIoTgzYuUvasjpHeR34gpGXDTw8+7KPNza!Xo4Z9GdrD28=
X-Abuse-and-DMCA-Info Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info Otherwise we will be unable to process your complaint properly
X-Postfilter 1.3.40
X-Original-Bytes 2761
Xref x330-a1.tempe.blueboxinc.net comp.lang.c:6957

Show key headers only | View raw


flebber wrote:
> 
> Hi
> 
> I am starting to learn c and have been playing with several simple
> scripts to learn.
> 
> Question why does the request for scanf precede printf. I created a
> simple script to calculate interest. When running the script though
> scanf is processed first and the request for input comes first,
> despite being in the second position. How do I get printf to call
> first.
> 
> This is my script.
> 
> # include <stdio.h>
> int main()
> {
>   float a,b,c,interest;
>   printf("Type in amount in dollars cents: \n");
>   scanf("%f",&a);
>   b = 0.075;
>   c = a * b;
>   interest = a + c;
>   printf("The total including interest is $%.2f", interest);
>     return 0;
> }

I'm not sure if I understand your question.
But if I do, then maybe placing
    fflush(stdout);
before your scanf call might solve your problem.

Also, it is best to terminate a text stream 
with a newline character in your last printf call.

# include <stdio.h>

int main(void)
{
    float a, b, c, interest;
    printf("Type in amount in dollars cents: \n");
    fflush(stdout);
    scanf("%f",&a);
    b = 0.075f;
    c = a * b;
    interest = a + c;
    printf("The total including interest is $%.2f\n", interest);
    return 0;
}

(double) is a more natural type than (float).
(double) is not subject to default argument promotions.

# include <stdio.h>

int main(void)
{
    double a, b, c, interest;
    printf("Type in amount in dollars cents: \n");
    fflush(stdout);
    scanf("%lf",&a);
    b = 0.075;
    c = a * b;
    interest = a + c;
    printf("The total including interest is $%.2f\n", interest);
    return 0;
} 

-- 
pete

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

printf & scanf order flebber <flebber.crue@gmail.com> - 2011-06-25 22:09 -0700
  Re: printf & scanf order pete <pfiland@mindspring.com> - 2011-06-26 02:06 -0400
    Re: printf & scanf order "Heinrich Wolf" <invalid@invalid.invalid> - 2011-06-26 14:58 +0200
      Re: printf & scanf order "Heinrich Wolf" <invalid@invalid.invalid> - 2011-06-26 15:16 +0200
        Re: printf & scanf order Ralf Damaschke <rwspam@gmx.de> - 2011-06-26 14:12 +0000
        Re: printf & scanf order "Heinrich Wolf" <invalid@invalid.invalid> - 2011-06-26 16:20 +0200
        Re: printf & scanf order "J. J. Farrell" <jjf@bcs.org.uk> - 2011-06-28 03:59 +0100
  Re: printf & scanf order Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-26 08:27 -0400
  Re: printf & scanf order "Heinrich Wolf" <invalid@invalid.invalid> - 2011-06-26 17:17 +0200

csiph-web