Why Is This Happening?

Hello, I'm making a funny text based game and early into it I have a problem, starting on line 123 I want the user to choose from 1-4, but whenever I test it, it goes straight to the character creation regardless of what you press. Why is it doing this? Thanks

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
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <windows.h>
#include <string>
#include <cstdlib>
#include <ctime>


using namespace std;


int main()
{

    int x;
    int baseHealth = 10; //starting health
    int baseAtk = 2;     //starting attack
    int baseDef = 3;     //starting deffense
    int weapon;          //weapon attribues
    int gold;
    int exp;
    string name;
    string rightName;




    goto titlescreen;

    // intro
    system("title");

    system("color 9");
    cout <<"\n\n\n\n\n\n\n\n\n\n\n                                    Intel";
    Sleep(3000);
    system("color 1");
    Sleep(500);
    system("cls");

    Sleep(2000);
    system("color A");
    cout <<"\n\n\n\n\n\n\n\n\n\n\n                                    Nvidia";
    Sleep(3200);
    system("color 2");
    Sleep(500);
    system("cls");

     Sleep(2000);
    system("color F");
    cout <<"\n\n\n\n\n\n\n\n\n\n\n                            Game Studio Proudly Presents";
    Sleep(3500);
    system("color 7");
    Sleep(400);
    system("color 8");
    Sleep(400);
    system("cls");







    //Title Screen
    Sleep(3000);
    system("title");
    titlescreen:

    cout << "\n\n\n\n\n\n";
    cout << " TTTTTTTTT  HHHHHHH    EEEEE  GGGGGGG      A        MMMMMMM  EEEEEEE  EEEEEEEEE\n";
    cout << " T          H      H     E    G            A        M     M  E     E      E\n";
    cout << " T          H       H    E    G            A        M     M  E     E      E\n";
    cout << " T          H       H    E    G            A        M     M  E     E      E\n";
    cout << " T          H     H      E    G            A        M     M  E     E      E\n";
    cout << " TTTTTTT    HHHHHH       E    G            A        M     M  E     E      E\n";
    cout << " T          H            E    G            A        M     M  E     E      E\n";
    cout << " T          H            E    G            A        M     M  E     E      E\n";
    cout << " T          H            E    G            A        M     M  E     E      E\n";
    cout << " T          H            E    G            A        M     M  E     E      E\n";
    cout << " TTTTTTTTT  H          EEEEE  GGGGGGG      AAAAAAA  MMMMMMM  EEEEEEE      E\n";


    system("color 0f");
    system("color f0");
    system("color 0f");
    system("color f0");
    system("color 0f");
    system("color f0");
    system("color 0f");
    system("color f0");
    system("color 0f");
    system("color f0");
    system("color 0f");
    system("color f0");
    system("color 0f");
    system("color f0");
    system("color 0f");
    system("color f0");
    system("color 0f");


    cout << "\n\n                            Press Enter To Start";

    cin.get();
    Beep(900,100);
    Beep(1135,200);
    system("cls");

    //End of title screen



    //Main menu

    cout << "\n                                 Epic Loot\n\n\n\n\n\n";
    cout << " 1. Start Game\n\n 2. Load Game\n\n 3. Instructions\n\n 4. Options\n\n ";
    cin >> x;

    if(x == 1){
        cout <<"one";


    if(x == 2)
    cout << "\n Error";
    }

    cin.get();




    //character creation


    system("cls");
    do{
    cout << "\n Enter your name.\n ";
    cin >> name;
    cout << "\a";
    cout << "Your name is " << name << "?" << endl;
    cin >> rightName;


    }while (rightName == "n");






}
the game


I think you should read up a bit more before you try this. You don't have any structure to your program.


I am going to answer your question with another question. Why do you think it wouldn't go straight into character creation? What is stopping it?
Also, line 127 will never be true, the only way to get there is if x is 1.
Not to mention, jesus christ that's a lot of system commands. Each one of those is a call to your operating system. Go easy there, it's a text based game it shouldn't be taking up a gig of ram!
I have minimal C++ knowledge, I am writing this from what I know right now just for fun. What are some things I should not do that I have in this and do you have tips on structure that could help me out?

I think the cin.get(); is stopping it.

I thought if statements meant "do this if it's equal to this" in this situation.
Hahaha I know, but i just wanted it to be ridiculous!
1
2
3
4
5
6
if(x == 1)
{
    cout <<"one";
    if(x == 2)   //The only way to get this line is if x is 1, this will always be false.
        cout << "\n Error";
}

I think you misunderstand how to use if statements.

http://cplusplus.com/doc/tutorial/control/
So should I be using else?

If you do this the second line isn't always false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{

    int x;

    cin >> x;

    if(x == 1)
    cout <<"one\n";

    if(x == 2 )
    cout << "two\n";

    cout <<"next line of code";
    return 0;
}


It gets the user input, if it's 1 it runs the first one, if it's 2 it runs the second, skipping the above 2 lines.

Sorry if I'm misunderstanding you.
Last edited on
Right, but that's not what you did in your original code. I just reformated what you wrote before.
Oh I see what you were saying now, I understand that it will always be false and won't run that line unless the 2 is put in, but for some reason no matter what it always skips it, even the first one. That's why it was messed up because I was trying different things thinking I wasn't doing it correctly. Copy and paste it and try to run it, it just won't work! Maybe I'm missing something, but tell me if you can get it to work.
I'm confused what your really trying to do here at the end? x will never be able to be both 1 and 2, so you'd be better of with and if statement and then an else if statement. But even if x == 1 in the above code it will just print out "one" and then continue on, you haven't actually changed what the program will do depending on the value of x. Also if x==2 corresponds to the user wanting to load a game why does it just print "error"?
I only want the person to choose one of the options, not both. What your saying it will do doesn't happen and that's what I want it to do is only print out one or error, that's why I'm so confused. It completely skips line 123 - 127. Just run it yourself and you'll see what I mean. The reason I have just words right now is because I was seeing if it actually was working because it wasn't doing what I wanted before, but the error is there just to be funny, that's the point of this game.
Last edited on
I have it now, the problem was with my cin.get(); so I looked it up and apparently whenever you use one it leaves a trailing \n and it was picking it up and skipping it.

Thanks for all the help guys and sorry the confusion.
Last edited on
As far as program structure, if you look at the source code of a full-fledged program, it isn't all done in a procedural manner like you did, going from top to bottom and only containing your code in the main function. Keeping your program confined to the main function will make your code sloppier, harder to follow, and much longer. Introduce some classes and functions into your game, and try to put as little code into the main function as you can.
Topic archived. No new replies allowed.