【问题标题】:Looping assignment in C++C++中的循环赋值
【发布时间】: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' &amp;&amp; inputSalesPerson != 'Z');(这可能需要两个嘎嘎声)
  • 把这个放在开头:if (static_cast&lt;double&gt;(17/100) == 0) abort();
  • 感谢 GreatBigBore 的帮助。抱歉有任何问题,正在尝试使用此网站帮助我学习。

标签: c++ loops c++11 for-loop cout


【解决方案1】:

我已在需要修复的区域注释了您的代码。此外,到处使用 cin.get() 也存在问题。我假设您这样做是为了在每次输入后丢弃返回字符。但是,如果在调用 cin.get() 时标准输入 (cin) 为空,它将阻塞程序,直到输入某些内容。当您输入多个 num_sales 时会发生这种情况:

for (int i = 0; i<num_sales; i++)
{
     cin.get();   

它处理第一个罚款,但在第二个循环你得到:

Enter the sale amount : 20
Commission earned by tom on this sale is : 23.40
// cin.get() blocks here, with no user instructions to enter the next sale amount

我已经注释掉了所有的 cin.get()。它仍然可以正常工作,因为 cin 运算符 >> 丢弃了空格和换行符,因此即使缓冲区中仍有 \n 换行符,下次您执行类似 cin >> num_sales 之类的操作时,它仍然会丢弃换行符。

#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.0); // Int divided by int will round to an int. 
                    // commRate is 0.0.  Divide by double instead (17 / 100.0)
    int num_sales;
    double salesAmount, totalSales = 0, tempComm; // totalSales needs to be initialised to 
                                                  // zero, otherwise it holds a garbage value.
    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 amount for sale number " << i+1 << ": ";
                cin >> salesAmount;
                system("cls"); // I would put the clear here,
                 // Otherwise the user can't see the commission made by Tom

                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; // I think you mean to add salesAmount maybe?
            }
            break;
        }

    } //while (inputSalesPerson != 'z'        ||     'Z');
      // Even if {         this ^^^^} is false,      ^^^ this is always
      // 'Z' char will convert to bool, any non-zero value is true.

      while (inputSalesPerson != 'z' && inputSalesPerson != 'Z');

    return 0;
}

【讨论】:

    猜你喜欢
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 2015-03-22
    • 2013-09-17
    • 1970-01-01
    • 2016-01-17
    • 2015-12-09
    相关资源
    最近更新 更多