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
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <windows.h>
#include <string>
#include <cstdlib>
#include <ctime>
usingnamespace 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");
}
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.
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.
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.
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.