Magic Dates in C++

I have to write a program that asks the user to enter a month (in numeric form), a day, and a two-digit year. The program should then determine whether the month times the day is equal to the year. If so, it should display a message saying the date is magic. Otherwise, it should display a message saying the date is not magic.
The only problem I'm having is validating the dates. I don't know how to do that. I have to check to make sure the user enters valid numbers. If the user does not enter even one valid number, it must indicate to the user and not calculate but end the program.


This is what I have so far:


#include <iostream>
using namespace std;

int main()
{
int m;
int d;
int y;
int magic;

cout << "Please enter a month in numeric form: ";
cin >> m;
cout << endl;

cout << "Please enter a day in numeric form: ";
cin >> d;
cout << endl;

cout << "Please enter a two-digit year: ";
cin >> y;
cout << endl;

magic = m * d;


switch (m)
{
case 2:
if (d > 28)
{
cout << "That day is invalid." << endl;
break;
}
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if (d > 31)
{
cout << "That day is invalid." << endl;
break;
}
case 4: case 6: case 9: case 11:
if (d > 30)
{
cout << "That day is invalid." << endl;
break;
}

}

if(magic == y)
{
cout << "The date is magic!" << endl;
}
else
{
cout << "The date is not magic." << endl;
}

system("pause");
return 0;
}
I'm not fully understanding what you mean with validating the numbers in this case, because that's kind of what you are already doing by using that switch statement, right? As for the cin statements you could try to make a do-while loop as in (for instance):

1
2
3
4
5
6
do
{
       cout << "Please enter a month in numeric form: ";
       cin >> m;
       cout << endl;
} while (!(m > 0 && m <= 12))


I don't usually use do-while loops often but that should work.

And for your switch statement you should always remember to check wether the numbers are below 0, for instance for the first one:

if (d > 28 || d < 0)

Also (in the switch statement below the cases) it's smart to make a

default:

For if the other cases don't get triggered. But if you're using the do-while loop as shown above, this probably won't be much of a problem.


Oh, and pause in system("PAUSE") should be in uppercase letters (if I remember correctly)


~Sorry for the bad formatting, this was sent from a phone. And I'm a beginner myself so even if my tip helps it might not be the most efficient one
Last edited on
Thank you NutHoofd!
The only problem is when I run the program and enter month 2 but with 31st day it will say it's invalid, but also adds whether the date is magic or not. How do I stop that from happening?
Do you mean you want the program to stop instead of to continue to the final if loop? Then you could simply add a boolean variable called something like bool isValid and set it to true when declaring it. Then, once you hit invalid dates in your switch statement you simply set it to false.

Having this boolean variable you can simply put a big if loop AROUND the final if loop, to check whether the program should execute that part of the program. Something like this:

1
2
3
4
5
6
7
8
9
10
11
if (isViable == true)
{
     if(magic == y)
     {
          cout << "The date is magic!" << endl;
     }
     else
     {
          cout << "The date is not magic." << endl;
     }
}
Last edited on
Topic archived. No new replies allowed.