Do-while loop issue

Hello, I am sure this is not hard at all but I'm pressed for time...I believe this is good except it's performing an infinite loop. I want it to continue prompting the user to enter a day and month for their birthday as long as they enter a "y" or "Y". If they enter a "n" or "N" I want the program to cout that it can't give them a result and end the program. I think it's all right except for the validation and do-while loop syntax.

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
52
53
54
55
//Write using nested if/else statements.
//first validate entry.


#include <iostream>
using namespace std;

int main()

{

	int month, day, ans = 0;

	do

	{
		cout << "Is your birthday is between April and August? Enter N for No or Y for Yes" << endl;
		cin >> ans;
		cout << "If yes, this program will tell you your zodiac sign." << endl;
		cout << "Enter your birth month and then the day." << endl;
		cin >> month;
		cin >> day;

		if ((month == 3 && day >= 21) || (month == 4 && day <= 19))
		{
			cout << "You are Aries, Apr 1 - Apr 19." << endl;
		}

		else if ((month == 4 && day >= 20) || (month == 4 && day <= 20))
		{
			cout << "You are Taurus, Apr 20 - May 20" << endl;
		}

		else if ((month == 5 && day >= 21) || (month == 6 && day <= 21))

		{
			cout << "You Are an Gemini, May 21 - June 21." << endl;
		}

		else if ((month == 6 && day >= 22) || (month == 7 && day <= 22))

		{
			cout << "You are Cancer, June 22 - July 21" << endl;
		}

		else if ((month == 7 && day >= 23) || (month == 8 && day <= 22))

		{
			cout << "You Are an Leo, July 22 – Aug 21" << endl;
		}

	} while ((ans != 'y') || (ans != 'Y'));

	return 0;
}
Last edited on
Case ans == 'y':
(ans != 'y') || (ans != 'Y')
false || true
true

Case ans == 'Y':
(ans != 'y') || (ans != 'Y')
true || false
true

Case ans == 'n':
(ans != 'y') || (ans != 'Y')
true || true
true

Do you see the problem?
closed account (SECMoG1T)
The cause of your infinite loop is due to the corruption of your stream (cin)

ans/// should be char not int , cin will always try to read a char value and an error state will be set
Thank you, yes I see both issues.
will repost with edits
Last edited on
Well, ans is a char, so any value it takes will be a char. You're looking for a condition that's true for all values of ans.
Last edited on
oh like a Bool ? How would I implement a bool to this?
But despite the char validation, do you know why it's not recognizing the cout for the last else statement or the cout when I enter N or n. The syntax is correct when I check.
oh like a Bool ? How would I implement a bool to this?
Well, I'm not sure what you mean, but anything you put as a while condition has to be a bool expression or an expression that is implicitly convertible to bool.

But despite the char validation, do you know why it's not recognizing the cout for the last else statement or the cout when I enter N or n. The syntax is correct when I check.
Recheck your code structure.
Topic archived. No new replies allowed.