hello all! I'm a beginner to programming and am currently working on learning C++ program. I bought a book titled "Problem Solving with C++, 7th ed." by Walter Savitch but am stuck with a C++ problem (not in the book). I'm trying to make my conditions program figure out how many days there are in a month; it also figures for leap year. The leap year part works, but I cannot make the rest to work out properly. If I enter anything other than 2 (for Feb), it will not end properly. It will give the right days of the month but will not end properly. Can anyone help me out with my code??? Here is the code: ==================== Code: #include <iostream> using namespace std; int main() { int Month_Number, Year; cout << "Enter a number matching the month \n"; cout << "of the year, then press return\n"; cin >> Month_Number; cout << " \n"; { if ( Month_Number == 1) cout << "The month has 31 days,\n"; else if ( Month_Number == 3) cout << "The month only has 31 days.\n"; else if ( Month_Number == 4) cout << "The month only has 30 days.\n"; else if ( Month_Number == 5) cout << "The month only has 31 days.\n"; else if ( Month_Number == 6) cout << "The month only has 30 days.\n"; else if ( Month_Number == 7) cout << "The month only has 31 days.\n"; else if ( Month_Number == 8) cout << "The month only has 31 days.\n"; else if ( Month_Number == 9) cout << "The month only has 30 days.\n"; else if ( Month_Number == 10) cout << "The month only has 31 days.\n"; else if ( Month_Number == 11) cout << "The month only has 30 days.\n"; else if ( Month_Number == 12) cout << "The month only has 31 days.\n"; } if (Month_Number == 2) cout << "Please enter the year.\n"; cin >> Year; if (Year % 4 == 0) cout << "February has 29 days. This is a Leap year\n"; else cout << "February has 28 days. \n"; system("pause"); return 0; }
You haven't said what "doesn't end properly" means, but I guess you're puzzled why it prompts you for the year without printing "Please enter the year." Or you're assuming that the prompt is some kind of hang. See if you can see what's missing between "if (Month_Number == 2)" and "cout << "Please enter the year.\n";", and after "cout << "February has 28 days. \n";" Clue: you have unnecessary ones around your if/else if ladder. If you need another clue, have you been Python programming as well recently?