class BoxOfProduce c++

Your Community Supported Agriculture (CSA) farm delivers a box of fresh fruits and vegetables to your house once a week. For this Programming Project, define the class BoxOfProduce that contains exactly three bundles of fruits or vegetables. You can represent the fruits or vegetables as an array of type string . Add accessor and mutator functions to get or set the fruits or vegetables stored in the array. Also write an output function that displays the complete contents of the box on the console. Next, write a main function that creates a BoxOfProduce with three items randomly selected from this list:

Broccoli
Tomato
Kiwi
Kale
Tomatillo

This list should be stored in a text file that is read in by your program. For now you can assume that the list contains exactly five types of fruits or vegetables. Do not worry if your program randomly selects duplicate produce for the three items. Next, the main function should display the contents of the box and allow the user to substitute any one of the five possible fruits or vegetables for any of the fruits or vegetables selected for the box. After the user is done with substitutions output the final contents of the box to be delivered.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

  class BoxOfProduce
{
  public:

      BoxOfProduce();
      void displayWord(int boxee);
      void input();
      void output();
      string Box[]; 
  private: 

      string full_list[5];
      static int count;

};
    int BoxOfProduce::count=0;
        BoxOfProduce::BoxOfProduce()
  {
    full_list[0] = ("Broccoli");
    full_list[1] = ("Tomato");
    full_list[2] = ("Kiwi");
    full_list[3] = ("Kale");
    full_list[4] = ("Tomatillo");
  }


 void BoxOfProduce::input()
    {

    }      
void BoxOfProduce::output()
{
    cout<<"your bundle: ";
    srand(time(0));
    for(int i=0; i < 3; i++) 
    {
        int boxee = rand()% 5;
        Box[i] = full_list[boxee]; 
        displayWord( boxee); 
    }
}
void BoxOfProduce::displayWord(int boxee)
{
    cout << Box[boxee]<<endl;
}
int main()      
{
    BoxOfProduce b1;
    b1.input();
    b1.output();
}


When I run the program, it shows no errors or exceptions, the program compiles, starts and then a Windows error pops up and says "The program has stopped running." Does anyone see anything wrong with this?
Last edited on
Please edit your post to put [code][/code] tags around your code.

It makes it easier to read, and shows line numbers which make it easier to point out issues.
What are you attempting to do when you saystring Box[];? Likewise, getting outside help may be an infringement of your school's scholarly trustworthiness strategy. (At the very least, you should refer to your sources.) Just take o look to some tutorials here - https://hackr.io/tutorials/learn-c-plus-plus
Line 15: How many strings do you think you're allocating? Why is Box public?

Line 18: full_list should occur once, not per BoxOfProduce.

Line 19,22: What is the purpose of count? You don't use it.

Lines 33-36: Per the instructions, you should be reading full_list from a text file.

Line 37-46: output() should not populate the BoxOfProduce each time it is called. The instructions say to call output twice. Once after initially populating the BoxOfProduce and again after allowing the user to change the selections.

Line 40: srand() is in the wrong place. It should be called once from main. Not each time output is called. Yes, I know you're only calling output() once, but if you change your program to call output twice per the instructions, you're going to reset the RNG each time you call output resulting in the same sequence of random numbers.

Line 52-57: You don't allow the user to change the contents of his box per the instructions.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Topic archived. No new replies allowed.