Loop still run even if condition is not met.

I'm just starting out in C++ and I have an assignment I seem to be having issues with. We are supposed to sing the 12 days of christmas using loops and increments. I'm having an issue where the program is still running the do-while loop even if the condition has not been met. If I initially say "No" to singing the song, it will still run the do-while loop.

Please note: The assignment instructions specifically state that we CANNOT use any RETURN or EXIT commands ANYWHERE in the code to end the program.

Can someone lend a helping hand?

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  #include <iostream>

using namespace std;

main() {

	int days, counter, tries;
	const string OnThe = "On the ", DayOf = " day of Christmas my true love sent to me ";
	string fname, answer;

	cout << "Hello!  What is your first name? ";
	cin >> fname;
	cout << endl << "Well, " << fname << ", would you like to sing the 'Twelve Days of Christmas' with me? (Y)es or (N)o: ";
	cin >> answer;
	

	while ((answer != "Y") && (answer != "y") && (answer != "N") && (answer != "n")) {
		cout << endl << "Your answer was invalid, please answer Y or N: ";
		cin >> answer;
	}
	
	if ( answer == "N" || answer == "n" )
                cout << "Bah-Humbug, you must not have been hugged as a child.  Try to enjoy the holiday anyway, Scrooge." << endl;

	do {
	cout << endl << "Awesome! Here we go!" << endl << endl;  

	for ( counter = 1; counter <= 12; counter++ ) {

	cout << endl << OnThe;

	switch ( counter ) {

		case 1:
			cout << "1st";
			break;
		case 2:
			cout << "2nd";
			break;
		case 3:
                        cout << "3rd";
                        break;
		case 4:
                        cout << "4th";
                        break;
		case 5:
                        cout << "5th";
                        break;
		case 6:
                        cout << "6th";
                        break;
		case 7:
                        cout << "7th";
                        break;
		case 8:
                        cout << "8th";
                        break;
		case 9:
                        cout << "9th";
                        break;
		case 10:
                        cout << "10th";
                        break;
		case 11:
                        cout << "11th";
                        break;
		case 12:
			cout << "12th";
			break;
		}
		
	cout << DayOf;

	
        switch ( counter ) {

                case 12:
                        cout << "Twelve Drummers drumming...\n";
                case 11:
                        cout << "Eleven Pipers piping...\n";
                case 10:
                        cout << "Ten Lords a leaping...\n";
                case 9:
                        cout << "Nine Ladies dancing...\n";
                case 8:
                        cout << "Eight Maids a milking...\n";
                case 7:
                        cout << "Seven Swans a swimming...\n";
                case 6:
                        cout << "Six Geese a laying...\n";
                case 5:
                        cout << "FIVE GOLDEN RIIINGGGSSS!!...\n";
                case 4:
                        cout << "Four Calling Birds...\n";
                case 3:
                        cout << "Three French Hens...\n";
                case 2:
                        cout << "Two Turtle Doves...\nand ";
                case 1:
                        cout << "a Partridge in a Pear Treeeee!";
                }
		 
	}
	cout << endl << "Would you like to sing it again? ";
	cin >> answer; 
	
	while ((answer != "Y") && (answer != "y") && (answer != "N") && (answer != "n")) {
                cout << endl << "Your answer was invalid, please answer Y or N: ";
                cin >> answer;
        }

	if ( answer == "N" || answer == "n" ) {
		cout << "Aww..well thanks for singing with me! Have a great holiday!" << endl;	
		break;
		}

	}while ( answer == "Y" || answer == "y" );
	

}
Really easy fix. You just need an else statement right above your do-while loop.

I added comments where things were added. On the if statements, brackets help keep things organized and prevent issues.

Here is the updated code:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>

using namespace std;

main() {

	int days, counter, tries;
	const string OnThe = "On the ", DayOf = " day of Christmas my true love sent to me ";
	string fname, answer;

	cout << "Hello!  What is your first name? ";
	cin >> fname;
	cout << endl << "Well, " << fname << ", would you like to sing the 'Twelve Days of Christmas' with me? (Y)es or (N)o: ";
	cin >> answer;
	

	while ((answer != "Y") && (answer != "y") && (answer != "N") && (answer != "n")) {
		cout << endl << "Your answer was invalid, please answer Y or N: ";
		cin >> answer;
	}
	
	if ( answer == "N" || answer == "n" ) { //added this '{' bracket
                cout << "Bah-Humbug, you must not have been hugged as a child.  Try to enjoy the holiday anyway, Scrooge." << endl;
	} //closed it
	else { //added this else statement
	do {
	cout << endl << "Awesome! Here we go!" << endl << endl;  

	for ( counter = 1; counter <= 12; counter++ ) {

	cout << endl << OnThe;

	switch ( counter ) {

		case 1:
			cout << "1st";
			break;
		case 2:
			cout << "2nd";
			break;
		case 3:
                        cout << "3rd";
                        break;
		case 4:
                        cout << "4th";
                        break;
		case 5:
                        cout << "5th";
                        break;
		case 6:
                        cout << "6th";
                        break;
		case 7:
                        cout << "7th";
                        break;
		case 8:
                        cout << "8th";
                        break;
		case 9:
                        cout << "9th";
                        break;
		case 10:
                        cout << "10th";
                        break;
		case 11:
                        cout << "11th";
                        break;
		case 12:
			cout << "12th";
			break;
		}
		
	cout << DayOf;

	
        switch ( counter ) {

                case 12:
                        cout << "Twelve Drummers drumming...\n";
                case 11:
                        cout << "Eleven Pipers piping...\n";
                case 10:
                        cout << "Ten Lords a leaping...\n";
                case 9:
                        cout << "Nine Ladies dancing...\n";
                case 8:
                        cout << "Eight Maids a milking...\n";
                case 7:
                        cout << "Seven Swans a swimming...\n";
                case 6:
                        cout << "Six Geese a laying...\n";
                case 5:
                        cout << "FIVE GOLDEN RIIINGGGSSS!!...\n";
                case 4:
                        cout << "Four Calling Birds...\n";
                case 3:
                        cout << "Three French Hens...\n";
                case 2:
                        cout << "Two Turtle Doves...\nand ";
                case 1:
                        cout << "a Partridge in a Pear Treeeee!";
                }
		 
	}
	cout << endl << "Would you like to sing it again? ";
	cin >> answer; 
	
	while ((answer != "Y") && (answer != "y") && (answer != "N") && (answer != "n")) {
                cout << endl << "Your answer was invalid, please answer Y or N: ";
                cin >> answer;
        }

	if ( answer == "N" || answer == "n" ) {
		cout << "Aww..well thanks for singing with me! Have a great holiday!" << endl;	
		break;
		}

	}while ( answer == "Y" || answer == "y" );

	} //closed the else statement

}

Last edited on
I was REALLY hoping it was something short and simple that I missed. We were told that we shouldn't use brackets if there's only 1 statement after the IF statement. I'm just getting into the habit of doing that now, but thank you so much!
Last edited on
Yeah, no problem! Mark the answer as solved at the top of the page.
Topic archived. No new replies allowed.