c ++程序崩溃。不知道为什么

时间:2016-09-04 23:43:06

标签: c++ vector

您的社区支持农业(CSA)农场每周向您的家中提供一盒新鲜水果和蔬菜。对于此编程项目,请定义包含三个水果或蔬菜捆绑的BoxOfProduce类。您可以将水果或蔬菜表示为字符串类型的数组。添加适当的构造函数和访问器/ mutator函数以获取或设置存储在数组中的水果或蔬菜。也写一个输出 功能,在控制台上显示该框的完整内容。 接下来,编写一个主函数,创建一个BoxOfProduce,其中包含从此列表中随机选择的三个项目:

•西兰花 • 番茄 • 猕猴桃 • 羽衣甘蓝 •Tomatillo

如果您的程序随机选择三个项目的重复产品,请不要担心。接下来,主要功能应显示盒子的内容,并允许用户用五种可能的水果或蔬菜中的任何一种替代为盒子选择的任何水果或蔬菜。在用户完成替换之后,它应该输出要传递的框的最终内容。然后它应该询问用户是否想要创建另一个框以及是否 是的,它应该重复上述步骤。它应该继续这样做,直到用户选择不创建另一盒产品。 最后,向类中添加一个静态变量,用于跟踪创建的产品盒的总数以及返回该值的静态函数。在主循环的每次迭代结束时在main函数中显示此值。

如果用户想要换出捆绑包,我的程序会在用户输入Y后进入removeStuff()函数。一旦它到达并且用户键入他们想要移除的水果/蔬菜,程序就会关闭。我不确定为什么会这样。非常感谢任何帮助。

#include <iostream>
#include "BoxOfProduce.h"
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <memory>
#include <algorithm>

using namespace std;

int main()
{
char myChar;
string answer = "";

srand(time(0));

BoxOfProduce bo;
bo.randomize();

cout << "Your box initially starts with: " << endl;
cout << bo.random << endl;

vector<string> randomResult = bo.randomize();
for (vector<string>::const_iterator iter = randomResult.begin(), iterEnd = randomResult.end();
        iter != iterEnd; ++iter){
    cout << *iter << endl;
    }

cout << "Would you like to swap out any of your bundles for any of the five     bundles you didn't get? (Y/n) " << endl;
    getline(cin, answer);
    if(answer.length() == 1){
        myChar = answer[0];
    }
    if(myChar == 'y' || myChar == 'Y'){
        cout << "Okay!" << endl;
        bo.removeStuff();
    }else if(myChar == 'n' || myChar == 'N'){
        BoxOfProduce bo1;
        bo1.createBox();
    }else{
        cout << "That is not a valid character. Goodbye." << endl;
        return 0;
    }

}

---------------------------------------------------------------------

#include "BoxOfProduce.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <memory>
#include <algorithm>


using namespace std;

BoxOfProduce::BoxOfProduce()
{
}

vector<string> BoxOfProduce::randomize()
{
    srand(time(0));
    string choices[] = {"Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo"};

    vector<string> random;
    for(int i = 0; i < 3; i++)
    {
        random.push_back(choices[rand() % 5]);
    }
    return random;
}

vector<string> BoxOfProduce::printContents(vector<string> bundles[3])
{
    cout << "Your box contains these three bundles: " << endl;
    cout << bundles << endl;
}

void BoxOfProduce::createBox(){
    cout << "Would you like to create another box? (Y/n)" << endl;
    getline(cin, answer);
    if(answer.length() == 1){
        myChar = answer[0];
        if(myChar == 'y' || myChar == 'Y'){
        vector<string> printContents();
        randomize();
    }
    }

}

void BoxOfProduce::removeStuff()
{
    cout << "Of your three bundles, what would like to remove?" << endl;
    cin >> answer;
    vector<string>::iterator result = find(randomResult.begin(),  randomResult.end(), answer);
    if(answer == "Tomato" || answer == "tomato" || answer == "broccoli" || answer == "Broccoli" || answer == "kiwi" || answer == "Kiwi" || answer == "kale" || answer == "Kale" || answer == "tomatillo" || answer == "Tomatillo"){
        randomResult.erase(result);
        bundles[3] = randomResult;
        addStuff();
    }else{
        cout << "That is not a choice!" << endl;
    }
}

void BoxOfProduce::addStuff()
{
    cout << "Now that we have removed a bundle, what would you like to swap that out for: Tomato, Broccoli, Kiwi, Kale, or Tomatillo?" << endl;
    getline(cin, answer);
    if(answer == "Tomato" || answer == "tomato" || answer == "broccoli" || answer == "Broccoli" || answer == "kiwi" || answer == "Kiwi" || answer == "kale" || answer == "Kale" || answer == "tomatillo" || answer == "Tomatillo"){
        randomResult.push_back(answer);
        bundles[3] = randomResult;
        printContents(bundles);
    }else{
        cout << "Sorry, you can't add that." << endl;
    }
}

-----------------------------------------------------------------

#ifndef BOXOFPRODUCE_H
#define BOXOFPRODUCE_H
#include <iostream>
#include <string>
#include <vector>
#include <memory>

using namespace std;


class BoxOfProduce
{
    public:
        BoxOfProduce();
        string getBundles();
        void setBundles(string b);
        vector<string> randomize();
        string bundleOfFruit();
        vector<string> printContents(vector<string> bundles[3]);
        string random;
        void createBox();
        void removeStuff();
        void addStuff();

    private:
        vector<string> bundles[3];
        vector<string> choices[5];
        char myChar;
        string answer = "";
        vector<string> randomResult;
};

#endif // BOXOFPRODUCE_H

这仍然是一项正在进行的工作,所以请放轻松我。

1 个答案:

答案 0 :(得分:2)

您正在访问越界,例如

bundles[3] = randomResult;

因为你有声明

vector<string> bundles[3]; // 3 elements, last one is indexed by 2

并且在C或C ++中,索引从0开始,因此数组bundles的最后一个元素应为bundles[2]

无论如何,你确定需要一个字符串向量数组吗?这看起来有点奇怪。