如何计算在Sentinel Loop C 中输入多少次输入

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

本文关键字:输入 Loop Sentinel 多少次 何计算 计算      更新时间:2023-10-16

这是指向编程问题的链接,以详细介绍。我已经完成了源代码,但我不知道如何计算和显示在Sentinel循环中输入多少次输入。这是我的源代码:

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

希望有人可以帮助我解决这个问题。我正在学习我的编程期末考试。

我知道您想计算客户端数(tot_clients)。您已经启用了TOT_CLIENTS = 0。这很好。您只需要在while循环内部递增tot_clients变量(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*/

tot_collection = tot_collection + charges;

之前添加tot_clients++;

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

`