Hello everyone,
I am trying to make the number of days in a month display when the user enters the month (1-12). For example, if the user enters the number 2, then they would see that there are 28 days in that month. Can someone tell me what I am doing wrong???? It will display the information as long as the number is 1-12, but I cannot get my "invalid entry" to work.
- For clarity, remove monthNumber-- and use days[monthNumber-1] on output instead.
- Comparing integer to char literal, remove the single quotes around your numbers.
- monthNumber should be greater than 0 ANDless than or equal to 12, you are using the OR operator.
Your program also spits the dummy if you enter something other than what is expected, try handling those cases too. ^^
int main() {
int month = 0;
cout << "Enter your month biatch: ";
cin >> month;
cout << endl;
switch (month) {
case 1:
cout << "Number of days in January is 31." << endl;
break;
case 2:
cout << "Number of days in February is 28, for Leap Year, it's 29." << endl;
break;
//blah blah etc.
}
return 0;}
Or if you wanted it to loop this is how I'd do it:
#include<iostream>
usingnamespace std;
int main(){
int month = 0;
char answer;
cout << "Do you want to enter the months? Type y for yes, and n for no, to quit at any time enter 0." << endl;
cin >> answer;
if (answer == 'y') {
cout << "Enter your month." << endl;
while (true) {
if (cin >> month) {
if ((month == 1) || (month == 3) /*blah blah etc.*/){
cout << "The number of days in this month is 31." << endl;
cout << "Enter your month." << endl;}
if (month == 2){
cout << "The number of days in this month is 28, or for Leap Years: 29." << endl;
cout << "Enter your month." << endl;}
if (/*30 Day Months blah blah blah etc.*/) {/*blah blah you get it*/}
if (month == 0) { break;}}}}
if (answer == 'n'){}
return 0;}