Writing a Modular Program in C++

Hello, I'm currently learning how to program using c++ and this is one of the exercises my teacher gave to me. I already made a code for it but for some reason when I enter a valid date...the output will still result to "invalid date" even tho it's valid. Please check the code I've made if there any errors.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 

#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 Month: ";
        cin >> day;
        cout << "Enter Day: ";
        cin >> month;
        cout << "Enter Year: ";
        cin >> year;
        
  if(year <= MIN_YEAR)  // invalid year
     validDate = false;
  else if (month < MIN_MONTH || month > MAX_MONTH)
     validDate = false;
  else if (day < MIN_DAY || day > MAX_DAY) // invalid day
     validDate = false; 
   
  if(validDate == true)  
  { 
      cout << day << "/" << month << "/" << year << " is a valid date.";
  }
    
  else
  {
      cout << day << "/" << month << "/" << year << " is an invalid date."; 
     return 0;
  }


Format of the output:
month/day/year is a valid date.
month/day/year is an invalid date.

The testing values:
month = 9, day = 21, year = 2002 ===> keeps saying it's invalid
month = 5, day = 32, year = 2014

Please help me :D



1
2
3
4
5
6
cout << "Enter Month: ";
        cin >> day;
        cout << "Enter Day: ";
        cin >> month;
        cout << "Enter Year: ";
        cin >> year;


"Enter month:" -> user inputs data into day variable. Vice versa for day.

Also you have a function prototype
bool validateDate(int, int, int);
You never actually define or use the function
Last edited on
Thank you so much..this really helped me a lot.

There's only 1 concern I have left is that in one of my requirements is to have an output of " is a valid date."
When the program executes..it has an output of " month/day/year is a valid data."
even so..it still doesn't consider it as the correct output. So, is there something I haven't considered yet?

Thanks again.
input:
Enter Month: 15
Enter Day: 20
Enter Year: 2018

gives:
20/15/2018 is an invalid date.


Input:
Enter Month: 10
Enter Day: 24
Enter Year: 2015

gives:
24/10/2015 is a valid date.


What input did you use for your program not to work? It works fine for me so far


EDIT:
This is considering that you made the changes below:
1
2
3
4
5
6
cout << "Enter Month: ";
        cin >> month;
        cout << "Enter Day: ";
        cin >> day;
        cout << "Enter Year: ";
        cin >> year;
Last edited on
I did made the changes and the program is running smoothly but still doesn't accept the output even tho it's correct.

Something is probably wrong with the programming exercise. I'm just gonna ask my teacher about this.

Thank you so much for helping me this far :D :D
My pleasure!

But are you sure you are inputting correct data, i'm telling you, your code works fine for me! :)
Yes, I'm pretty sure :D :D
Thanks again.
Topic archived. No new replies allowed.