pete
6/26/2011 6:06:00 AM
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