I've tried to make a small game where you choose what are you planting, with a money system.
I created the function soldSystem in which I use switch for type of plants and how much each costs. And in main I print the info about the plants and with an if I've printed the money. It doesn't work as I want. An example: I have 100$ and with 50 i planted somthing, and when I sold it I have 150$, but when I plant another thing, it starts from 100$ again.
int soldSystem(int moneyNum) {
int money;
money = 100;
int i;
i = 1;
while (i < 2) {
switch (moneyNum)
{
case 0: {
if (money >= 50) {
cout << "Tomatoes planted\n";
money = money - 50;
cout << "You have :" << money << " Dollars" << endl;
cout << "\n";
cout << "Tomatoes Sold\n";
money = money + 100;
}
else cout << "Don't have enough money\n";
}
break;
case 1: {
if (money >= 100) {
cout << "Carrots planted";
money = money - 100;
cout << "You have:" << money << " Dollars" << endl;
cout << "\n";
cout << "Carrots Sold\n";
money = money + 300;
}
else cout << "Don't have enough money\n";
}
break;
case 2: {
if (money >= 300) {
cout << "Mushrooms planted\n";
money = money - 500;
cout << "You have:" << money << " Dollars" << endl;
cout << "\n";
cout << "Mushrooms Sold\n";
money = money + 1000;
}
else cout << "Don't have enough money\n";
}
break;
default:
cout << "Try again\n";
money = money;
break;
}
i++;
}
return money;
}
int main()
{
int i,playerChoose;
i = 1;
while (i > 0) {
cout << "Choose what to plant\n";
cout << "0-Tomatoes(50/100);1-Carrots(100/300);2-Mushrooms(500/1000)\n";
cin >> playerChoose;
if (-1 < playerChoose < 3) {
cout << "You have:" << soldSystem(playerChoose) << endl;
cout << "\n";
}
}
cout <<"You have:"<<soldSystem << endl;
return 0;
}