为什么我的程序在尝试填充数组时崩溃?

Why is my program crashing when I'm trying to fill up an array?

所以对于这个程序,基本上,我要求三个人的名字,并将这些字符串存储在一个数组中。那部分似乎工作正常。

之后,我要了他们的季报。所以每个人得到 4 个,并通过数组排序,以便:

索引 0-3 归于 A

索引 4-7 属于 B

而索引 8-11 属于 C。

在处理第二个数组的第二个 for 循环中,我有一个 if/else if 语句列表,这些语句确定我将要询问的相关人员的姓名。在单独的函数 readSales() 中,我设置了一个类似的东西来确定要询问哪个季度。这被设计为循环 12 次以填充所有 12 个索引。

由于某种原因,我在输入人名后,程序崩溃了。知道为什么吗?

另外,我知道 "using namespace std;" 不是很受欢迎,但这就是我的教授想要的,所以我也有。

// In this program, I will
// (1) Ask for the names of three salespeople.
// (2) Accept sales for each quarter (for each person).
// (3) Display their name and total sales amount.

// I will use three functions:
// (1) main
// (2) readInfo 
// (3) displayInfo

// No global variables, and I will pass data as parameters.

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

// In order to pass an array through a void function,
// I must create it in the main function.

// So...

string readNames(int); // Function that gathers data from user.
double readSales(string, int);
void displayInfo(); // Function that displays final result.

int main() // Our main function
{
    // Create my variables for arrays and final result.
    string arrNames[3];
    double arrSales[12], result;

    // I must call my first function now.
    for (int i = 0; i < 3; i++)
    {
        arrNames[i] = readNames(i); // Successfully gathers all 3 names and stores them in the array. Woo!
    }



    // Now I must gather the number data, using a double array.
    string person = "uninitialized";

    int x = 0;
    for (x; x < 12; x++);
    {
        if (x < 4) // setup to ask question
            person = arrNames[0];
        if (x < 8)
            person = arrNames[1];
        if (x < 12)
            person = arrNames[2];

        arrSales[x] = readSales(person, x); // Successfully gathers all 12 quarters and stores them. Yay!
    }

    cout << arrNames[3];


} // end function main()

string readNames(int count)
{
    for (count; count < 3;)
    {
        string i;

        cout << "Please input salesperson " << count + 1 << "'s name: ";
        cin >> i;
        cout << endl;

        return i;
    }

    return 0;
} // end function readNames()

double readSales(string person, int count) // reading the sales
{
    double i; // variable I am returning at the end of function.
    int quarter;

    if (count == 0 || count == 4 || count == 8)
    {
        quarter = 1;
    }

    else if (count == 1 || count == 5 || count == 9)
    {
        quarter = 2;
    }

    else if (count == 2 || count == 6 || count == 10)
    {
        quarter = 3;
    }

    else if (count == 3 || count == 7 || count == 11)
    {
        quarter = 4;
    }
    else
        return 0;

    cout << "Please input salesperson " << person << "'s sales for Quarter " << quarter << " (Please round to the nearest cent): $" << endl;
    cin >> i;

    return i;
}

请去掉for (x; x < 12; x++);

中的分号

你可以这样做,

int x = 0;
for (x; x < 12; x++)
{
    if ((x >= 0) && ( x <= 3)) // setup to ask question
    {
        person = arrNames[0];
    }
    else if ((x >= 4) && (x <= 7))
    {
        person = arrNames[1];
    }
    else
    {
         person = arrNames[2];
    }


    arrSales[x] = readSales(person, x); // Successfully gathers all 12 quarters and stores them. Yay!
}