Help please

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <iostream>
#include <string>

using namespace std;

int main()
{

	string = choice;
	int = 0;

	cout << "Welcome to Let's Make a Deal!" << endl;
	cout << "Choose your door to claim your prize." << endl;
	cout << endl;

	cout << "Door (one)\n"
		<< "Door (two)\n"
		<< "Door (three)\n"
		<< "Quit (four)\n" << endl;
	cout << endl;

	cout << "enter your door choice as text: ";
	cin >> choice;
	string mystring; 
		if (mystring == "one" || mystring == "One")
		{
			
			cout << "You chose door #1" << endl;
			cout << "You've been ZONKED!!!" << endl;
		}
		else if (mystring == "Two" || mystring == "two")
		{
			
			cout << "You chose door #2" << endl;
			cout << "You won a Trip to Italy! Congrats!!" << endl;
		}
		else if (mystring == "Three" || mystring == "three")
		{
			
			cout << "You chose door #3" << endl;
			cout << "You won $1000.00 Cash" << endl;
		}
		else if (mystring == "Four" || mystring == "four")
		{
		
			cout << "You chose not to play" << endl;
			cout << "Thank you for joining us today" << endl;
		}
	return 0;
}
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.

Post your real code.

What you posted won't even compile.
1
2
3
4
5
 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.
Can I still do the menu with words?


Since the "words" are std::string, yes.
salem that is my code. On visual studio it is running just fine.
I doubt very much that the code as presented "runs just fine", since there are syntax errors on lines 10 and 11.

Perhaps part of your problem is that the code you're really running is not what you think.


Hello jadams0904,

When I put the code in VS2017 it did not run. I had the same errors that jlb mentioned.

On line 24 the "cin" is to choice then on line 25 you create a new string variable that is empty.

The if statements are checking an empty string against a constant string like "one". This will not work.

Something that you could try is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cctype>  // <--- For "std::tolower() and std::toupper()" + others.
#include <iostream>
#include <string>

using namespace 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.

Andy
Thanks all for the input. Heres what I fixed now. And it works. My professor looked at it. Visual studio also corrputed.

#include <iostream>
#include <string>

using namespace std;

int main()
{

string choice = "";


cout << "Welcome to Let's Make a Deal!" << endl;
cout << "Choose your door to claim your prize." << endl;
cout << endl;

cout << "Door (one)\n"
<< "Door (two)\n"
<< "Door (three)\n"
<< "Quit (four)\n" << endl;
cout << endl;

cout << "enter your door choice as text: ";
cin >> choice;

if (choice == "one" || choice == "One")
{
cout << "You chose door #1" << endl;
cout << "You've been ZONKED!!!" << endl;
}
else if (choice == "Two" || choice == "two")
{

cout << "You chose door #2" << endl;
cout << "You won a Trip to Italy! Congrats!!" << endl;
}
else if (choice == "Three" || choice == "three")
{

cout << "You chose door #3" << endl;
cout << "You won $1000.00 Cash" << endl;
}
else if (choice == "Four" || choice == "four")
{

cout << "You chose not to play" << endl;
cout << "Thank you for joining us today" << endl;
}

return 0;
Topic archived. No new replies allowed.