Sign in to follow this  

A nice code I redid using the Switch/case strucutre

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this