Problems with my game in C++
Hi,
I have started writing a game in C++, my problems are what is happening below 1. The menu is very messy and i do not know how to center the text. 2. y or n dont work the way they should do (how do i fix it so user can enter y/Y/YES/yes and n/N/NO/No Please? 3. If user starts the game it will ask for there handle they wish to use, the problem is once asked if they wish to use that handle yes or no will appear the only option that will work is n for no. As the user should get some text output for when they press y. Please help me fix Also i know about the ostream files that i can't do it that way, i will fix that after i get this problem solved first. Also note this game is incomplete as i am still working on it. Thankyou Andy My code is below :- ------------------------------------------------------------------------------ Code:
// Space Empires BBS Door By Andrew WIlkie |
The problem is your switch statement. It isn't doing what you think it is. Here it is with comments explaining what it really does in capitals.
Code:
switch(choice) |
I'm not sure if there is another easier way of doing centred text in C++ but certainly one way of doing it would be to use the ncurses library: http://www.gnu.org/software/ncurses/
EDIT: I found this website which gives a breif overview of using the ncurses menu library: http://dickey.his.com/ncurses/ncurses-intro.html#menu |
Also, your variable names (c,d,n,y,h,etc.) make your code very hard to understand. Not to mention I'm not sure what this code is supposed to do:
Code:
ofstream outfile("player.dat,news.dat,names.dat,ranks.dat,hof.dat,ios::app"); If you wanted to open each file for append, did you consider: Code:
ofstream playerFile ("player.dat",ios::app); |
As was mentioned before, case statements in switch "loops" have to be terminated or will run through every case. This is done with a break statement or return. EX
switch (var) { case '1': { statement; statement; break; } case '2': { etc; } } also, you're code could really use some cleaning up. single letter variables are never really ok to use even though you can get away with them in small scope loops but for strings?!? A lot of little problems, quirks, and bugs can be easily never implemented with a practice of writing clean code. Just something to think about... |
Hi,
Thankyou for your reply, i have changed my code and have got it working, but i now have another little glitch if the user presses no in my program after being asked if they wish to use that name, it outputs the text that should only been seen if the user pressed yes. Please help me fix and also how would i change my code so it will take the user back to the main menu? Thankyou Note: this is not complete so dont expect anything to happen after the yes part apart from the output text. twirl My Code :- ----------------------------------------------------------------------- Code:
// Space Empires BBS Door By Andrew WIlkie |
First of all, it's way more complicated than it should be. Just like what I said before, you're just looking to introduce bugs this way (unclean/overly complicated code).
Why is choice a char? make it an int since it doesn't matter, and you don't have to screw with escape characters, etc. I don't exactly get what you're doing here. You're printing out menus, one after the other and only checking if they ever pick the `1` option? In the default, it's could be 2 or 3 because you never checked for those. I'm having a hard time keeping everything straight. Tried to help you but i think there's got to be huge parts missing because, honestly, it doesn't really make too much sense as a whole... |
You forgot to use braces with your if statements.
The syntax is : if(condition) { do_stuff... } You can omit the braces if and only if do_stuff... is a single line. So the line if(choice=='1') followed by statements not wrapped around braces checks whether choice=='1', does nothing with the result, and goes on and executes the following statements as if the 'if' statement had never been there in the first place. You haven't noticed this bug yet, but you also forgot to put "break;" at the end of your case statement. Some advice - try to find and read other people's code to get an idea of how to structure your programs and name your variables. This code looks really weird, and as was mentioned, that's a formula for bugs and code that you can't understand. |
this doesnt really solve anything, but should probably be used instead:
after you check for 'y', in the list of output you produce, you dont need to have a cout at every line. you can have all of that output in one cout command. ie: Code:
cout << "Well...Here you are. Just another new" << endl |
Code:
std::string play; Code:
// Start main menu Code:
switch(choice) Code:
Code:
if(choice=='1') cout <<name <<endl; cin >>name; it will not output the sentence you filled name with in the start. Code:
return 1; heres a tip(use it or dont, its up to you) to make code more readable with conditional statements etc i indent code like this: Code:
int func() happy coding;) |
Hi,
Thankyou very much for your reply's i have fixed the code and i am now working on the logical part :( the fun part hehe) . twirl :-) |
Maybe you should try writing it in strict C. I m not joking. Just try.
|
If I'm right in assuming that this is going to be a text adventure, then I think that C is going to be a pain. C++ streams make reading individual words easy whereas in C, scanf is a function which requires some thinking about before using it.
|
All times are GMT -5. The time now is 03:44 AM. |