我有点难过。我有一种感觉,我在代码中的某个地方出了问题,但似乎无法完全按照我的意思。基本上我被分配了一个C代码,据说可以模拟警用雷达枪。用户将插入一个整数,并且根据输入,将告知用户该输入是否会超速,并且如果是特定罚款将被给出。
以下是我的代码
/* *****
Author: Shivan Kamal
Date: 12-3-2015
Program Reason: To simulate a police speed radar by getting user to insert a number
and to give them an answer to whether they are speed or not, and provide a warning or a fine
where required.
*****/
#include <stdio.h>
int main()
{
/*****
Declaring values
******/
int speed;
const int LIMIT1 = 60;
const int LIMIT2 = 65;
const int LIMIT3 = 70;
const int LIMIT4 = 80;
const int LIMIT5 = 200;
const int FINE1 = 80;
const int FINE2 = 150;
const int FINE3 = 500;
/*****
Beginning of the program
*****/
printf(" If you want to check whether a certain speed issues you\n");
printf(" a warning or a fine and how much, please input a number value\n");
printf(" NOTE: the number value will be in KM/h.\n");
scanf("%d%*c" , speed);
if(speed == 0)
{
printf(" Your vehicle is not moving at '0'km/h. If your car was blocking traffic, you would be issued a fine. Please be careful.\n");
}
else
if(speed <0)
{
printf("Your vehicle is most likely in reverse. If this is the case and your vehicle disrupts traffic. You may be issued a fine. Please be careful.\n");
}
else
if(speed <= LIMIT1)
{
printf("You're within the speed limit.\n");
}
else
if(speed <= LIMIT2)
{
printf("SPEEDING - Warning! You are going over the speed limit.\n Please slow down or you may be issued a fine");
}
else
if(speed <= LIMIT3)
{
printf("SPEEDING - You have gone over the speed limit.\n Fined $FINE1.\n");
}
else
if(speed <= LIMIT4)
{
printf("SPEEDING - You have gone over the speed limit.\n Fined $FINE2.\n");
}
else
if(speed <= LIMIT5)
{
printf("SPEEDING - You have gone over the speed limit.\n Fined $FINE3.\n");
}
else
{
printf(" By getting caught going faster than any limit higher than 20km/h.\n Your fine will simply keep getting bigger.\n");
printf("Please stick to the speed limit and follow the rules.\n");
}
return(0);
}
编译代码并运行代码时,无论用户输入什么内容,都会一遍又一遍地提供一条特定的消息。消息在屏幕截图中。
任何人都可以告诉我,我哪里出错了?
也正是如何在printf
语句中打印常量变量。这是另外一部分,我认为我也做错了。
答案 0 :(得分:4)
更改
scanf("%d%*c" , speed);
到
scanf("%d%*c" , &speed);
在接受&
的值时,您需要使用scanf()
。一元&
返回其后面的变量的地址。 scanf()
将值存储在该地址中。因此,您需要在&
中使用scanf()
。但是,您不需要&
使用printf()
(除非您出于某种原因打印该变量的地址)
您还应该将3 printf()
语句更改为
printf("SPEEDING - You have gone over the speed limit.\n Fined $%d.\n",FINE1);
printf("SPEEDING - You have gone over the speed limit.\n Fined $%d .\n", FINE2);
printf("SPEEDING - You have gone over the speed limit.\n Fined $%d .\n", FINE3);
否则,它不会显示FINE的值