Jan 18, 2014 at 4:33pm UTC
Hello i just started learning classes and i encoutered a problem.
The program below is supposed to initialize each friut with a random color.
However it initializes them only with Red.Could someone correct my code please!
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Fruit{
public :
string color;
};
int main()
{
int intcolor;
srand(time(0));
Fruit fruitarray[10];
for (int i=0;i<10;i++)
{
intcolor=rand()%5;
switch (intcolor)
{
case 0:fruitarray[i].color="Orange" ;
case 1:fruitarray[i].color="Yellow" ;
case 2:fruitarray[i].color="Green" ;
case 3:fruitarray[i].color="Purple" ;
case 4:fruitarray[i].color="Red" ;
}
}
for (int i=0;i<10;i++)
{
cout<<i<<' ' <<fruitarray[i].color<<endl;
}
return 0;
}
Last edited on Jan 18, 2014 at 4:33pm UTC
Jan 18, 2014 at 4:47pm UTC
thanks a lot i forgot to put break !