-5

こんにちは、私は課題に取り組んでいます。何時間も費やしましたが、プログラムの最後に「Total Commission」と「Total due」が加算されない理由がわかりません。私は初心者なので、正しい方向に導くことができるものは何でも大歓迎です. ありがとう。

オリジナルコード

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

const double PC_BASE_SALES = 6000.00, //PC is Personal Computers
             PC_COMMISSION = 0.12,
             PRINTER_BASE_SALES = 2500.00,
             PRINTER_COMMISSION = 0.10,
             ACCESSORIE_BASE_SALES = 2000.00,
             ACCESSORIE_COMMISSION = 0.10,
             MAINTENANCE_BASE_SALES = 1500.00,
             MAINTENANCE_COMMISSION = 0.06;

double       baseSalary = 0.00,
             pcSales = 0.00,
             printerSales = 0.00,
             accessorieSales = 0.00,
             maintenanceSales = 0.00,
             totalCommission = 0.00,
             totalDue = 0.00,
             noCommission = 0.00,
             pcCommission = baseSalary * PC_COMMISSION,
             printerCommission = baseSalary * PRINTER_COMMISSION,
             accessorieCommission =  baseSalary * ACCESSORIE_COMMISSION,
             maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;

int          idNumber; //salesman identification number

do
{
cout << "Please Enter Salesman Identification or -999 to Terminate ";
cin >> idNumber;

if (idNumber == -999)
    return 0;
else

cout << "Please Enter Salesman Base Salary\t\t\t  ";
cin >> baseSalary;

cout << "Please Enter Personal Computer Sales\t\t\t  ";
cin >> pcSales;

cout << "Please Enter Printer Sales\t\t\t\t  ";
cin >> printerSales;

cout << "Please Enter Accessories Sales\t\t\t\t   ";
cin >> accessorieSales;

cout << "Please Enter Maintenance Sales\t\t\t\t  ";
cin >> maintenanceSales;
cout << endl;
cout << endl;
cout << endl;
cout << endl;

cout << "\t\t\t  My Computer Company\n ";
cout << endl;
cout << "\t\t\t Commission Statement\n ";
cout << endl;
cout << "\t\t\t  Salesman Number " << idNumber << endl;
cout << endl;
cout << "\t\t   ********************************\n";
cout << endl;

cout << fixed;
cout << setprecision(2);

cout << left << setw(20) << "Product";
cout << right << setw(20) << "Sales Amount";
cout << right << setw(20) << "Commission";
cout << "\n" << endl;

cout << left << setw(20) << "Personal Computers";
cout << right << setw(20) << pcSales;

while (pcSales > PC_BASE_SALES)
    {
        double pcCommission = baseSalary * PC_COMMISSION;
        cout << right << setw(20) << pcCommission;
        cout << "\n" << endl;
        pcCommission += totalCommission;
        break;
    }
    while (pcSales < PC_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    break;
    }

cout << left << setw(20) << "Printers";
cout << right << setw(20) << printerSales;
    while (printerSales > PRINTER_BASE_SALES)
    {
        double printerCommission = baseSalary * PRINTER_COMMISSION;
        cout << right << setw(20) << printerCommission;
        cout << "\n" << endl;
        printerCommission += totalCommission;
        break;
    }
    while (printerSales < PRINTER_BASE_SALES)
    {
        cout << right << setw(20) << noCommission;
        cout << "\n" << endl;
        break;
    }

cout << left << setw(20) << "Accessories";
cout << right << setw(20) << accessorieSales;
    while (accessorieSales > ACCESSORIE_BASE_SALES)
    {
        double accessorieCommission =  baseSalary * ACCESSORIE_COMMISSION;
        cout << right << setw(20) << accessorieCommission;
        cout << "\n" << endl;
        accessorieCommission += totalCommission;
        break;
    }
    while (accessorieSales < ACCESSORIE_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    break;
    }

cout << left << setw(20) << "Maintenance";
cout << right << setw(20) << maintenanceSales;
    while (maintenanceSales > MAINTENANCE_BASE_SALES)
    {
        double maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
        cout << right << setw(20) << maintenanceCommission;
        cout << "\n" << endl;
        maintenanceCommission += totalCommission;
        break;
    }
    while (maintenanceSales < MAINTENANCE_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    cout << endl;
    break;
    }

cout << endl;

      totalCommission =  pcCommission + printerCommission + accessorieCommission +   
      maintenanceCommission;
      totalDue = baseSalary + totalCommission;

cout << left << setw(20) << "Total Commission";
cout << right << setw(40) << totalCommission;
cout << "\n" << endl;

      cout << left << setw(20) << "Base Pay";
cout << right << setw(40) << baseSalary;
cout << "\n" << endl;

cout << left << setw(20) << "Total Due";
cout << right << setw(40) << totalDue;
cout << "\n" << endl;

    } while ( idNumber != -999);

