if else not working

I've tried various methods for getting the correct output here and nothing seems to work. I really don't understand what I'm doing wrong and its probably something really dumb. It should be a simple yes or no to get output for one or the other. Can someone please help? Thanks

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
#include "stdafx.h"
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
	char a{};

	cout << endl
		<< "Is today March 8th? ";
	cin >> a;

	if (a == 'yes')
	{
		cout << endl
			<< "Happy Women's Day!"
			<< endl;
		return 0;
	}
	else if (a == 'no')
	{
		cout << endl
			<< "Have a great day!"
			<< endl;
	}


	return 0;
}
Last edited on
Hello and welcome!

The problem is your if statement: if (a == 'yes')

a can only be a single character. So, if you change your code to this, it will work:

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

using std::cin;
using std::cout;
using std::endl;

int main()
{
char a{};

cout << "\nIs today March 8th? ";
cin >> a;

// Check whether the input is y/Y - n/N
while (toupper(a) != 'Y' && toupper(a) != 'N')
{
    cout << "\nIs today March 8th? ";
    cin >> a;
}

if (std::toupper(a) == 'Y')
{
    cout << "\nHappy Women's Day!" << endl;
}
else if (std::toupper(a) == 'N')
{
    cout << "Have a great day!" << endl;
}

return 0;
} 


Also, please use code tags. See this link: http://www.cplusplus.com/forum/articles/42672/
Last edited on
A char can hold only a single character so you could do it like this:
if (a == 'y')
Another option is to use a string like
1
2
3
4
std::string answer;
cin >> answer;
if (answer == "yes")
  // and so on 

Oh ok, I knew it would be something simple, this worked perfectly!! Thank you very much and thanks for the link on using code tags, I updated my original post.
Topic archived. No new replies allowed.