【发布时间】:2018-03-13 04:28:37
【问题描述】:
我的任务是利用循环。该程序应接受 3 名员工(Mary、Tom 和 Chris)的销售输入。流程应该如下:
初始? > 要输入的销售数量 > 输入销售金额 > 显示 17% 的销售佣金 > 将佣金和销售额添加到各自的变量中 >> 继续直到输入“z”作为 inputSalesPerson >> 显示信息
所以我想弄清楚为什么我的 tempComm 变量的返回值没有返回正确的值。如果我要为变量 inputSalesPerson 输入 't',它会将我放入 switch case 't' 没问题。输入销售数量并且有效。但是当我输入 salesAmount 然后显示佣金时,它不会正确计算。
此外,如果我输入“z”或“Z”作为 inputSalesPerson,它不会结束程序。我有很多事情要做。
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int salesT = 0, salesC = 0, salesM = 0;
double amountT = 0, amountC = 0, amountM = 0;
double commT = 0, commC = 0, commM = 0;
double commRate = (17/100);
int num_sales;
double salesAmount, totalSales, tempComm;
char inputSalesPerson;
do
{
cout << "Enter the sales person's initial (\"Z\" to quit): ";
cin >> inputSalesPerson;
while(inputSalesPerson != 't' && inputSalesPerson != 'T' && inputSalesPerson != 'm' && inputSalesPerson != 'M' && inputSalesPerson != 'c' && inputSalesPerson != 'C' && inputSalesPerson != 'z' && inputSalesPerson != 'Z')
{
cin.get();
system("cls");
cout << "Invalid input for employee. Please Input (T)om, (C)hris, (M)ary, or (Z) to End : ";
cin >> inputSalesPerson;
}
switch(inputSalesPerson)
{
case 't' :
case 'T' :
system("cls");
cout << "Enter the number of sales : ";
cin >> num_sales;
while(num_sales < 1 || num_sales > 5)
{
system("cls");
cout << "Invalid number of sales. Please enter a value between 1 and 5 : ";
cin >> num_sales;
}
salesT += num_sales;
for(int i = 0; i<num_sales; i++)
{
cin.get();
system("cls");
cout << "Enter the sale amount : ";
cin >> salesAmount;
while(salesAmount < 0)
{
cin.get();
system("cls");
cout << "Invalid sale amount. Please enter a positive amount : ";
cin >> salesAmount;
}
tempComm = salesAmount + (salesAmount * commRate);
cout << fixed << setprecision(2) << "Commission earned by tom on this sale is : " << tempComm << endl;
cin.get();
amountT += salesAmount + tempComm;
commT += tempComm;
totalSales += amountT;
}
break;
}
}while(inputSalesPerson != 'z' || 'Z');
return 0;
}
****编辑**** 感谢您提供有关单步调试的信息。多亏了那条评论,我才能够更深入地了解如何使用调试工具,这帮助我让一切工作得更好一些。
【问题讨论】:
-
编辑您的问题并讨论您在使用调试器单步执行代码时发现的内容。
-
见How to debug small programs 不要忽视鸭子。他工作。
-
一个非常响亮的嘎嘎声可能是
while (inputSalesPerson != 'z' && inputSalesPerson != 'Z');(这可能需要两个嘎嘎声) -
把这个放在开头:
if (static_cast<double>(17/100) == 0) abort(); -
感谢 GreatBigBore 的帮助。抱歉有任何问题,正在尝试使用此网站帮助我学习。
标签: c++ loops c++11 for-loop cout