patterncppMinor
Finding out players gender and name in text based adventure game
Viewed 0 times
gendertextadventureplayersgamenamebasedfindingandout
Problem
#include
#include
#define NL "\n"
int main(void) {
//Acquires players first name
std::string introOne = "Welcome to The World of Magick\nWhat is your name?\n";
std::cout > playerName;
//Asks for players gender
while(true) {
std::cout > playerGenderSelect;
//Sets gender depending on value entered
switch(playerGenderSelect) {
case 1:
playerGender = "male";
break;
case 2:
playerGender = "female";
break;
default:
std::cout << "Not a valid number, try again" << NL;
}
//If the player entered a valid number, break loop, if not, continue it.
if(playerGender == "male" or playerGender == "female") {
break;
}
else {
continue;
}
}
return 0;
}I wrote this code for a text based adventure game I'm making too keep me busy over the weekend.
I usually do this kind of boredom-project in python but decided to tackle it in C++. I was wondering if there were any obvious taboos or things that could obviously be optimized better.
Solution
I see a number of things that may help you improve your code.
Use objects
The code has two things that look like a menu. It also collects two pieces of data about a single player. Those things,
Prefer
Instead of using the old C-style
It's generally better and more type-safe to use a
However, I'd probably not define this particular one in either case and simply type
Use constant string concatenation
The code currently contains this:
But you don't really need to do it that way which potentially calls the
Which is both more readable and only calls
Use consistent formatting
The code as posted has inconsistent indentation which makes it hard to read and understand. Pick a style and apply it consistently.
Sanitize user input better
The code doesn't quite work as posted. If I enter a string such as "Edward" to answer a question, the program stays in an endless loop. It would be better to read a (text) line in and then convert it to a number. Users can do funny things and you want your program to be robust.
Omit
When a C or C++ program reaches the end of
Note: when I make this suggestion, it's almost invariably followed by one of two kinds of comments: "I didn't know that." or "That's bad advice!" My rationale is that it's safe and useful to rely on compiler behavior explicitly supported by the standard. For C, since C99; see ISO/IEC 9899:1999 section 5.1.2.2.3:
[...] a return from the initial call to the
For C++, since the first standard in 1998; see ISO/IEC 14882:1998 section 3.6.1:
If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;
All versions of both standards since then (C99 and C++98) have maintained the same idea. We rely on automatically generated member functions in C++, and few people write explicit
So I advocate omitting it; others disagree (often vehemently!) In any case, if you encounter code that omits it, you'll know that it's explicitly supported by the standard and you'll know what it means.
Use objects
The code has two things that look like a menu. It also collects two pieces of data about a single player. Those things,
menu and player could and probably should be objects. Perhaps you're a beginning programmer, and haven't learned about objects yet, but this kind of repeated task with associated data is really well-suited to object-oriented programming and that's something that C++ is very good at expressing. For an example of a usable menu object, see this answer.Prefer
const variables to #defineInstead of using the old C-style
#define as in this line:#define NL "\n"It's generally better and more type-safe to use a
const variable like this:static const std::string NL{"\n"};However, I'd probably not define this particular one in either case and simply type
\n where it's needed.Use constant string concatenation
The code currently contains this:
std::cout << "Are you a boy or a girl?" << NL << "1. Boy | 2. Girl"
<< NL << "Choices are selected by typing in a single number" << NL;But you don't really need to do it that way which potentially calls the
<< operator six times. Instead, you could express the same thing as this:std::cout << "Are you a boy or a girl?\n"
"1. Boy | 2. Girl\n"
"Choices are selected by typing in a single number\n";Which is both more readable and only calls
<< once. The compiler automatically concatenates the string literals together.Use consistent formatting
The code as posted has inconsistent indentation which makes it hard to read and understand. Pick a style and apply it consistently.
Sanitize user input better
The code doesn't quite work as posted. If I enter a string such as "Edward" to answer a question, the program stays in an endless loop. It would be better to read a (text) line in and then convert it to a number. Users can do funny things and you want your program to be robust.
Omit
return 0When a C or C++ program reaches the end of
main the compiler will automatically generate code to return 0, so there is no need to put return 0; explicitly at the end of main. Note: when I make this suggestion, it's almost invariably followed by one of two kinds of comments: "I didn't know that." or "That's bad advice!" My rationale is that it's safe and useful to rely on compiler behavior explicitly supported by the standard. For C, since C99; see ISO/IEC 9899:1999 section 5.1.2.2.3:
[...] a return from the initial call to the
main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.For C++, since the first standard in 1998; see ISO/IEC 14882:1998 section 3.6.1:
If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;
All versions of both standards since then (C99 and C++98) have maintained the same idea. We rely on automatically generated member functions in C++, and few people write explicit
return; statements at the end of a void function. Reasons against omitting seem to boil down to "it looks weird". If, like me, you're curious about the rationale for the change to the C standard read this question. Also note that in the early 1990s this was considered "sloppy practice" because it was undefined behavior (although widely supported) at the time. So I advocate omitting it; others disagree (often vehemently!) In any case, if you encounter code that omits it, you'll know that it's explicitly supported by the standard and you'll know what it means.
Code Snippets
#define NL "\n"static const std::string NL{"\n"};std::cout << "Are you a boy or a girl?" << NL << "1. Boy | 2. Girl"
<< NL << "Choices are selected by typing in a single number" << NL;std::cout << "Are you a boy or a girl?\n"
"1. Boy | 2. Girl\n"
"Choices are selected by typing in a single number\n";Context
StackExchange Code Review Q#141655, answer score: 4
Revisions (0)
No revisions yet.