Public Group

# A nice code I redid using the Switch/case strucutre

This topic is 4766 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Sorry guys afteri posted it I noticed a huge bug in the source code, So I took it down to fix this problem. I fixed the problem I forgot to put the break; after the case is done.
#include <iostream>
using namespace std;

int main ()
{
int Hp; // Is the Player's Base Hp.
int Defense; //Is the Player's Base.
int Agility; // This is the Players Base Agility.
int Gold; //This is the Players Base gold.

Hp = 20;

Defense = 10;

Agility = 12;

Gold = 1000;

cout << " Hello and welcome to my Shop.\n";
cout << " Is there Anything That Intrest you?\n";
int choice;
cout << " 1 - Hp UpGrade.\n";
cout << " 2 - Defense UpGrade.\n";
cout << " 3 - Agility UpGrade.\n";
cin >> choice;

switch ( choice ) {
case 1:
cout << " I see that you are Intrested in the HpUpgrade.\n";
cout << " It Will cost 500 gold for 1 point of Hp.\n";
cout << " Do you want to do this?.\n";
int choice;
cout << " 1 - Yes.\n";
cout << " 2 - No.\n";
cin >> choice;

if ( choice == 1 )
{
cout << " That will be 500 gold.\n";
Gold -= 500;
cout << " Your gold after Paying the man is " << Gold << endl;
Hp += 1;
cout << " Your Hp after the Upgrade is " << Hp << endl;

}
else
cout << " Please come back when you have the money.\n";
break;
case 2:
cout << " I see that you are intrested in the Defense Upgrade.\n";
cout << " It will cost 400 gold, for 1 Point of Defense.\n";
cout << " Do you want to do this?\n";
int pick;
cout << " 1 - Yes.\n";
cout << " 2 - No.\n";
cin >> pick;

if ( pick == 1 )
{
cout << " That will be 400 Gold.\n";
Gold -= 400;
cout << " Your gold left after paying the man is " << Gold << endl;
Defense += 1;
cout << " Your Defense is after the Upgrade " << Defense << endl;
}
else
cout << " Please come back when you have the money.\n";
break;

case 3:
cout << " I see that your intrested in the Agility Upgrade.\n";
cout << " It will cost 900 gold for 1 point of Agility.\n";
cout << " Do you wantto do this?\n";
int Choose;
cout << " 1 - Yes.\n";
cout << " 2 - No.\n";
cin >> Choose;

if ( Choose == 1 )
{
cout << " That will be 900 gold.\n";
Gold -= 900;
cout << " Your gold left after paying the man is " << Gold << endl;
Agility += 1;
cout << " Your Agiltiy after the upgrade is " << Agility << endl;
}
else
cout << " Please come back when you have the money.\n";
}
system ("PAUSE");
return 0;
}



##### Share on other sites
Looks pretty good.

Maybe, you could make some functions for it, so it could be easier. Just a suggestion though.

Nice job though. Keep on coding.

good job!

##### Share on other sites
Nice, now try making it so that the player must have enough money before they can buy something...As it stands now, your player could easily end up with negative money after a transaction (if you weren't limited to one iteration of dealing). If you don't believe me, just test it out yourself by setting Gold to say 100 instead of 1000.

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
15
5. 5

• 10
• 9
• 14
• 9
• 33
• ### Forum Statistics

• Total Topics
632592
• Total Posts
3007295

×