Okay, so I need some serious help! This is due tomorrow, and I've been breaking my head trying to figure this out for the past 3 days and I just can't deal with it anymore. I'm usually okay with C++, but this is impossible for me right now. Can someone please help! I can do part a, but parts b and c are my problem. I really appreciate the help!
3. (Data Processing)
a. Write a program to display the following two prompts:
Enter a month (use a 1 for Jan, etc.):
Enter a day of the month:
Have your program accept and store a number in the variable month in response to the first prompt and accept and store a number in the variable day in response to the second prompt. If the month entered is not between 1 and 12, print a message informing the user that an invalid month has been entered. If the day entered is not between 1 and 31, print a message informing the user that an invalid day has been entered.
b. What will your program do if the user enters a number with a decimal point for the month? How can you make sure your if statements check for an integer number?
c. In a non-leap year, February has 28 days; the months January, March, May, July, August, October, and December have 31 days; and all other months have 30 days. Using this information, modify the program written in Exercise 3a to display a message when an invalid day is entered for a user-entered month. For this program, ignore leap years.
Hi LB, I did part a. Part b is confusing me because I am not sure how to keep the user from entering a decimal. Is there a specific code that checks for an integer number? Part c is just too confusing as a whole. Maybe I've been trying to figure it out for so long that my brain has stopped working, I don't know. But it's too complex, for lack of a better word, for me to deal with it.
Our residential biscuit has told you about how cin doesn't like wrong input. Normally you can do things like if(!(cin >> myvar)) which will run the code if cin didn't like the input. make sure you ingore the input and clear the error sate.
Part c is a simple switch-case structure with case fallthrough, something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int max;
switch(month)
{
case 2 /*Feb*/:
max = 28;
break;
case 1 /*Jan*/:
case 3 /*Mar*/;
//same for May, Jul, Aug, Oct, and Dec...
max = 31;
break;
default /*all other months*/:
max = 30;
}
if(day > max)
{
//report to user
}
Well, for c, use if and similar statements to say, if the month equals 2, make the limit = 28, and use the or comparison to make it make the limit 30 for certain months, and 31 for others. Can you do that?
I'm not sure about b, I might know, but I don't want to give the wrong answer. Can someone tell him it?
Ok, so don't laugh at me, but from what you gave me, this is what I've got:
It's not running to say the least.
#include<iostream>
using namespace std;
int main()
{
double month, day, max;
cout<< "Enter a month (use a 1 for Jan, etc.):" <<endl;
cin>> month;
cout<< "Enter a day of the month:"<< endl;
cin>> day;
if (month>12)
{
cout<< "That is an invalid month."<< endl;
}
if (month<=0)
{
cout<< "That is an invalid month."<< endl;
}
if (day<=0)
{
cout<< "That is an invalid day."<< endl;
}
if (day>=32)
{
cout<< "That is an invalid day."<< endl;
}
switch(month)
{
case 2:
max = 28;
break;
case 1:
max = 31;
break;
case 3:
max = 31;
break;
case 5:
max = 31;
break;
case 7:
max = 31;
break;
case 8:
max = 31;
break;
case 10:
max = 31;
break;
case 12:
max = 31;
break;
default:
max = 30;
}
if(day > max)
{
cout<< "..."<< endl;
}
else
cout<< "Thank you."<< endl;
return 0;
}
I think the main reason is that the month variable is a double. It should be an int type, as well as all the others. If I'm wrong, can someone correct me?
Anywho, yea why are you using double? No need for any floating point numbers.
If you haven't looked into enums, this would be a good situation to learn about them :D They're not complicated at all really
Now what problems are you getting? From the looks of it, you're only gonna get a friendly "Thank you" at the end, or a mysterious "..." at the end.
if (day>=32) This is wrong. As far as I know, no month will ever have 32 days. At least with the current Gregorian calendar.
1 2
case 2:
max = 28;
Also wrong, as we see today. Albeit, 75% of the time this is alright
This code could use quite a bit of cleaning up, but let's get it working first. Tell us what issue exactly you're having.
Ok, my first issue is:
I don't know how to combine if statements. As you can see, I wrote 4 separate statements when I could have combined them into 2.
My second issue is, I don't fully comprehend how to have 'cin' check to see that the number the user enters is an integer instead of a decimal.
My last issue is, I just don't know how to use the switch command. I'm making it more complicated than it has to be. Now, because of that, I am not able to do part 'c' of my problem.