C++ 某些输入的数组错误

C++ Array error with certain inputs

本文关键字:数组 错误 输入 C++      更新时间:2023-10-16

因此,在此程序中,当用户输入例如"shirt"作为销售项目的名称时,它会直接跳到输出并失败。 但是,如果我输入一个简单的数字,例如销售项目的名称仅为 10,则一切正常。 任何帮助将不胜感激

编辑:该功能已从更大的程序中拉出以发布。 在主程序中,它是一个浮点函数,而不是一个 int,并且仍然给出错误

#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int main()
{
double taxPct;
char status;
double saleAmount, value;
do
{
    system("cls");  // clears the screen when the user runs the program again
    saleAmount = 0; // resets sale amount to 0 when the user runs the program again
    cout
    <<"********************************************" <<endl
    <<"********************************************" <<endl
    <<"*****" << right << setw(39) << "*****" <<endl
    <<"*****" << setw(24) << "W E L C O M E" << setw(15) << "*****" <<endl
    <<"*****" << setw(39) << "*****" <<endl
    <<"*****" << setw(23) << "T O   T H E" <<setw(16) << "*****" <<endl
    <<"*****" << setw(39) << "*****" <<endl
    <<"*****" << setw(30) << "S A L E S   R E C E I P T" <<setw(9) << "*****" <<endl
    <<"*****" << setw(39) << "*****" <<endl
    <<"*****" << setw(24) << "P R O G R A M" <<setw(15) << "*****" <<endl
    <<"*****" << setw(39) << "*****" <<endl
    <<"********************************************" <<endl
    <<"********************************************" <<endl
    <<endl;
    int items;
    cout << "How many sales items do you have? : ";
    cin >> items;
    int sales[items][2];
    int counter =0;
    for (int counter = 0; counter < items; counter++)
        {
            cout << "Enter the name of sales item " << counter + 1 << ": ";
            cin >> sales[counter][0];
            cout << "Enter the price of " << sales[counter][0] << " : $";
            cin >> sales[counter][1];
            saleAmount=saleAmount+sales[counter][1];
        }
    cout << "Enter in the sales tax percentage" <<endl
         << "(Enter 10 for 10%): ";
    cin >> taxPct;
    cout <<endl <<endl;
    if (taxPct>.9999)
    {                        // failsafe: converts tax percentage to a decimal for calculating tax amount if a whole number is entered (i.e. entering .06 or 6 will give the same result)
        taxPct=taxPct/100;
    }
    double taxAmount = saleAmount * taxPct;
    double grandTotal = saleAmount + taxAmount;
    cout << fixed << setprecision(2)
    <<"********************************************" <<endl
    <<"********" << setw(26) << "S A L E S  R E C E I P T" << setw(10) << "********" <<endl
    <<"********************************************" <<endl
    <<"**" << setw(42) << "**" <<endl
    <<"**" << setw(42) << "**" <<endl
    <<"**  Item" << setw(26) << "Price" << setw(10) << "**" <<endl
    <<"**  ------------------------------------  **" <<endl;
    for (counter = 0; counter < items; counter++)
    {
    cout <<"**  " << left << setw(12) << sales[counter][0] << right << setw(11) << "$" << setw(9) << sales[counter][1] << setw(8) << "**" <<endl;
    }
    cout
    <<"**" << setw(42) << "**" <<endl
    <<"**" << setw(42) << "**" <<endl
    <<"********************************************" <<endl
    <<"**" << setw(42) << "**" <<endl
    <<"**" << setw(42) << "**" <<endl
    << left << setw(4) << "**" << setw(11) << "Total Sales" << right <<setw(12) << "$" << setw(9) << saleAmount << setw(8) <<"**" <<endl
    << left << setw(4) << "**" << setw(9) << "Sales Tax" << right <<setw(14) << "$" << setw(9) << taxAmount << setw(8) <<"**" <<endl
    << left << setw(27) << "**" << setw (15) << "-----------" <<"**" <<endl
    << left << setw(4) << "**" << setw(11) << "Grand Total" << right << setw(12)<< "$" << setw(9) << grandTotal << setw(8) <<"**" <<endl
    <<"**" << setw(42) << "**" <<endl
    <<"**" << setw(42) << "**" <<endl
    <<"********************************************" <<endl <<endl <<endl;
    cout <<"Do you want to run this program again? (Y/N): "; // asks user if they wish to calculate another sale
    cin >> status;
    cout <<endl;
}
while (status == 'Y' || status == 'y');
return 0;
}
你需要

设计你的数据结构。您似乎想要存储销售信息,并且每个项目,名称和价格都有2件事。因此,让我们制作一个销售项目

struct SalesItem
{
std::string Name;
int Price; // maybe there is a better type, but int will do
}

现在让我们分配一些

SalesItem sitems[items];

好的,现在让我们输入一个

    cout << "Enter the name of sales item " << counter + 1 << ": ";
    cin >> sitems[counter].Name;
    cout << "Enter the price of " << sitems[counter].Name << " : $";
    cin >> sitems[counter].Price;

你应该考虑 std::vector 而不是数组。

你应该使用一种设计用于持有货币的类型(谷歌 c++ 货币类型)