gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:01:36 26 4
gpt4 key购买 nike

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

• 西兰花• 番茄• 奇异果• 羽衣甘蓝• 番茄酱

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

如果用户想要换出 bundle ,我的程序会在用户输入 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

这项工作仍在进行中,所以请放轻松。

最佳答案

您正在越界访问,例如

bundles[3] = randomResult;

因为你有声明

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

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

无论如何,您确定需要一个字符串 vector 数组吗?好像有点奇怪。

关于C++ 程序崩溃。不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39322421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com