Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
While循环故障 /*从用户处获取输入以计算旅行计划的成本 使用旅行者数量/站点数量/每个站点的成本 提供旅行费用的一半和全额*/ 整数计算(整数合计); #包括 内部主(空) { 浮动客户、站点、响应、成本、i、站点、总计、一半成本、总计行程; i=0; printf(“欢迎使用航空快递。\n”); printf(“您想计算一次旅行的费用吗?”); scanf(“%f”、&response); while(响应!=0) { printf(“请输入将要旅行的客户数量。12岁以下的儿童计为.5:”); scanf(“%f”和“客户”); 如果(客户_C - Fatal编程技术网

While循环故障 /*从用户处获取输入以计算旅行计划的成本 使用旅行者数量/站点数量/每个站点的成本 提供旅行费用的一半和全额*/ 整数计算(整数合计); #包括 内部主(空) { 浮动客户、站点、响应、成本、i、站点、总计、一半成本、总计行程; i=0; printf(“欢迎使用航空快递。\n”); printf(“您想计算一次旅行的费用吗?”); scanf(“%f”、&response); while(响应!=0) { printf(“请输入将要旅行的客户数量。12岁以下的儿童计为.5:”); scanf(“%f”和“客户”); 如果(客户

While循环故障 /*从用户处获取输入以计算旅行计划的成本 使用旅行者数量/站点数量/每个站点的成本 提供旅行费用的一半和全额*/ 整数计算(整数合计); #包括 内部主(空) { 浮动客户、站点、响应、成本、i、站点、总计、一半成本、总计行程; i=0; printf(“欢迎使用航空快递。\n”); printf(“您想计算一次旅行的费用吗?”); scanf(“%f”、&response); while(响应!=0) { printf(“请输入将要旅行的客户数量。12岁以下的儿童计为.5:”); scanf(“%f”和“客户”); 如果(客户,c,C,将变量的声明移动到循环内部 /*Taking input from user to calculate the cost of a travel plan

将变量的声明移动到循环内部

/*Taking input from user to calculate the cost of a travel plan                                                                                                                                                                            
using the number of travelers/ number of stops/ cost of each stop                                                                                                                                                                          
giving both the half cost of a trip and its full price*/

int calculate( int total);

#include <stdio.h>
int main(void)
{
    float  customer, stops, response,  cost, i, stop, total, halfcost, totaltrip;
    i=0;
    printf("Welcome to Airline express.\n");

    printf("Would you like to calculate the cost of a trip?");

    scanf("%f", &response);
    while ( response != 0 )
    {
        printf("Please enter how many customer will be traveling. Children under 12 are counted as .5:");
        scanf("%f", &customer);
        if (customer < 0)
            customer = 3;

        printf("Please enter how many stops there are:");
        scanf("%f", &stop);

        while (i<stop)
        {
            printf("Please enter cost of the next stop:");
            scanf("%f", &cost);
            if (cost < 0)
                cost = 100;
            total +=cost;
            i++;
        }

        halfcost = calculate(total);
        printf("Half cost is: %f", halfcost);


        totaltrip = total * customer;
        printf("Total cost for trip is: %f\n", totaltrip);

        printf("Would you like to calculate another trip?");
        scanf("%f", &response);
    }
    return 0;
}

int calculate( int total)
{
    return total/2;
}

它可以工作:D但是有一个小问题,我可以在函数的顶部而不是在循环中声明它们,或者它必须在循环中才能正常工作,注意C中变量声明的要求是在块的开头;通常是这样(mis)解释为要求它作为函数的开始。将变量声明尽可能靠近需要它们的位置是一种有用的模式。谢谢,我也在想同样的猜测,我将不得不就她的偏好与教授争论。这是一个令人困惑的问题。更局部的范围本身就很好(除了响应),但真正的问题是,
total
在循环中没有初始化为0。@Keith“注意到C中的变量声明要求”虽然20年前有这样的要求,但今天却没有。你真的应该学习整数。
float
对于可数的事物不是一种合适的数据类型(与停车次数类似)。
total
是少数几个可能接受分数的变量之一,您可以将其传递到
int
参数中。您从未初始化过
total
(即使是第一次);在开始向其添加
成本
s之前,它不能保证为0。
响应
也应该是字符…仅Y/Y或N/N是一个直观的输入。
while ( response != 0 )
{
    float  customer = 0, stops = 0, response = 0,  cost = 0, i = 0, stop = 0, total = 0, halfcost = 0, totaltrip = 0;
    ...
}