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

时间:2015-12-09 21:34:51

标签: c++ arrays for-loop crash

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

之后,我要求他们提供季度报告。所以每个人得到4,并通过数组排序,以便:

指数0-3转到A人

指数4-7转到B人

指数8-11指向人C.

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

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

另外,我知道“使用命名空间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;
}

1 个答案:

答案 0 :(得分:0)

请删除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!
}