C ++:结构向量并传递它们-错误:进程返回-1073741819(0xC0000005)

时间:2019-02-02 00:57:46

标签: c++ vector struct

以下程序应创建一个结构向量,并将其传递给两个函数。老实说,我对传递此结构向量的所有不同方式以及执行该操作的表示感到困惑。无论哪种方式,我都想出了这个程序,该程序可以编译,但是在运行时却给我错误。具体来说,一输入收入金额(函数中的第一个cin)。

const int SIZE = 2;

// structure of floats named TaxPayer
struct TaxPayer
{
    float taxRate;
    float income;
    float taxes;
};

// prototypes for taxTaker and taxPrint functions
void taxTaker(vector<TaxPayer>&);
void taxPrint(vector<TaxPayer>&);

int main()
{
    // driver
    // vector of type TaxPayer named "citizen"
    std::vector<TaxPayer>citizen(SIZE);
    taxTaker(citizen);
    taxPrint(citizen);

    return 0;
}

有问题的功能之一:

void taxTaker(vector<TaxPayer> &citizen)
{
    int loops = 1;
    bool loopFlag = true;

    // asks for an validates inputs depending on vector size (it's 2 in this     
       case so it asks for inputs from 2 different people)
    do
    {
        cout << "Enter this year's income for tax payer #" << loops << ": " << endl;

        // validates entered income
        do
        {
            cin >> citizen[loops].income;
            if (std::cin.fail() || citizen[loops].income <= 0)
            {
                std::cout << "\nInvalid income. Amount must be over 0" << std::endl;
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
            }
            else
            {
                loopFlag = false;
            }
        }
        while (loopFlag);

        cout << "Enter the tax rate for tax payer #" << loops << ": " << endl;

        // validates entered tax rate
        do
        {
            cin >> citizen[loops].taxRate;
            if (std::cin.fail() || citizen[loops].taxRate < 0.01 || citizen[loops].taxRate > 9.9)
            {
                std::cout << "\nInvalid tax rate. Amount must be over 0.01 and under 9.9" << std::endl;
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
            }
            else
            {
                loopFlag = false;
            }
        }
        while (loopFlag);

        // should calculate the final tax for each person
        citizen[loops].taxes = citizen[loops].income * citizen[loops].taxRate;
        loops++;
    }
    while (loops <= SIZE);

    }

0 个答案:

没有答案