如何计算在Sentinel Loop C 中输入多少次输入
How to Calculate How Many Times Inputs Entered in Sentinel Loop c++
这是指向编程问题的链接,以详细介绍。我已经完成了源代码,但我不知道如何计算和显示在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;
}
`
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 用户定义函数中的指针和输入
- 如果用户输入数字 <0,我如何停止" while loop "?
- 是否可以对用户输入进行"loop interrupt"?
- 如何计算在Sentinel Loop C 中输入多少次输入
- 如何在没有任何指示终止的输入的情况下摆脱这种"loop"?