Sign in to follow this  

Need help with the If Structure

This topic is 4493 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

Hello Everone I was wondering if someone could help me with my source code. I am still very new to c++ and I cant get my prog to work the way i want it to. I am only on ch2 of my book which is Control Structures. The funny thing thing is this I got my if/else Strucure to work. Which I will show the source code to but then I tired to get a regual if structure to work, I know that a If structure has only one enterce and one exit but I was trying to nesting to work. but it failed well here is my fist if/else selection Structure. ________________________________________________________________________________ //job chooser created on August 31 2005 By Crosis #include <iostream> using namespace std; int main ( ) { cout << " Here are the avaiable Job's.\n"; cout << " 1 - Mage.\n"; cout << " 2- Warrior.\n"; cout << " 3 - Priest.\n"; int choice; cout << " Please enter the number above for the job you want.\n"; cin >> choice; if ( choice == 1) cout << " You have chosen the way of a a magic user, go learn some spell's.\n"; else if ( choice == 2 ) cout << " You have Chosen the way of a Fighter, go kill some demon.\n"; else if (choice == 3 ) cout << " You have chosen The way of a Healer. God Heal some people.\n"; return 0; } I know this is not the best way to choose a job for a game but like I sayed I am new to c++ and I was just seeing If I understood what I was reading. Please comment if u thing I should change this code around. P.s this is the If/else Selection Structure. This is not the one I am having trouble with. ________________________________________________________________________________ // JobStatus.cpp created on August 31 2005 By Crosis #include <iostream> using namespace std; int main () { int Mage_Hp; //Mage Current beginning hp He has. int Mage_Defense; //Mage Current Beginning Defense He has. int Mage_Agility; // Current Beginning Agility The mage has. int Mage_Intellgence; // Current begining Intellgence the mage has. int Mage_Dexerity; // Current beginning Dexerity The Mage has. int Mage_WillPower; // Current Beginning Willpower the mage has. int Mage_Luck; // Current Beginng Luck the Mage has. int Warrior_Hp; //Warrior Current beginning Hp. int Warrior_Defense; //Warrior Current beginning Defense he has. int Warrior_Agility; //Warrior current beginning Agility he has. int Warrior_Intellgence; //Warrior Current Beginning Intelgence. int Warrior_Dexerity; // Beginning Dexerity the Warrior has. int Warrior_WillPower; // Warrior Current Beginning Willpower. int Warrior_Luck; // Warrior Current beginning Luck. int job; cout << "Please Enter the number of you'r Job For your beging Stats.\n"; cout << " 1 - Mage.\n"; cout << " 2 - Warrior.\n"; cout << " 3 - Priest.\n"; cin >> job; Mage_Hp = 20; Mage_Hp = Mage_Hp + 10; Mage_Defense = 5; Mage_Defense = Mage_Defense + 6; Mage_Agility = 4; Mage_Agility = Mage_Agility + 7; Mage_Intellgence = 15; Mage_Intellgence = Mage_Intellgence + 10; Mage_Dexerity = 10; Mage_Dexerity = Mage_Dexerity + 4; Mage_WillPower = 11; Mage_WillPower = Mage_WillPower + 4; Mage_Luck = 3; Mage_Luck = Mage_Luck + 2; Warrior_Hp = 20; Warrior_Hp = Warrior_Hp = 10; Warrior_Defense = 15; Warrior_Defense = Warrior_Defense + 5; Warrior_Agility = 9; Warrior_Agility = Warrior_Agility + 3; Warrior_Intellgence = 5; Warrior_Intellgence = Warrior_Intellgence + 2; Warrior_Dexerity = 11; Warrior_Dexerity = Warrior_Dexerity + 4; Warrior_WillPower = 7; Warrior_WillPower = Warrior_WillPower + 2; Warrior_Luck = 1; Warrior_Luck = Warrior_Luck + 1; if ( job == 1 ) cout << " Your beginning Mage Stats are.\n"; cout << " Mage_Hp is " << Mage_Hp << endl; cout << " Mage_Defense is " << Mage_Defense << endl; cout << " Mage_Agility is " << Mage_Agility << endl; cout << " MageIntellgence is " << Mage_Intellgence << endl; cout << " Mage_Dexerity is " << Mage_Dexerity << endl; cout << " Mage_WillPower is " << Mage_WillPower << endl; cout << " Mage_Luck is " << Mage_Luck << endl; if ( job == 2) cout << " Your Beginning Warrior Stats are.\n"; cout << " Warrior_Hp is " << Warrior_Hp << endl; cout << " Warrior_Defense is " << Warrior_Defense << endl; cout << " Warrior_Agility is " << Warrior_Agility << endl; cout << " Warrior_Intellgence is " << Warrior_Intellgence << endl; cout << " Warrior_Dexerity is " << Warrior_Dexerity << endl; cout << " Warrior_WillPower is " << Warrior_WillPower << endl; cout << " Warrior_Luck is " << Warrior_Luck << endl; return 0; } What I wanted to do was this, I wanted the person to be able to put in there job so if they where a Mage warrior are priest it would show the beginning stat's. It worked fine for the Mage but when I want to go to the Warrior Selection it showed both of the Stats like it sayed Your beging mage stats are, and then listed the beging mage stats. And then it went on to The beging Warrior stats are. I was trying to get it just to show the class stats themselfes Any help on this would be greatly appercited. Thank's in Adavnce.

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy
You need curly braces ({ and }) around the if body.


