Trouble with my if statement

My code is supposed to output "Excellent enjoy your stay" when true, and "lets try that again" if false. But I am getting the false statement no matter what. I'm very new to c++ (2nd week coding in free time), so I'm sure the issue is very obvious.

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
  #include <iostream>
int main () {
using namespace std;

cout << "The Baked Turkey Hotel Guestbook" << endl << endl;

string firstName;
string lastName;
string middleIn;
bool yes = true;
bool no = false;
    
    //Getting information from user
cout << "Please enter your first name: ";
cin >> firstName;
cout << "Please enter your middle initial: ";
cin >> middleIn;
cout << "Please enter your last name: ";
cin >> lastName;
    
    //Confirming Information
cout << "Is the following information correct?" << endl << endl;
cout << "First Name:" << firstName << endl;
cout << "Middle Initial:" << middleIn << endl;
cout << "Last Name:" << lastName << endl;
cout << "Please enter only \"yes\" or \"no\": ";
cin >> yes or no;

    
    if (yes){
        cout << "Excellent, enjoy your stay!" << endl;
    }else (no){
        cout << "Darn, let's try this again." << endl;
    }
Hi

You only need 1 bool variable, it is set to either true or false.

or is a key word, so line 27 is a problem. Input to one variable.
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
#include <iostream>
#include <string>
using namespace std;

int main() {
	cout << "The Baked Turkey Hotel Guestbook\n\n";

	string firstName;
	string lastName;
	string middleIn;
	string yesorno;

	//Getting information from user
	cout << "Please enter your first name: ";
	cin >> firstName;

	cout << "Please enter your middle initial: ";
	cin >> middleIn;

	cout << "Please enter your last name: ";
	cin >> lastName;

	//Confirming Information
	cout << "Is the following information correct?\n\n";
	cout << "First Name: " << firstName << '\n';
	cout << "Middle Initial: " << middleIn << '\n';
	cout << "Last Name :" << lastName << '\n';

	do {
		cout << "Please enter only \"yes\" or \"no\": ";
		cin >> yesorno;
	} while (yesorno != "yes" && yesorno != "no");

	if (yesorno == "yes")
		cout << "Excellent, enjoy your stay!\n";
	else
		cout << "Darn, let's try this again.\n";
}

Here's how I would do it.

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
#include <iostream>
#include <string>
#include <conio.h>

int main() {
	using namespace std;

	cout << "The Baked Turkey Hotel Guestbook" << endl << endl;

	string firstName;
	string lastName;
	string middleIn;
	char c = 0;

	//Getting information from user
	cout << "Please enter your first name: ";
	cin >> firstName;
	cout << "Please enter your middle initial: ";
	cin >> middleIn;
	cout << "Please enter your last name: ";
	cin >> lastName;

	//Confirming Information
	cout << "Is the following information correct?" << endl << endl;
	cout << "First Name:      " << firstName << endl;
	cout << "Middle Initial:  " << middleIn << endl;
	cout << "Last Name:       " << lastName << endl;
	
	
	while(c != 'y' && c != 'n')
	{
		c = _getch();

		switch (c) {
		case 'y':
			cout << "Excellent, enjoy your stay!" << endl;
			break;
		case 'n':
			cout << "Darn, let's try this again." << endl;
			break;
		default:
			cout << "Please enter only \"y\" or \"n\": " << endl;
			break;
		}
	}
	
}
Another version:

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
#include <iostream>

int main ()
{
    using namespace std;
    
    cout << "The Baked Turkey Hotel Guestbook" << endl << endl;
    
    string firstName;
    string lastName;
    string middleIn;
    
    string answer{"Who knows"};
    
    //Getting information from user
    while( answer != "yes")
    {
        cout << "Please enter your first name: ";
        cin >> firstName;
        cout << "Please enter your middle initial: ";
        cin >> middleIn;
        cout << "Please enter your last name: ";
        cin >> lastName;
        
        //Confirming Information
        cout << "Is the following information correct?" << endl << endl;
        cout << "First Name:" << firstName << endl;
        cout << "Middle Initial:" << middleIn << endl;
        cout << "Last Name:" << lastName << endl;
        cout << "Please enter only \"yes\" or \"no\": ";
        cin >> answer;
        if(answer != "yes")
            cout << "Darn, let's try this again.\n";
        else
            cout << "Excellent, enjoy your stay!\n";
    }
    
    return 0;
}
Topic archived. No new replies allowed.