Produce like fruit or vegetable

Using dev c++ I am building a class the generates 3 bundles of fruit or vegetables. I am stuck on the issue of producing more that one output fruit or veggie from the randomly selected array. I only produce one element from the array.

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
58
59
60
61
62
63
64
65
66
  //Demonstrating the class box of produce output random produce 3 bundles
#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()
{
	int i;
	cout<<"your bundle: ";
	for( i=0; i < 3; i++)
	{
		Box[i] = full_list[i];
	}
	srand(time(0));
	 int boxee = rand()% 5;
	displayWord( boxee);

}
void BoxOfProduce::displayWord(int boxee)
{
	cout << Box[boxee]<<endl;
}
int main()		
{
	
	BoxOfProduce b1;
	b1.input();
	b1.output();
}
const string Broc = "Broccoli";
const string Tom = "Tomato";
const string Kw = "Kiwi";
const string Ka = "Kale";
const string Til = "Tomatillo";
closed account (o3hC5Di1)
Hi there,

There are a few problems with your code:

string Box[]; //line 16

In your output() member function you state that a Box will contain 3 items (your for loop runs three times), so you should probably declare your array as such. It would be advisable to use a const variable:

const int BOX_SIZE = 3;

Which you can then use as: string Box[BOX_SIZE]; //line 16 That makes a lot more sense to someone reading your code.

Now, let's look at your output member function:

1
2
3
4
5
6
7
8
9
10
11
12
13
void BoxOfProduce::output()
{
	int i;
	cout<<"your bundle: ";

	for( i=0; i < 3; i++) //this is your loop, which will run three times
	{
		Box[i] = full_list[i];
	}
	srand(time(0)); //this is the code which selects a random product and prints it
	 int boxee = rand()% 5;
	displayWord( boxee);
}


Why is the code which selects a random product and prints it outside of the for-loop? This will cause it to run only once, after the loop has already run. You are meaning to fill the Box with random products, so what you actually want to do is:

1
2
3
4
5
6
7
for( i=0; i < 3; i++) //this is your loop, which will run three times
	{
		srand(time(0)); //you only need to do this once, so this could be outside of the loop
	 	int boxee = rand()% 5; //make random number
                Box[i] = full_list[boxee]; //add it to the Box
		displayWord( boxee); //display it
	}


Hope that helps.

All the best,
NwN
Success, this will certainly help. Now I can continue to clean up this program to look presentable.
Topic archived. No new replies allowed.