• Problem with while loop in C++ console game.
    13 replies, posted
I'm having trouble with my text based game whereby it asks the player to enter a gender for their character (M or F and lowercase variants), here is the part of my code I'm having trouble with: [code] while (charGender != 'M' or charGender != 'm' or charGender != 'F' or charGender != 'f') { cout << "Please type a gender for your character (M or F): " << endl; cin >> charGender; } cout << "Correct Input."; [/code] The problem is it keeps asking for the gender and does not proceed out of the loop. By the way, charGender is a declared char variable. Any help would be appreciated thanks.
You need to use and there, Since it'd only exit the loop like that if it charGender was equal to 'M', 'm', 'F' and 'f' at the same time.
Ah sweet, using 'and' in place of the or's works thanks. One more question, what is a good way of limiting the length that someone could input a string for example this part in my code? (charName is a string variable) [code] cout << "Please type a name for your character (8 characters max): "; cin >> charName; [/code]
Since it's a char*, you could use strlen. Although, for user input. I'd suggest using std::string and getline(cin,str), since it allows for spaces, and is "safer" than C strings.
Could you give me an example? Here is my full source code so far. [code] #include <iostream> #include <cstdlib> #include <windows.h> using namespace std; string charName; char charGender; int main() { SetConsoleTitle("LittleRPG"); cout << "Welcome to LittleRPG. This game was created by Jalit." << endl << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15); cout << "Please type a name for your character (8 characters max): "; cin >> charName; system("cls"); while (charGender != 'M' and charGender != 'm' and charGender != 'F' and charGender != 'f') { cout << "Please type a gender for your character (M or F): " << endl; cin >> charGender; } cout << "Correct Input."; return 0; } [/code]
[code]while(charName.length() == 0 || charName.length() > 8) { cout << "Please type a name for your character (8 characters max): "; getline(cin, charName); }[/code] That should work.
Sweet man thanks for the help.
Good luck on your project! I look forward to seeing the results.
Also in many people's opinion, you should define your variables "charName" and "charGender" a bit later, and they are also a little strange names for variables too in my opinion. Well done so far though :).
I'm stumped again guys, I'm trying to create a menu system whereby the player can choose to either start a New game or read the instructions etc. here is the current source code: [code] #include <iostream> #include <cstdlib> #include <windows.h> using namespace std; string characterName; string characterGender; string menuSelection; void NewGame() { system("cls"); // Ask for a character name. while (characterName.length() == 0 || characterName.length() > 8) { cout << "Please type a name for your character (8 characters max): "; getline(cin, characterName); } system("cls"); // Ask for the character's gender. while (characterGender != "M" && characterGender != "m" && characterGender != "F" && characterGender != "f" || characterGender.length() > 1) { cout << "What gender is your character? (M or F): "; getline(cin, characterGender); } } void ReadMe() { system("cls"); cout << "Readme text goes here."; } int main() { // Menu screen SetConsoleTitle("LittleRPG"); cout << "Welcome to LittleRPG. This game was created by Jalit." << endl << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15); cout << "Type N to begin a New game." << endl << "Type R for Readme. " << endl; getline(cin, menuSelection); while (menuSelection == "N" || menuSelection == "n") NewGame(); while (menuSelection == "R" || menuSelection == "r") ReadMe(); return 0; } [/code] The problem I am having is that if the player types anything other than N, n, R or r. The application closes down. Also am I doing functions wrong or is any of the code inefficient or weird? Sorry this is my first proper attempt at a C++ application yet alone a game.
You don't want while-loops there, but instead a chain of if(..) else if(..) there. You can use a while-loop to ask for input if none was correct (that would be in the final else). And yes, the code is quite weird, but since you seem to be a beginner that is nothing I would worry about. I'll guess that you are using some resource to learn C++ from and if you just continue reading then you will see how to make things more efficient yourself.
I managed to fix it myself by using if statements for the menu selection then at the end I put main(); so it starts the main function over again.
Better to use a loop, or else you might get a stack-overflow. [url=http://en.wikipedia.org/wiki/File:Call_stack_layout.svg]This[/url] is a neat little picture of that and by calling main again, you'll add an additional entry.
a standard model for what you want is something like this [CODE]read menuSelection while(menuSelection != quitCondition){ do stuff read menuSelection } [/CODE]
Sorry, you need to Log In to post a reply to this thread.