system ("PAUSE");
return 0;

更新されたコード

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

const double PC_BASE_SALES = 6000.00, //PC is Personal Computers
             PC_COMMISSION = 0.12,
             PRINTER_BASE_SALES = 2500.00,
             PRINTER_COMMISSION = 0.10,
             ACCESSORIE_BASE_SALES = 2000.00,
             ACCESSORIE_COMMISSION = 0.10,
             MAINTENANCE_BASE_SALES = 1500.00,
             MAINTENANCE_COMMISSION = 0.06;

double       baseSalary = 0.00,
             pcSales = 0.00,
             printerSales = 0.00,
             accessorieSales = 0.00,
             maintenanceSales = 0.00,
             totalCommission = 0.00,
             totalDue = 0.00,
             noCommission = 0.00,
             pcCommission = baseSalary * PC_COMMISSION,
             printerCommission = baseSalary * PRINTER_COMMISSION,
             accessorieCommission =  baseSalary * ACCESSORIE_COMMISSION,
             maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;

int          idNumber; //salesman identification number

do
{
cout << "Please Enter Salesman Identification or -999 to Terminate ";
cin >> idNumber;

if (idNumber == -999)
    return 0;
else

cout << "Please Enter Salesman Base Salary\t\t\t  ";
cin >> baseSalary;

cout << "Please Enter Personal Computer Sales\t\t\t  ";
cin >> pcSales;

cout << "Please Enter Printer Sales\t\t\t\t  ";
cin >> printerSales;

cout << "Please Enter Accessories Sales\t\t\t\t   ";
cin >> accessorieSales;

cout << "Please Enter Maintenance Sales\t\t\t\t  ";
cin >> maintenanceSales;
cout << endl;
cout << endl;
cout << endl;
cout << endl;

cout << "\t\t\t  My Computer Company\n ";
cout << endl;
cout << "\t\t\t Commission Statement\n ";
cout << endl;
cout << "\t\t\t  Salesman Number " << idNumber << endl;
cout << endl;
cout << "\t\t   ********************************\n";
cout << endl;

cout << fixed;
cout << setprecision(2);

cout << left << setw(20) << "Product";
cout << right << setw(20) << "Sales Amount";
cout << right << setw(20) << "Commission";
cout << "\n" << endl;

cout << left << setw(20) << "Personal Computers";
cout << right << setw(20) << pcSales;

if (pcSales > PC_BASE_SALES)
    {
        double pcCommission = baseSalary * PC_COMMISSION;
        cout << right << setw(20) << pcCommission;
        cout << "\n" << endl;
        totalCommission += pcCommission;
    }
if (pcSales < PC_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    }

cout << left << setw(20) << "Printers";
cout << right << setw(20) << printerSales;

if (printerSales > PRINTER_BASE_SALES)
    {
        double printerCommission = baseSalary * PRINTER_COMMISSION;
        cout << right << setw(20) << printerCommission;
        cout << "\n" << endl;
        totalCommission += printerCommission;
    }
if (printerSales < PRINTER_BASE_SALES)
    {
        cout << right << setw(20) << noCommission;
        cout << "\n" << endl;
    }

cout << left << setw(20) << "Accessories";
cout << right << setw(20) << accessorieSales;

if (accessorieSales > ACCESSORIE_BASE_SALES)
    {
        double accessorieCommission =  baseSalary * ACCESSORIE_COMMISSION;
        cout << right << setw(20) << accessorieCommission;
        cout << "\n" << endl;
        totalCommission += accessorieCommission;
    }
if (accessorieSales < ACCESSORIE_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    }

cout << left << setw(20) << "Maintenance";
cout << right << setw(20) << maintenanceSales;
if (maintenanceSales > MAINTENANCE_BASE_SALES)
    {
        double maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
        cout << right << setw(20) << maintenanceCommission;
        cout << "\n" << endl;
        totalCommission += maintenanceCommission;
    }
if (maintenanceSales < MAINTENANCE_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    cout << endl;
    }

cout << endl;

totalCommission =  pcCommission + printerCommission + accessorieCommission + maintenanceCommission;
totalDue = baseSalary + totalCommission;

cout << left << setw(20) << "Total Commission";
cout << right << setw(40) << totalCommission;
cout << "\n" << endl;

cout << left << setw(20) << "Base Pay";
cout << right << setw(40) << baseSalary;
cout << "\n" << endl;

cout << left << setw(20) << "Total Due";
cout << right << setw(40) << totalDue;
cout << "\n" << endl;

    } while ( idNumber != -999);

