reoutput main

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.
closed account (10oTURfi)
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();
}
Last edited on
okay, I think I get it. Thanks.

I updated it a bit, here's the whole code:


#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?
Last edited on
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.

You could put your loop like this:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
     loop:
code
code
code
if( myvariable = 0)
{
    goto loop;
}
return 0;
}
Just a few suggestions:

1) Use code tags and indentation. Its makes it easier to read and understand your code.
2) Instead of this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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();
	else if (choice1 == 2)
		WesternClearing();
	else if (choice1 == 3)
		SouthernClearing();
	else if (choice1 == 4)
		Tent();
	else if (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?
Last edited on
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... :)
You need a loop. You have already been given several examples, but here is a more complete one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;

void play()
  {
  if (rand() % 2) cout << "You win!\n";
  else            cout << "You loose!\n";
  }

bool playagain()
  {
  while (true)
    {
    cout << "Play again? ";
    string s;
    getline( cin, s );
    if (s.empty()) continue;
    switch (s[ 0 ]) {
      case 'Y': case 'y': return true;
      case 'N': case 'n': return false;
      }
    }
  }

int main()
  {
  srand( time( NULL ) );

  do {
    play();
  } while (playagain());

  cout << "Good-bye.\n";
  return 0;
  }

Hope this helps.
without you using the code formatting on the webpage its hard to read your code. One method is to do loops.


What your trying to do isn't really a loop.

@Goraan2012

Whatever you do, don't make loops using goto.
@ 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?
There is no point in keeping main() empty -- it is the entry point of the application. There should be code in main().
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(); }
Last edited on
@ Duoas: I thought so. I've got the first function called in main

How do I fix: Semantic Issue: Use of undeclared identifier 'Awakening'

I put the main above the Awakening sub function and got that error


Edit: Nevermind, I got it to work. Thanks for the help guys.

Edit: Any other tips? I'm open to any help available. I left out the dialogue to save space.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <string>
using namespace std;


void r_NorthClearing()
{
    int choice3, r_Awakening(), r_CenterClearing();
    cout << "descriptive writing" << endl;
    cin >> choice3;
    if (choice3 == 1)
        r_CenterClearing();
    if (choice3 == 2)
        cout << "descriptive writing" << endl;
    r_Awakening();
    if (choice3 == 3)
        cout << "descripitive writing" << endl;
        r_NorthClearing();
}

void r_WesternClearing()
{
    int choice1, r_Awakening(), r_CenterClearing();
    cout << "descriptive writing" << endl;
    cin >> choice1;
    if (choice1 == 1)
        r_CenterClearing();
    else
        cout << "descriptive writing " << endl;
   r_Awakening();
}

void r_SouthernClearing()
{
    int choice1, r_CenterClearing(), r_Awakening();
    cout << "descriptive writing" << endl;
    cin >> choice1;
    if (choice1 == 1)
        r_CenterClearing();
    if (choice1 == 2)
        cout << "descriptive writing" << endl;
    r_Awakening();
}

void r_Tent()
{
    int r_CenterClearing();
    cout << "descriptive writing" << endl;
    r_CenterClearing();
}

void r_FirePit()
{
    int r_CenterClearing();
    cout << "descriptive writing" << endl;
    r_CenterClearing();
}




void r_Awakening()
{
    int choice1;
    cout << "descriptive writing" << endl;
    cin >> choice1;
   if (choice1 == 1)
        r_NorthClearing();
   else if (choice1 == 2)
        r_WesternClearing();
   else if (choice1 == 3)
        r_SouthernClearing();
   else if (choice1 == 4)
        r_Tent();
   else if (choice1 == 5)
        r_FirePit();
    else
        cout << "That isn't an option. Look at the available answers and try again." << endl;
    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)
            r_NorthClearing();
        if (choice1 == 2)
            r_WesternClearing();
        if (choice1 == 3)
            r_SouthernClearing();
        if (choice1 == 4)
            r_Tent();
        if (choice1 == 5)
            r_FirePit();
    }
}

int main(void)
{
    r_Awakening();
}    

void r_CenterClearing()
{
    int chioce4;
    cout << "desriptive writing" << endl;
    cin >> chioce4;
    if (chioce4 == 1)
        r_NorthClearing();
    else if (chioce4 == 2)
        r_WesternClearing();
    else if (chioce4 == 3)
        r_SouthernClearing();
    else if (chioce4 == 4)
        r_Tent();
    else if (chioce4 == 5)
        r_FirePit();
    else
        cout << "That isn't an option. Look at the available answers and try again." << endl;
    while (chioce4 != 1 && chioce4 != 2 && chioce4 != 3 && chioce4 != 4 && chioce4 != 5) 
    {
        int choice5;
        cout << "That isn't an option. Look at the available answers and try again." << endl;
        cin >> choice5;
        if (choice5 == 1)
            r_NorthClearing();
        if (choice5 == 2)
            r_WesternClearing();
        if (choice5 == 3)
            r_SouthernClearing();
        if (choice5 == 4)
            r_Tent();
        if (choice5 == 5)
            r_FirePit();
    }
}
Last edited on
Sorry for the long post. I added a checkpoint system using a simple int addition, but can't end the program. Please help!
Line 23 and 35 are suspicious... What are you trying to do there?
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?
Topic archived. No new replies allowed.