How to Calculate How Many Times Inputs Entered in Sentinel Loop c++

1.3k views Asked by At

Here is the link to the programming question for detail. I have done my source code but I don't know how to calculate and display how many times inputs are entered in sentinel loop. Here is my source code:

#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;
}

Hope someone can help me solving this. I am studying for my programming final exam.

3

There are 3 answers

1
Anand Kumar On

I understand you want to count the number of clients (tot_clients). You have already intitalised the tot_clients = 0. This is good. You just have to increment the tot_clients variable inside the while loop (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
Abdul Rehman On

Add tot_clients++; just before or after tot_collection = tot_collection + charges;

1
anubhav deshwal On

1- initialize hours=0 otherwise hour will have some undetermined value during first-time condition check of while loop.
2-i am assuming that tot_clients stores total no. of customers than, you just need to increment tot_clients on each iteration

 #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;

}

`