简体   繁体   English

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

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

So with this program, basically, I ask for the names of three people, and store those strings in an array. 因此,基本上,使用该程序,我要求三个人的名字,并将这些字符串存储在数组中。 That parts seems to work fine. 该部分似乎工作正常。

After that, I ask for their quarterly reports. 之后,我要求他们提供季度报告。 So each person gets 4, and it's ordered through the array so that: 因此,每个人得到4,并通过数组进行排序,以便:

Index 0-3 goes to person A 索引0-3转到人员A

Index 4-7 goes to person B 索引4-7转到人B

And index 8-11 goes to person C. 而索引8-11转到C人。

In this second for-loop that processes this second array, I have a list of if/else if statements that determine the name of the person in question that I will be asking for. 在处理第二个数组的第二个for循环中,我有一个if / else if语句列表,这些语句确定了我要询问的相关人员的姓名。 In the separate function itself, readSales(), I have a similar thing set up to determine which quarter to ask for. 在单独的函数readSales()中,我进行了类似的设置来确定要查询哪个季度。 This is designed to loop 12 times in order to get all 12 indexes filled. 它设计为循环12次,以填充所有12个索引。

For some reason, after I input the people's names, the program crashes. 由于某些原因,在输入人名后,程序崩溃。 Any idea why? 知道为什么吗?

Also, I know "using namespace std;" 另外,我知道“使用命名空间std;”。 isn't very popular, but that's how my professor wants it so that's how I have it. 并不是很受欢迎,但这就是我的教授想要的,所以我就是这样。

// 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;
}

Please remove the semi-colon in the for (x; x < 12; x++); 请删除for (x; x < 12; x++);的分号for (x; x < 12; x++);

You can make it like this, 你可以这样

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!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM