Is there a way to repeat the main function? Silly question, I'm sure, but I can't figure it out.
cout << "You are in a wooded area. There is a path to the east, west, and north. In the clearing you are in, there's a wooden table and benches, a tent, and a fire pit. What do you do?\n 1: Go north\n 2: go east\n 3: go west\n 4: examine tent\n 5: examine fire pit" << endl;
cin >> choice1;
if (choice1 == 1)
NorthernClearing();
if (choice1 == 2)
EasternClearing();
if (choice1 == 3)
WesternClearing();
if (choice1 == 4)
void main(void);
}
This is all that has a function, the rest of the code in the program is for the function.
You arent allowed to call main function from inside the program.
Thats why you should put it in while loop
1 2 3 4 5 6 7 8 9 10 11
while(choice1 != 4)
{
cout << "You are in a wooded area. There is a path to the east, west, and north. In the clearing you are in, there's a wooden table and benches, a tent, and a fire pit. What do you do?\n 1: Go north\n 2: go east\n 3: go west\n 4: examine tent\n 5: examine fire pit" << endl;
cin >> choice1;
if (choice1 == 1)
NorthernClearing();
if (choice1 == 2)
EasternClearing();
if (choice1 == 3)
WesternClearing();
}
#include <iostream>
#include <string>
using namespace std;
int choice1, choice2, choice3, choice4, choice5, choice6;
void NorthClearing()
{
cout << "Th path leads to another clearing. There's a well and a sign nearby. The well looks old, vines running up the sides of the stones. A rope is attached to a metal bar running the diameter of the roof, swaying slowly back and forth, presumably with the movement of water deep inside.\n What do you do\n 1: Go back\n 2: Look inside the well\n 3: Look at sign\n" << endl;
}
void WesternClearing()
{
cout << "The clearing is surrounded on all sides by woods, save the path that led here. The trees are too close together to pass through, and the way their branches twist towards you makes you feel as though getting too close would be a bad idea. Aside from the woods, there is little of note.\n What do you do?\n 1: Go back\n 2: Go towards trees\n" << endl;
}
void SouthernClearing()
{
cout << "The path leads to a rundown shack. The door is boarded up, and the window is either blocked or tinted, as you can't see inside. Benches line one wall, and there is a small backpack at one end of the benches." << endl;
}
void Tent()
{
cout << "The inside of the tent is as you would expect, a single sleeping bag laying on the ground. There is also a pack lying open on the ground, and miscellaneous bits of camping equipment strewn about the interior of the tent. None of it seems needed to you, so you leave it where it is, in case the owner of the tent comes back." << endl;
}
void FirePit()
{
cout << "After a quick analysis of the fire, consisting mostly of guesswork, you come to the conclusion that the pit was used recently. There may be markings in the dirt suggesting that there was at least one chair here recently. The pit itself is surrounded by small stones, and burned wood and ashes fill the space." << endl;
}
int main (void)
{
cout << "You awaken in a wooded clearing. You don't remember anything before waking up, and decide to look around before going anywhere. You see a tent, firepit, and a nearby sign. The are also paths to the north, west, and south.\n What do you do?\n 1: Go north\n 2: Go west\n 3: Go south\n 4: Look at tent\n 5: look at fire pit\n" << endl;
cin >> choice1;
if (choice1 == 1)
NorthClearing();
if (choice1 == 2)
WesternClearing();
if (choice1 == 3)
SouthernClearing();
if (choice1 == 4)
Tent();
if (choice1 == 5)
FirePit();
while (choice1 != 1 && choice1 != 2 && choice1 != 3 && choice1 != 4 && choice1 != 5)
{
cout << "That isn't an option. Look at the available answers and try again." << endl;
cin >> choice1;
if (choice1 == 1)
NorthClearing();
if (choice1 == 2)
WesternClearing();
if (choice1 == 3)
SouthernClearing();
if (choice1 == 4)
Tent();
if (choice1 == 5)
FirePit();
}
}
I need to find a way to recall the main function at the end of each subroutine, but if I can't call the main, then I've got no clue how to do it. Any ideas?
without you using the code formatting on the webpage its hard to read your code. One method is to do loops.
1 2 3
Loophere:
goto loophere;
"loophere" being anything you want it to be. I read of a lot of people bashing this method, but it has its applications, and short of making some huge program, I think it has its uses. Put the "loophere:" wherever you want the code to return, and put the "goto loophere;" in the code where you want it to return. the "goto" can also be put into if statements.
int main (void)
{
cout << "You awaken in a wooded clearing. (And the rest of your lines)\n" << endl;
cin >> choice1;
if (choice1 == 1)
NorthClearing();
if (choice1 == 2)
WesternClearing();
if (choice1 == 3)
SouthernClearing();
if (choice1 == 4)
Tent();
if (choice1 == 5)
FirePit();
while (choice1 != 1 && choice1 != 2 && choice1 != 3 && choice1 != 4 && choice1 != 5)
{
cout << "That isn't an option. Look at the available answers and try again." << endl;
cin >> choice1;
if (choice1 == 1)
NorthClearing();
if (choice1 == 2)
WesternClearing();
if (choice1 == 3)
SouthernClearing();
if (choice1 == 4)
Tent();
if (choice1 == 5)
FirePit();
}
}
Why not have something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
do
{
cin >> choice1;
if (choice1 == 1)
NorthClearing();
elseif (choice1 == 2)
WesternClearing();
elseif (choice1 == 3)
SouthernClearing();
elseif (choice1 == 4)
Tent();
elseif (choice1 == 5)
FirePit();
else
cout<<"That isn't an option. Look at the available answers and try again!";
}
while (choice1!= 1 && choice1!= 2 && choice1!= 3 && choice1!= 4 && choice1!= 5);
The reason is clear. It avoids unnecessary repetition of code.
3) Why are there 6 choices? (Your code is not complete I assume, but still..) And why are they global? Keep your variables local as far as possible. Its easier to keep track of them that way...
4) For your issue of wanting to repeat the main, I would suggest having no code in the main at all, and keeping even your spawning in another function. That way you can easily repeat the main...
I was thinking about that. I'm still messing with it, so I'm open to any other suggestions. How would I keep the main empty without getting an error, or having the program not work?
Daring to stray from the topic, I'd like to say that I like your way of descriptive writing... It paints quite a picture, and makes playing such text-based games a lot of fun... :)
@ Caprico: Thanks.
@ everyone else: ignoring aesthetic reasons, why are goto loops so bad? Also, how would I keep my main empty without getting a compiler error or having the program not run?
You can do what you originally wanted to do. C++ supports recursion (just not with the main function.) Do this instead if you ever want that quick and dirty behavior:
1 2 3 4 5 6 7 8 9 10 11 12
//recursive main
int r_main() {
blah(); blah();
if( x )
r_main(); //recursion, do "blah blah" again
else
done();
return 0;
}
//entree point
int main() { return r_main(); }
the variables are local, they don't interfere with one another, but I had to add the r_Center and Western clearing because the code doesn't work otherwise. I can't end the program though. Is there a way to do this without redoing the whole code line?