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.
#include <iostream>
usingnamespace 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" );
}
#include <iostream>
usingnamespace 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
}
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!