2016-12-25 7 views
-1

Here - ссылка на вопрос программирования для деталей. Я сделал свой исходный код, но я не знаю, как рассчитать и отобразить, сколько раз вводится ввод в цикле дозорного устройства. Вот мой исходный код:Как подсчитать, сколько раз вводится ввод в петле Sentinel C++

#include <iostream> 

using namespace std; 

int main() { 
    int i, hours; 
    int tot_clients = 0; 
    float charges; 
    float tot_collection = 0; 
    const int sentinel = -1; 

    cout << "Welcome to ABC Parking Services Sdn Bhd" << endl; 
    cout << "=======================================" << endl; 

    while (hours != sentinel) { 
    cout << "Please enter number of parking hours (-1 to stop)" << endl; 
    cin >> hours; 

    if (hours <= 2 && hours > 0) { 
     charges = 1.00 * hours; 
     cout << "Charges: " << charges << endl; 
    } 
    else if (hours > 2 && hours <= 12) { 
     charges = (1.00 * 2) + ((hours - 2) * 0.50); 
     cout << "Charges: " << charges << endl; 
    } 
    else if (hours > 12) { 
     charges = 10.00 * hours; 
     cout << "Charges: " << charges << endl; 
    } 
    else if (hours == sentinel) { 
     break; 
    } 

    tot_collection = tot_collection + charges; 
    } 

    cout << "SUMMARY OF REPORT" << endl; 
    cout << "=======================================" << endl; 

    cout << "Total clients:" << tot_clients << endl; 
    cout << "Total colletion:" << tot_collection << endl; 

    return 0; 
} 

Надеюсь, кто-то может помочь мне в решении этого вопроса. Я изучаю свой выпускной экзамен по программированию.

+1

Для начала использовать переменную' hours' до его инициализации, что означает, что его значение будет * неопределенными * и у вас будет * неопределенное поведение *. Возможно, здесь будет лучше делать цикл while? –

+0

emmm, но вопрос попросил написать в цикле дозорного. : ') –

+2

И цикл 'do ... while (...)' по-прежнему представляет собой цикл. :) –

ответ

3

Насколько я понимаю, вы хотите подсчитать количество клиентов (tot_clients). Вы уже выполнили tot_clients = 0. Это хорошо. Вам просто нужно увеличить значение переменной tot_clients внутри цикла while (tot_clients ++).

while (hours != sentinel) 
{ 
    cout << "Please enter number of parking hours (-1 to stop)" << endl; 
    cin >> hours; 

    tot_clients++; //this is the code to be added 

    /*rest of the code remains same*/ 
+0

omg thaaaaaaanks hahah это помогает мне много жаль, если мой вопрос путает хуху, но рад, что вы понимаете, что я имел в виду! –

+0

нет проблем ... рад помочь :) –

2

Добавить tot_clients++; непосредственно перед или после tot_collection = tot_collection + charges;

+0

thaaaanks it works !! –

1

1- инициализации часов = 0 иначе часа будет иметь некоторое неопределенное значение в течение первого времени условия проверки в то время цикла.
2-я предполагаю, что tot_clients магазины нет. клиентов, чем, вам просто нужно увеличивать tot_clients на каждой итерации

#include <iostream> 
    using namespace std; 
    int main() 
    { 
    int i, hours=0; 
    int tot_clients=0; 
    float charges; 
    float tot_collection=0; 
    const int sentinel = -1; 

    cout << "Welcome to ABC Parking Services Sdn Bhd" << endl; 
    cout << "=======================================" << endl; 

    while (hours != sentinel) 
    { 
    cout << "Please enter number of parking hours (-1 to stop)" << endl; 
    cin >> hours;` 

    if (hours <= 2 && hours >0) 
    { 
     charges = 1.00 * hours; 
     cout << "Charges: " << charges << endl; 
    } 

    else if (hours>2 && hours <=12) 
    { 
     charges = (1.00*2) + ((hours - 2) * 0.50); 
     cout << "Charges: " << charges << endl;   
    } 

    else if (hours > 12) 
    { 
     charges = 10.00 * hours; 
     cout << "Charges: " << charges << endl;   
    } 

    else if (hours == sentinel) 
    { 
     break; 
    } 

    tot_collection = tot_collection + charges; 
    tot_clients=tot_clients+1; //increment's on each iteration except on input -1 

} 


cout << "SUMMARY OF REPORT" << endl; 
cout << "=======================================" << endl; 

cout << "Total clients:" << tot_clients << endl; 
cout << "Total colletion:" << tot_collection << endl; 

return 0; 

} 

`

+0

hoho thaaanks. знак равно –

Смежные вопросы