Unit 2 covered Chapters 4-7. Chapter 4 covered selection statements which includ
ID: 642707 • Letter: U
Question
Unit 2 covered Chapters 4-7. Chapter 4 covered selection statements which included if, if-else, and switch statements. Chapter 5 covered basic loop structures, such as while, for and do-while statements Chapters 6 and 7 covered functions. Refer to "What to Turn In" for specific details on what should be turned in and how to turn in your project.
Write a C program that requests both a month and day value. Only month values between 1 and 12 should be accepted. Further, day values between 1 and 28 should only be accepted for month 2 (February); day values between 1 and 30 should only be accepted for months, 4, 6, 9, and 11 (April, June, September, and November); and only day values between 1 and 31 should be accepted for all remaining months. (Note: Use a switch statement.) Sample output:
Enter a month between 1 and 12: 3
Enter a day between 1 and 31: 30
The month accepted is 3
The day accepted is 30
Enter a month between 1 and 12: 2
Enter a day between 1 and 28: 30
Error - the day you entered is not valid.
Enter a day between 1 and 28: 29
Error - the day you entered is not valid.
Enter a day between 1 and 28: 28
The month accepted is 2
The day accepted is 28
Explanation / Answer
#include <iostream>
#include<string>
using namespace std;
int main() {
// your code goes here
return 0;
}#include <iostream>
#include<string>
using namespace std;
int main() {
// your code goes here
int arr[12]={31,28,31,30,31,30,31,31,30,31,30,31};
string month1[12] ={"janaury","february","march","april","may","june","july","august","september","october","november","december"};
int month,day,c;
do
{
cout<<"Enter a Month between 1 to 12";
cout<<endl;
cin>> month;
cout<<"Enter a Day between 1 " <<arr[month] ;
cin>>day;
if (month>day)
{
cout << "Error - the day you entered is not valid.";
}
cout<<" The month accepted is"<< month1[month+1];
cout<< "The day accepted is" <<day;
cout<" do you want continue press 1 for continue 0 for cancel";
cin>>c;
}while(c>0);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.