您的社區支持農業(CSA)農場每週向您家提供一盒新鮮水果和蔬菜。對於此編程項目,請定義BoxOfProduce類,其中包含三組水果或蔬菜。您可以將水果或蔬菜表示爲字符串類型的數組。添加適當的構造函數和訪問器/增變函數來獲取或設置存儲在陣列中的水果或蔬菜。還編寫一個輸出 函數,該函數在控制檯上顯示該框的完整內容。 接下來,編寫與從該列表中隨機抽取三個項目創建一個BoxOfProduce一個主要功能:C++程序崩潰。不知道爲什麼
•西蘭花 •番茄 •獼猴桃 •卡勒 •粘果酸漿
不要擔心,如果您的程序隨機選擇重複產品的三個項目。接下來,主要功能應該顯示盒子的內容,並且允許用戶用五種可能的水果或蔬菜中的任何一種來代替盒子中選擇的任何水果或蔬菜。在用戶完成替換之後,它應該輸出要交付的盒子的最終內容。那麼它應該詢問用戶是否想要創建另一個盒子,如果是,則應重複上述步驟。它應該繼續這樣做,直到用戶選擇不創建另一盒產品。 最後,爲您的類添加一個靜態變量,用於跟蹤創建的產品盒子總數以及返回該值的靜態函數。在主循環的每次迭代結束時在主函數中顯示此值。
我的程序在用戶輸入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
這仍然是一個正在進行的工作,所以請對我輕鬆點。
除了問題的答案可能會列出,「BoxOfProduce :: printContents」對它的調用者說謊,因爲它沒有返回值,但聲稱它的確如此。而'BoxOfProduce :: createBox()'在收集輸入的時候沒什麼意義,因爲if-block中的代碼聲明瞭一個無用的函數'printContents'(它不會調用你認爲可能的成員),然後調用'randomize()',它創建一個本地向量,並將其返回給調用者,忽略它。對於這些問題以及其他問題,我可以給出的最好建議是*將**編譯器警告轉變爲迂腐程度*,並*修復*標記的內容。 – WhozCraig