system ("PAUSE");
return 0;

}

4

2 に答える 2

3

難読化された C++ の競争のために意図的にコードを書いていますか?

これ(いくつかの同様のものの例として):

while (printerSales > PRINTER_BASE_SALES)
{
    double printerCommission = baseSalary * PRINTER_COMMISSION;
    cout << right << setw(20) << printerCommission;
    cout << "\n" << endl;
    printerCommission += totalCommission;
    break;
}

とまったく同じです:

if (printerSales > PRINTER_BASE_SALES)
{
    double printerCommission = baseSalary * PRINTER_COMMISSION;
    cout << right << setw(20) << printerCommission;
    cout << "\n" << endl;
    printerCommission += totalCommission;
}

breakループ内で無条件であるため、ループは一度だけ実行されます。これはおそらく、そもそもあなたが望んでいたことです。

また、printerCommissionループ内のローカル変数であるため、これは何も役に立ちません。

    printerCommission += totalCommission;

もしかして:

    totalCommission += printerCommission;

また、コメントが言うように、doubleorを使用しfloatてお金を計算すると、最終的にはうまくいきません。浮動小数点値が誤って丸められることがあるためです。たとえば、2 進浮動小数点表現では、値 0.1、0.2、および 0.6、および 0.7 は、それぞれ 0.099999999999999、0.199999999999999、0.59999999999999、および 0.699999999999 になります。1/3 を 10 進数で表現しようとするのと同じで、0.333333333 と、何桁使っても「ちょうど 1/3」にはなりません。そのような値を使用して十分な計算を行うと、「間違った」結果になります。しかし、あなたはほぼ正しい価値を得る必要があります.

于 2013-06-29T17:52:52.483 に答える
0

ここに私が見つけた問題があります:

なしのelseステートメントは、次のステートメントのみを実行します {}

do
{
    cout << "Please Enter Salesman Identification or -999 to Terminate ";
    cin >> idNumber;

    if (idNumber == -999)
        return 0;
    else // <---  This else will cause only the next line to be executed
         //       since there is no '{' following it.

    cout << "Please Enter Salesman Base Salary\t\t\t  ";
    cin >> baseSalary;

デバッガーを使用してコードをステップ実行しましたか?

于 2013-06-29T19:06:54.593 に答える