Пока петля беда - PullRequest
       3

Пока петля беда

1 голос
/ 09 февраля 2011
/*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;
}

У меня проблемы со входами, я пытаюсь запустить цикл, когда пользователь запрашивает другие вычисления.Но когда он снова запускает другой тест, он сохраняет данные предыдущего теста, есть ли способ сбросить переменные в 0?Я пытался назначить все переменные внутри и снаружи цикла, но я немного растерялся, что делать дальше.

1 Ответ

2 голосов
/ 09 февраля 2011

Переместить объявление переменных, чтобы они находились внутри цикла.

while ( response != 0 )
{
    float  customer = 0, stops = 0, response = 0,  cost = 0, i = 0, stop = 0, total = 0, halfcost = 0, totaltrip = 0;
    ...
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...