Hello all,
I am needing to set this up for class and I can not figure out where I went wrong. No matter what door I put in, i.e. One, Two Three, Four, it always comes back with the answer for door one. I am new to this especially if else if statements. She does not want us to use switches. Thank you all.
First using a std::string for this could be very difficult. As it is you are only checking for two of the many combinations of characters for each "number", what happens if the user enters "tWo" or "twO", etc.
I would suggest that you use a menu, and ask the user to enter a number representing the menu item.
Also you are using an uninitialized variable (mystring) in your if() chain, wouldn't using "choice" be a better choice?
Also what would happen if the user would enter something like "five"? Hint: Your if() chain really should end with a simple else.
In function 'int main()':
10:9: error: expected unqualified-id before '=' token
11:6: error: expected unqualified-id before '=' token
24:9: error: 'choice' was not declared in this scope
Okay. No matter what I put in. It comes up with the if statement. You chose door 1. Can I still do the menu with words? She only wants up to use words.
#include <cctype> // <--- For "std::tolower() and std::toupper()" + others.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string choice;
//string mystring; // <--- Not needed.
//int = 0; // <--- You have no variable name.
cout << "Welcome to Let's Make a Deal!\n";
cout << "Choose your door to claim your prize.\n\n";
cout <<
"Door (one)\n""Door (two)\n""Door (three)\n""Quit (four)\n";
cout << "Enter your door choice as text: ";
cin >> choice;
for (size_t idx = 0; idx < choice.size(); idx++)
{
choice[idx] = std::tolower(choice[idx]);
}
if (choice == "one")
{
cout << "\nYou chose door #1\n";
cout << "You've been ZONKED!!!\n";
// or std::cout <<"\nYou chose door #1\nYou've been ZONKED!!!\n";
}
Lines 18 - 21 you do not need to start every line with the insertion operator, (<<). As strings as long as each line is in double quotes it works.
Lines 26 - 29 change anything that is upper case to lower case. The is no way to correct spelling problems, but you should add an "else" at the end to cover anything that is incorrect input.