Strings
Example Problems
The following examples demonstrate common string problems. These examples aim to help you better understand string manipulation before proceeding to create a text-based adventure in the next section.
These examples illustrate different approaches to solving string-related problems in C++, showing how to manipulate strings effectively and efficiently.
Text-Based Game
Create a text-based game that runs in the terminal. The content of your game is entirely up to you, as long as it remains appropriate. Your game must meet the following requirements:
- User Interaction: Your game must prompt the user for actions using
std::cout
. - Input Handling: Each user action must be read as a string using
getline(std::cin, some_string_variable)
. - Input Validation: The program must verify the validity of each user action. For example, if the program asks the user to enter “north” or “south,” it should ensure the input matches one of those options.
- Handling Invalid Input: When the user enters an invalid action, the program must notify them with an appropriate message and prompt them again. This will require the use of loops.
- Dynamic Storyline: The game’s storyline must evolve based on the user’s chosen actions. You’ll need to use conditional statements (e.g., if statements) to implement this behavior.
- Multiple Prompts: The game must include at least three distinct prompts, each with its own set of valid actions. Note: The user doesn’t need to encounter all three prompts in a single playthrough. Some actions may end the game before all prompts are reached.
Here are some example runs of Alexander Guyer’s text-based game:
./a.outYou wake up and find yourself in a desert. You feel weak and nauseous.There appears to be an oasis in the distance. What do you do?
Type "walk toward oasis" to walk toward the oasis, or "walk into desert"to walk away from the oasis, deeper into the desert:
drink water
That's not a valid action!
Type "walk toward oasis" to walk toward the oasis, or "walk into desert"to walk away from the oasis, deeper into the desert:
walk toward oasis
You begin stumbling toward the oasis... After hours of walking,the oasis appears just as far away as when you started.You're dehydrated, but you've stumbled across a desert cactus.What do you do?
Type "walk toward oasis" to continue walking toward the oasis.Type "drink cactus water" to drink water from the cactus.
drink cactus water
You drink the cactus water. It tastes acrid.Even worse, cactus water is apparently not meant for human consumption.You feel weak...Your legs give out, and you pass out in the hot desert sand.
Game over.
./a.outYou wake up and find yourself in a desert. You feel weak and nauseous.There appears to be an oasis in the distance. What do you do?
Type "walk toward oasis" to walk toward the oasis, or "walk into desert"to walk away from the oasis, deeper into the desert:
walk into desert
You feel uneasy about the oasis, suspecting it to be a hallucination.You turn around and walk deeper into the desert.After hours of walking, you come across a well-kept road running fromnorth to south, but you see the peak of a building emerging on the easternhorizon. You feel dehydrated, and you're probably too exhausted to reachit... What do you do?
Type "go east" to fight the odds and walk through the sand toward the building.Type "go north" to walk north along the road.Type "go south" to walk south along the road.Type "lay down" to lay down in the middle of the road.
lay down
You recognize that you're nearing your limits.You choose to lay down in the road to conserve energy,hoping that help will arrive soon...
Your decision pays off.A caravan of merchants arrives from the north, apparently on their way tosell their wares in a distant town to the south.They offer you water and escort you to safety.