c - 创建 if else 程序来计算不同车辆的不同 parking 费用时遇到麻烦

标签 c dev-c++

我无法使此代码正常工作。我刚刚学习了这些内容(if else 语句),并且必须通过创建一个计算每辆不同车辆的 parking 费用的程序来将其付诸实践。 c 代表汽车(每小时 2 美元),b 代表巴士(每小时 3 美元),t 代表卡车(每小时 4 美元)。这是使用 dev-c++ 编译器进行的 C 编程。

感谢所有反馈,提前谢谢您!

#include <stdio.h>

//declaration
char parkingCharge (int pc);
int pc;
int h, total, c, b, t;
char v;

int main (void)
{	
//statements

	printf ("Enter type of vehicle (c for car, ");
	printf ("b for bus, or t for truck): ");
	scanf ("%c", &v);
	
	printf ("How long did you park: ");
	scanf ("%d", &h);
	
	total = pc;
	printf ("Your total is: %d", total);

return 0;
}

char parkingCharge (int pc)
{
//statements

	if (v == c){
	   pc = 2 * h;
    }
    else if (v == b){
	   pc = 3 * h;
    }
    else if (v == t){
	   pc = 4 * h;
    }
return total;
}

最佳答案

#include <stdio.h>

//declaration
int parkingCharge (char v, int h);
int h;
char v;

int main (void)
{
//statements

        printf ("Enter type of vehicle (c for car, ");
        printf ("b for bus, or t for truck): ");
        scanf ("%c", &v);

        printf ("How long did you park: ");
        scanf ("%d", &h);
        // You forgot to call the function parkingCharge.
        int total = parkingCharge(v, h);
        printf ("Your total is: %d\n", total);

return 0;
}
// parkingCharge should return an int value and you could pass
// v and h as parameters.
int parkingCharge (char v, int h)
{
    int pc;
    // Remember when you compare a variable with a constant char, you
    // put the constant char value between ''.
    if (v == 'c'){
           pc = 2 * h;
    }
    else if (v == 'b'){
           pc = 3 * h;
    }
    else if (v == 't'){
           pc = 4 * h;
    }
    return pc;
}

关于c - 创建 if else 程序来计算不同车辆的不同 parking 费用时遇到麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29198086/

相关文章:

c++ - 重载 cout 时 undefined reference

java - 如何正确初始化 JNA 中 `Structure` 内的数组字段?

c++ - 在取消定义宏函数之前使用/将宏变量的值复制到宏函数中 : is it possible?

c++ - 两个如何将matlab中的以下行转换为c++

c - 如何修复这个 if else 语句?c 程序

compiler-errors - 关于书中示例的错误

c - 如何使用 C 中的 while 循环使程序无限重新启动

c - 尝试使用 scanf 读取数字时程序崩溃

c - 在 C 的自定义 shell 中执行重定向( ">"和 "<")

c++ - 控制台应用程序不会立即关闭