I am hapy look at the code I made today.

Started by
10 comments, last by CoryK 18 years, 7 months ago
// StatsUpgrade shop made by Crosis On September 3 2005 #include <iostream> using namespace std; int main () { int Hp; // This is the Player's Base Hp. int Defense; // This is the Players base defense. int Intellegence; // This is the Players base Inteleegence. int Gold; // This is the Players Base Gold. Hp = 20; Defense = 15; Intellegence = 10; Gold = 1000; cout <<" Hello and Welcome to My Shop.\n"; cout << " Is there Any Services I can Intrest you in.\n"; int choice; cout << " 1 - HpUpgrade.\n"; cout << " 2 - DefenseUpgrade.\n"; cout << " 3 - Intellegence.\n"; cin >> choice; if ( choice == 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 = Gold - 500; cout << " Your gold left is " << Gold << endl; Hp = Hp + 1; cout << " After the UpGrade You'r Hp is " << Hp << endl; } else cout << " Please come back When you want the Upgrade.\n"; } else if ( choice == 2 ) { cout << " I see that You are intrested in the Defense upgrade.\n"; cout << " It will cost 400 gold . for 1 point of Defense upgrade.\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 400 gold for the Upgrade.\n"; Gold = Gold - 400; cout << " Your gold left after Paying him is. " << Gold << endl; Defense = Defense + 1; cout << " Your defesne after the upgrade is " << Defense << endl; } else cout << " Please Come back when you have the Money.\n"; } else if ( choice == 3 ) { cout << " I see that you are Intrested in the Intellegence Upgrade.\n"; cout << " It will cost 100 for 1 point of Intellegence upgrade.\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 100 gold for the Upgrade.\n"; Gold = Gold - 100; cout << " Your gold left after paying him is " << Gold << endl; Intellegence = Intellegence + 1; cout << " Your Intellegence after the Upgrade is " << Intellegence << endl; } else cout << " Please come back When you have the Money.\n"; } return 0; }
Advertisement
Try using a switch instead. Much pweetier! [grin]
Not bad.


How long have you been learning C++? So, are you creating a Text Based game of some sort? Just wondering.


Well, I better get back to work on my code. I am creating a Pong Clone in SDL right now.


Later.

Keep up the good work.

EDIT:OH YEAH, I MEANT TO AGREE WITH THE OTHER GUY AND SAY USE A SWITCH. IT WILL LOOK ALOT BETTER AND WELL, IT WILL BE SHORTER.
Hi, welcome to the forums! Nice looking code u got there.... Looks like code for an RPG game if im correct. Keep at it, ur doin great!!

-

Oh yea, can u please giv me a positive rating?? People have been giving me negative ratings for no apperrant reason! How unfair. Im a nice guy.
Quote:Original post by xor
Try using a switch instead. Much pweetier! [grin]


Yes, please. switch/case statements are so much more elegant and easier to read than sifing through if statement upon if statemnet. Also, indent more as you dive deeper and deeper through the if statements. It lets the reader know when the statement ends and what code is part of what statement.
I have been learing c++ for about a week are so. Mybe a week ½. Right now I am not making a text based game but what I do is that when I read a char are a couple of pages in my Learn to programe C++. I take what I just read and Try to make stuff realted to games becasue my Dream in life is to be a Game programer. I still have a far way to go I know, But I feel I am making pretty good progress.
Oh yeah,
you can use the source tags. like this.

I will put your code in the source tag.


// StatsUpgrade shop made by Crosis On September 3 2005#include <iostream>using namespace std;int main (){int Hp; // This is the Player's Base Hp.int Defense; // This is the Players base defense.int Intellegence; // This is the Players base Inteleegence.int Gold; // This is the Players Base Gold.Hp = 20;Defense = 15;Intellegence = 10;Gold = 1000;cout <<" Hello and Welcome to My Shop.\n";cout << " Is there Any Services I can Intrest you in.\n";int choice;cout << " 1 - HpUpgrade.\n";cout << " 2 - DefenseUpgrade.\n";cout << " 3 - Intellegence.\n";cin >> choice;if ( choice == 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 = Gold - 500;cout << " Your gold left is " << Gold << endl;Hp = Hp + 1;cout << " After the UpGrade You'r Hp is " << Hp << endl;}elsecout << " Please come back When you want the Upgrade.\n";}elseif ( choice == 2 ){cout << " I see that You are intrested in the Defense upgrade.\n";cout << " It will cost 400 gold . for 1 point of Defense upgrade.\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 400 gold for the Upgrade.\n";Gold = Gold - 400;cout << " Your gold left after Paying him is. " << Gold << endl;Defense = Defense + 1;cout << " Your defesne after the upgrade is " << Defense << endl;}elsecout << " Please Come back when you have the Money.\n";}elseif ( choice == 3 ){cout << " I see that you are Intrested in the Intellegence Upgrade.\n";cout << " It will cost 100 for 1 point of Intellegence upgrade.\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 100 gold for the Upgrade.\n";Gold = Gold - 100;cout << " Your gold left after paying him is " << Gold << endl;Intellegence = Intellegence + 1;cout << " Your Intellegence after the Upgrade is " << Intellegence << endl;}else cout << " Please come back When you have the Money.\n";}



I did that by....[ source] and [/ source](without the spaces) It makes it ALOT easier to read.

Chad Smith
It looks good. [smile]

Two suggestions:

- Use the switch construct rather than a long string of ifs. It's easier to read and often more intuitive.

- It's unnecessary to write "Intelligent = Intelligent + 1". C++ provides two syntactic mechanisms for doing this concisely. They both ultimately add 1 to the variable:

Pre-increment: "++Intelligence;" This adds 1 to Intelligence immediately.
Post-increment: "Intelligence++;" This adds 1 to Intelligence after the current line is finished executing.

There are also the decrement equivalents, notated with a "--" as opposed to "++".
Good job. Personally, I would move all the code in the if sections into functions, and then call them when you want to use them instead of writing the code every time.

Example:
instead of:
 if ( choice == 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 = Gold - 500;cout << " Your gold left is " << Gold << endl;Hp = Hp + 1;cout << " After the UpGrade You'r Hp is " << Hp << endl;}elsecout << " Please come back When you want the Upgrade.\n";}

Perhaps have:
if (choice==1){some_function_that_does_everything_above();}


That way you can reuse it later on in the game, and can make it much easier to work with. Just a suggestion [grin]

Other than that, great job. It's always nice to see people's code [smile]
hippopotomonstrosesquippedaliophobia- the fear of big words
Look around for using shorthand operators. They reduce how much you have to type by a lot.

Instead of Gold = Gold - 500; you can use Gold -= 500;

This works with the four basic math operators (+ - * /)

This topic is closed to new replies.

Advertisement