Problems with the goto statement

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <string>
using namespace std;


int main ()
{
    goto test2;
string action_string;
string deci_string;
string shop_string;
    int hp;
    int maxhp;
    int mhp;
    int att;
    int matt;
    int currexp;
    int expg;
    int lv;
    int z;
    int gold;
    int potion;
    int herb;
    int weapon;
    potion = 0;
    gold = 25;
    z = 0;
     maxhp = 100;
     hp = 100;
     mhp = mhp + 3 + matt * 2;
     att = att + lv * 5;
     matt = matt + mhp / 3;
     currexp = currexp + expg;
     lv = 1;
      
     cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+";
     cout << "\n";
     cout << "+                          Dragon Hunter                        +";
     cout << "\n";
      cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+";
      cout << "\n\n\n\n";
      cout << "Controls:\n-Type the commands on the side. Case is imporatant.";
      cout << "\n-You gain a level when your experience is full. Level ups give \n you more health, and attack power. \n-Gold is used to by new equipment and sleep at the inn.";
      cout << "\nType OK to continue.";
      cout << "\n";
      z = z + 1;
      
      
      while (z = 1)
      {
      cin >> deci_string;
      if (deci_string == "OK")
      {
       cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nYour adventure starts in the town of Ravenhome. \nYou're a young man, whos destiny is woven into the fabric of time.\n";

       if (z = 2)
      {
         ravenhome;
          cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
    cout << " " << endl;
    cout << "You are in the town square of Ravenhome. Here are your options: " << endl;
    cout << "Hunt - Traverse the fields, to find monsters." << endl;
    cout << "Shop - Enter the market to buy equipment." << endl;
    cout << "Travel - Travel to a different town." << endl;
    cout << "Stats - Check statistics." << endl;
    cout << " " << endl;
    cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
    cout << " " << endl;
    cout << "Which do you choose? \n";
    cin >> action_string;
    
    if (action_string == "Stats"){
                      cout << "You're Health is ";
                      cout << hp;
                      cout << " out of ";
                      cout << maxhp;
                      cout << ".\n";
                      cout << "You have ";
                      cout << gold;
                      cout << " Gold.\n";
                      cout << "You're inventory contains:\n";
                      cout << potion;
                      cout << " Potions";
                      cout << z;
}


if (action_string == "Shop")
    {
    test2; 
    cout << "Here is a list of items you may purchase\n\n";
    cout << "     Items        \n" ;
    cout << "------------------\n" ;
    cout << "-Potion, Ten Gold.\n\n" ;
    cout << "      Weapons     \n" ;
    cout << "------------------\n" ;
    cout << "-Dagger, Twenty Five Gold\n" ;
    cout << "-Baselard, One Hundred Fifty Gold\n";  
    cin >> shop_string;
    
    
if (shop_string == "Potion") {
  if (gold >= 10){
            potion = potion + 1;
   gold = gold - 10;
   cout << "That will be Ten Gold";
   goto shop;
}
else {
     cout << "You don't have enough Gold";
   
}
}
if (shop_string == "Dagger") {
   if (lv >= 2)    {
                   
    weapon = 1;
    gold = gold - 25;
}
    else {
        cout << "You need to be a higher level\n" ;
        }
        
if (shop_string == "Baselard") {
   if (lv >= 4)    {
                   
    weapon = 2;
    gold = gold - 150;
}
    else {
        cout << "You need to be a higher level\n" ;
     
     
      
}
if (shop_string == "Leave") {
                goto ravenhome;
                }
}
}
}
}
}


else {
         cout << "Unknown Command: ";
         cout << action_string;
         cout << "\nTry again.\n";
         
         cin >> action_string;
         }
}
    cin.get();
    return 0;

}


For some odd reason, I can't get the goto function to work at all.
I've even just c&p'd a tutorial off the internet, and it doesn't work.
It says that they're all unidentified.
You shouldn't be using goto statements. Though there are very rare cases where they can be used usefully, they should generally be avoided. There is usually going to be a much better way. And the gotos are very poorly laid out in this program.

Though I will tell you the compiler is complaining because when you are making the label, the statement is finished with a colon (:) and not a semi-colon (;).

This apparent "tutorial" should really be an example of how not to program, as opposed to a useful tool for learning. Aside from the syntax errors, the very first line in the entry point of the program calls a goto before initializing any variables, variables that are used in the code that was skipped to.
Last edited on
Ah. Well, I just threw that in there to test it. I was wanting to skip all the variables.
Thank you for clearing that up. It's odd, because all the tutorials I found online, use ;, not :.
Well, I suppose I better start re-writing things then....
Topic archived. No new replies allowed.