Public Group

# Introduction and Help with C++

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

## Recommended Posts

Hello! This is my first post here. I began learning C++ a short while ago(as in three days) from a few online tutorials at Cprogramming. I soon plan on picking up a book, but I am here to ask you about a short game I am making to practice my If Statements and simple typing in C++. Please look the code over, and compile and run it if you have the time. This is only a small portion of what I want the game to be, but I am running into a problem: I am having to use different numbers for the selectors(you will see what I mean in the code.), while I would like to use 1 and 2 for every choice. Please give me some constructive criticism on shortcuts, etc. as well. Thanks!

The Code:

#include <iostream>

using namespace std;

int main()
{
int intro;
int choice1;
int choice2;
int choice3;

{
cout<<"LEVERAGE: A JOURNEY THROUGH THE MIND\n";
cout<<"A game by Jared Henson\n";
cout<<"Press 1 to continue.\n";

cin>>intro;

if (intro == 1)
{cout<<"You awaken in a dark room. Assuming it is your bedroom, you roll over.\n";
cout<<"You hear a strange noise outside.\n";
cout<<"1. Turn on the light.\n";
cout<<"2. Go back to sleep.\n";} //The first choice.

}

{
cin>>choice1;

if (choice1 == 1)
{cout<<"You struggle blindly for a few seconds before reaching the lightswitch.\n";
cout<<"You realize that this is not your bedroom after all. The walls are a dark,\n";
cout<<"ugly brown. The room is lit by a single hanging bulb, characterized only by\n";
cout<<"a bed and a white nighstand. There is a black door in the corner.\n";
cout<<"1. Exit the bedroom.\n";
cout<<"2. Search the nightstand.\n";} //Option 1 for choice 1.

else if (choice1 == 2)
{cout<<"You fall asleep, Never to awake.\n";
cout<<"GAME OVER\n";}//Option 2 for choice 1.
}
{
cin>>choice2;

if (choice2 == 1)
{
cout<<"You open the door. Before you is a wide ocean, stretching as far\n";
cout<<"as the eye can see. The house seems to be suspended twenty or\n";
cout<<"thirty feet above the sea. To your left, a dangerous looking\n";
cout<<"balcony stretches around the house. Dark shapes move in the water below.\n";
cout<<"1. Jump into the sea.\n";
cout<<"2. Take the balcony.\n";}//Option 1 for choice 2.

if (choice2 == 2)

{

cout<<"Upon investingation of the nightstand, all drawers seem to be jammed\n";
cout<<"except for the top two. You pull the top drawer open, and gaze inside.\n";
cout<<"It looks to be some sort of window. On the other side is a sprawling field\n";
cout<<"of flowers, with many hills. The sun is setting. You pull the bottom drawer\n";
cout<<"open, revealing a small town, covered in snow. It is Christmas.\n";
cout<<"3. Enter the top drawer.\n";
cout<<"4. Enter the bottom drawer.\n";}//Option 2 for choice 2.

}
{
cin>>choice3;

if(choice3 == 1)

{cout<<"You leap into the water. After treading water for a few minutes,\n";
cout<<"you peer under, just in time to see a mammoth shark\n";
cout<<"racing towards you. It swallows you whole. You get the courage\n";
cout<<"to open your eyes, and realize that you are in fact sitting on\n";
cout<<"a park bench, in a crowded city. A homeless man beckons from\n";
cout<<"across the street.\n";
cout<<"1. Go join the homeless man.\n";
cout<<"2. Stay on the bench.\n";}

else if (choice3 == 2)

{
cout<<"You take a few tentative steps out onto the balcony.\n";
cout<<"Gaining confidence, you begin to walk faster,\n";
cout<<"which shatters the thin metal structure. You plunge\n";
cout<<"downward, expecting to hit ocean but landing instead\n";
cout<<"on rough wood.You look up to see the operater of the\n";
cout<<"makeshift raft, a small, old man with a flowing white\n";
cout<<"beard. He reaches in one of his clay pots and extracts\n";
cout<<"an apple, which he hands to you.\n";
cout<<"3. Eat the apple.\n";
cout<<"4. Throw the apple into the ocean.\n";}

else if (choice3 == 3)

{
cout<<"You enter the top drawer, amazed at the landscape before you\n";
cout<<"At the top of a hill, you notice a stange object, which reveals\n";
cout<<"itself to be a table. On it is a cake, and a small bottle.\n";
cout<<"5. EAT ME.\n";
cout<<"6. DRINK ME.\n";
}

else (choice3 == 4);
{
cout<<"You enter the bottom drawer. Your bare feet crunch against\n";
cout<<"the hard snow. You look into the first house. A family is\n";
cout<<"happily sipping drinks around the fire. The small boy turns\n";
cout<<"and smiles at you. You move on to a second house.\n";
cout<<"Two very unhappy looking children are seated on the couch,\n";
cout<<"gazing with no apparent intrest at you. The house is cold and\n";
cout<<"dark, and the parents are no where to be found.\n";
cout<<"7. The First House\n";
cout<<"8. The Second House\n";
}

}

##### Share on other sites
else (choice3 == 4);[/quote]

If this is your final else statement, you don't need a conditional, just "else{" will work. Otherwise make this an "else if".

There are a myriad of ways (with varying complexity) that this can be redesigned, but for 3 days of C++ it at least functions. That should be your goal: make it work. Then you can start to look into making it work better/faster.

You might want to check out switch statements, though they're really just if/else chains under the hood.

##### Share on other sites
First of all put the source in code tags, just click on the insert code snippet!

Second of all code readability, there is no reason your couts shouldn't be like this:

cout << "Nice Code!" << endl;

Also the duplicate braces that weren't needed make it weird to read.

Now for your problem, I would use a loop and a seperate story variable to say where the player is in the story, using a switch statement and not use the choices they make to say where they are in the story.

 int main() { bool isFinished = false; int storyPoint = 0; int choice = 0; while(!isFinished) { switch(storyPoint) { case 0: cout << "Leverage: A Journey Through the Mind" << endl; cout << "A game by Jared Henson" << endl; cout << "Press 1 to continue. Or 9 to exit at any time." << endl; cin >> choice; if(choice == 1) { storyPoint = 1; } break; } if(choice == 9) { cout << "Thanks for playing!" << endl; break; } } } 

This is a little hacky but will work perfectly fine for what you need until you learn more!

Disclaimer: This code was written in the gamedev.net editor and has not been compiled this means it may contain little random errors, but the idea still stands.

##### Share on other sites

First of all put the source in code tags, just click on the insert code snippet!

Second of all code readability, there is no reason your couts shouldn't be like this:

cout << "Nice Code!" << endl;

Also the duplicate braces that weren't needed make it weird to read.

Now for your problem, I would use a loop and a seperate story variable to say where the player is in the story, using a switch statement and not use the choices they make to say where they are in the story.

 int main() { bool isFinished = false; int storyPoint = 0; int choice = 0; while(!isFinished) { switch(storyPoint) { case 0: cout << "Leverage: A Journey Through the Mind" << endl; cout << "A game by Jared Henson" << endl; cout << "Press 1 to continue. Or 9 to exit at any time." << endl; cin >> choice; if(choice == 1) { storyPoint = 1; } break; } if(choice == 9) { cout << "Thanks for playing!" << endl; break; } } } 

This is a little hacky but will work perfectly fine for what you need until you learn more!

Disclaimer: This code was written in the gamedev.net editor and has not been compiled this means it may contain little random errors, but the idea still stands.

tried to implement this but I failed catastrophically haha.

##### Share on other sites

[quote name='CRYP7IK' timestamp='1299540594' post='4783026']
-snip-

tried to implement this but I failed catastrophically haha.
[/quote]

What did you try? What errors did you get? What don't you understand?

##### Share on other sites
It is very important to allign yoor code using spaces, tabs (whitespace), it makes your code look clearer, don't forget this

##### Share on other sites

[quote name='Supurstamin' timestamp='1299546183' post='4783058']
[quote name='CRYP7IK' timestamp='1299540594' post='4783026']
-snip-

tried to implement this but I failed catastrophically haha.
[/quote]

What did you try? What errors did you get? What don't you understand?
[/quote]

I copied the code, compiled, and ran it and it worked perfectly. I put it into the game, adjusting a few integers and whatnot, and ran it. It was a blank screen. Went back to the original and ran it again. Also blank screen. May be a compiler problem, but I don't know.

##### Share on other sites
Save yourself a huge headache later on down the line and stop naming variables choice1 choice2 choice3 etc cause eventually when you are doing

choice1 = 2;
choice2 = choice1;
choice3 = 1;

You are going to screw up and set an integer to the text value of the variable or vice versa and it is going to be very hard to spot.

##### Share on other sites
why not use a data file to load the text and stuff?

Binary:
RapidShare: http://rapidshare.co...venture.tar.bz2

Source:
RapidShare: http://rapidshare.co...eSource.tar.bz2

Edit: to compile the code without codeblocks you will have to set tinyxml as include directory and define win32 as compiler define.

##### Share on other sites

why not use a data file to load the text and stuff?

Binary:
RapidShare: http://rapidshare.co...venture.tar.bz2

Source:
RapidShare: http://rapidshare.co...eSource.tar.bz2

Edit: to compile the code without codeblocks you will have to set tinyxml as include directory and define win32 as compiler define.

Relax. He just started learning C++... >.>

• 40
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631371
• Total Posts
2999607
×