To expand upon that, if you do:

if(condition)
statement1;
statement2;


only statement1 is part of the if statement. If you want both statement1 and statement2 to be part of the if statement, you have to group them in a block.

if(condition)
{
statement1;
statement2;
}

Share this post


Link to post
Share on other sites
I have one more question if you guys dont mind me asking. I am using visual c++ 6.0 complier. My os is windows xp pro ed. I was wondering this when ever I complie a source code and send it to someone it exits out as soon as they hit enter like the text scrools really fast and then it exits out. I think it has to do with the line return 0; casue that is if the person hit enter it will exit the program. I was wondering how do I get around this so that the person can actully see how the program works?

Share this post


Link to post
Share on other sites
Quote:
Original post by RebornCrosis
I have one more question if you guys dont mind me asking. I am using visual c++ 6.0 complier. My os is windows xp pro ed. I was wondering this when ever I complie a source code and send it to someone it exits out as soon as they hit enter like the text scrools really fast and then it exits out. I think it has to do with the line return 0; casue that is if the person hit enter it will exit the program. I was wondering how do I get around this so that the person can actully see how the program works?


The program runs and immediately exits afte receiving input which is why the window closes. Since you're using windows you can stick a system("PAUSE"); before return 0; which will pause. There might be a few people charging in here later telling you to write a .bat instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by RebornCrosis
-snip-
I was wondering how do I get around this so that the person can actully see how the program works?


The easiest way would be to run the program from the command prompt. Simply type cmd in the Run dialog and execute it from there.

Also, a suggestion on your code. Right now you're using two sets of variables, one of which might not even ever see use. Consider setting it up so that it only uses one set relevant to the character, and modify that.

For example:

#include <iostream>
#include <string>
using namespace std;

int main()
{
const int BaseHP = 10;
const int BaseDefense = 5;
const int BaseAgility = 5;
const int BaseIntelligence = 5;
const int BaseDexterity = 5;
const int BaseWillPower = 5;
const int BaseLuck = 5;

string CharJob = "None";
int CharHP = BaseHP;
int CharDefense = BaseDefense;
int CharAgility = BaseAgility;
int CharIntelligence = BaseIntelligence;
int CharDexterity = BaseDexterity;
int CharWillPower = BaseWillPower;
int CharLuck = BaseLuck;

int job;

cout << "Select a profession:\n"
<< "\t1 - Mage.\n"
<< "\t2 - Warrior." << endl;
cin >> job;

if ( job == 1 )
{
CharJob = "Mage";
CharHP += 20;
CharDefense += 6;
CharAgility += 6;
CharIntelligence += 20;
CharDexterity += 9;
CharWillPower += 10;
CharLuck += 0;
}

else if ( job == 2 )
{
CharJob = "Warrior";
CharHP += 20;
CharDefense += 15;
CharAgility += 7;
CharIntelligence += 2;
CharDexterity += 10;
CharWillPower += 4;
CharLuck += -3;
}

cout << " Your beginning " << CharJob << " Stats are:\n"
<< " HP is " << CharHP << "\n"
<< " Defense is " << CharDefense << "\n"
<< " Agility is " << CharAgility << "\n"
<< " Intelligence is " << CharIntelligence << "\n"
<< " Dexterity is " << CharDexterity << "\n"
<< " Will Power is " << CharWillPower << "\n"
<< " Luck is " << CharLuck << endl;

return 0;
}




Once you really get into it, you'll have all sorts of fun with stuffing all this data into an object. Good luck with your coding!

Share this post


Link to post
Share on other sites
Thank you ever one for Helping me Get this to work. and also for you'r idea's on mod the source code. I understand most of what you wrote tychon
, And I thank you for showing me a diffretn way to do it. I cant wait until I get up to ch 6 thats where i get in (oop) it is going to be so cool.

Share this post


Link to post
Share on other sites
Here is an example what an if else ladder is for...

cout << "Select an option (1-3):" << endl;
int opt;
cin >> opt;

if(opt == 1) { /*do this*/ }
else if(opt == 2) { /*do this*/ }
else if(opt == 3) { /*do this*/ }
else { /*if no option selected do this*/ }


Share this post


Link to post
Share on other sites

This topic is 4493 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this