Writing a modular program in visual c++. I am new to this and not sure what I am missing. I am getting the following error:

BadDate.cpp: In function ‘int main()’:
BadDate.cpp:50:3: error: ‘else’ without a prev
ious ‘if’
else

Here are the instructions and code:
Writing a Modular Program in C++
In this lab, you add the input and output statements to a partially completed C++ program. When completed, the user should be able to enter a year, a month, and a day. The program then determines if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31.

Notice that variables have been declared for you.

Write the simulated housekeeping() function that contains the prompts and input statements to retrieve a year, a month, and a day from the user.

Include the output statements in the simulated endOfJob() function. The format of the output is as follows:

month/day/year is a valid date.

or

month/day/year is an invalid date.

Execute the program entering the following date: month = 5, day = 32, year = 2014. Record the output of this program.

Execute the program entering the following date: month = 9, day = 21, year = 2002. Record the output of this program.

Respuesta :

Answer:

#include <iostream>

bool validateDate(int, int, int);

using namespace std;

int main()

{

int year;

int month;

int day;

const int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;

bool validDate = true;

   cout<<"Enter the year"<<endl;

   cin>>year;

   cout<<"Enter the month"<<endl;

   cin>>month;

   cout<<"Enter the day"<<endl;

   cin>>day;

if(year <= MIN_YEAR) // invalid year

    validDate = false;

else if (month < MIN_MONTH || month > MAX_MONTH) // invalid month

    validDate = false;

else if (day < MIN_DAY || day > MAX_DAY) // invalid day

    validDate = false;

if(validDate == true)

{

    // Output statement

    cout<<month<<"/"<<day<<"/"<<year<<"is a valid date"<<endl;

}

else

{

    // Output statement

    cout<<month<<"/"<<day<<"/"<<year<<" is an invalid date"<<endl;

}

}

Explanation:

  • Declare variables and take the date as input from user .
  • Inside the main function, check if year is valid using the conditional statements.
  • Check if date is valid and then output that accordingly.