Problem: Create an algorithm for a program that simulates a police radar gun. The algorithm should read an automobile speed (in km/h) and print the message “Speeding” if the speed exceeds 59kmh. The algorithm should then also calculate the appropriate fine, $80 for 1-10kmh above the lim开发者_高级运维it, $150 for 11-30kmh above the limit, $500 for 31kmh or more above the limit. Use the space below.
My solutions:
#include <stdio.h>
int main()
{
int speed = 0;
int limit = 59;
/*Get automobile speed from user*/
printf("Please enter the speed: ");
scanf("%d%*c", &speed);
/* Based on the speed, generates the corresponding fine*/
if (speed > limit)
{
printf("Speeding");
if((speed - limit) <= 10)
printf("Fine: $80");
else
if((speed - limit) >= 11 & (speed - limit) <= 30)
printf("Fine: $150");
else
if((speed - limit) >= 31)
printf("Fine: $500");
}
else
printf("The automobile is not speeding");
return (0);
}
The problem here is that it won't print out the message "Speeding". Can anyone give me a hand?
printf
is buffered when writing to stdout. Use fflush(stdout
) after your printf
functions, or add a new line, ie printf("Speeding\n");
You can also disable buffering on the stdout stream by using setbuf(stdout, NULL);
精彩评论