Hello SweatyCoder,
I had to step away from the computer earlier and lost track of where I was at. To explain "std::" a bit:
This is known as a name space qualifier. Most classes and books that I have heard about tend to put off teaching about name space till later and think that
using namespace std;
is a good thing to promote until they get around name spaces.
using namespace std;
will work for awhile, but eventually will become a problem. Most of your first programs putting the using statement in the program is not going to cause any problem, but at the same time it is not helping you either.
After creating a program to work wit I came up with this:
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
// <--- Most common includes to start with.
#include <iostream>
#include <iomanip>
#include <limits>
//using namespace std; // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
// <--- A better alternative to using namespace std;
//using std::cin;
//using std::cout;
//using std::endl;
//using std::string;
int main()
{
constexpr int MAX_DAYS{31};
int birthMonth{};
int birthDay{};
int birthYear{};
int daysInMonth[13]{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
do
{
std::cout << "\n Year: ";
std::cin >> birthYear;
if (!std::cin) // <--- Checks if you entered something other than a number and fixes "std::cin".
{
std::cout << "\n Invalid Input! Must be a number\n"; // <--- Indented to show a problem. Sets it off in the output to the screen.
std::cin.clear(); // <--- Resets the state bits.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. Clears the input buffer.
continue; // <--- causes a jump to the while condition to start over. Same for the others.
}
if (birthYear < 1900 || birthYear > 2020)
std::cout << "\n Invalid Year! Try again!\n";
} while (birthYear < 1900 || birthYear > 2020);
if ((birthYear % 4 == 0) && (birthYear % 100 != 0 || birthYear % 400 == 0)) // <--- Check for leap year.
daysInMonth[2] = 29;
do
{
std::cout << "\n Month: ";
std::cin >> birthMonth;
if (!std::cin)
{
std::cout << "\n Invalid Input! Must be a number\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
continue; // <--- causes a jump to the while condition to start over.
}
if (birthMonth < 1 || birthMonth > 12)
std::cout << "\n Invalid Month! Try again.\n";
} while (birthMonth < 1 || birthMonth > 12);
do
{
std::cout << "\n Day: ";
std::cin >> birthDay;
if (!std::cin)
{
std::cout << "\n Invalid Input! Must be a number\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
continue; // <--- causes a jump to the while condition to start over.
}
//if (birthDay < 1 || birthDay > MAX-DAYS)
if (birthDay < 1 || birthDay > daysInMonth[birthMonth]) // <--- Uses the array for correct number of days in a given month.
std::cout << "\n Invalid Day! Try again.\n";
} while (birthDay < 1 || birthDay > daysInMonth[birthMonth]);
std::cout << "\n Your birth date is: " << birthMonth << '/' << birthDay << '/' << birthYear << '\n';
// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
|
The comments in the program should explain what it is doing.
Line 17 I will get to later.
Lines 19 - 21 is the same as
birthMonth = 0;
it is known as the uniform initializer and is available from C++11 on. The empty {}s will set the variable to the type of zero based on the type of the variable. You also have the option of putting something inside the {}s to give it a value other than zero.
The do/while loops are all about the same. I did change it to ask for the year first which works with line 44 to determine if it is a leap year. Which is one of the reasons I used the array. The check for a failed "std::cin" may be ahead of what you need and can be removed if you want.
For line 83 the original code I wrote
if (birthDay < 1 || birthDay > 31)
the 31 is known as a magic number. This should be avoided. At the time it was easy to do and I realize I should not have done it. Right now your program is small and easy to change, but when it becomes 100 to 300 lines trying to find all these magic numbers will be difficult and you will likely miss 1 or 2 maybe more. With the constant variable you only have 1 place to make a change and it will change everywhere in the program. You could also create constant variables for min and max month, days and years.
Line 89 I added to display what you gathered as input. You can keep this or do something more at this line.
Lines 92 - 96 is just something I use. It is neither required or may not be needed. If you do not need it comment it out and keep for future reference.
As a whole keep this code for future reference. You may find it useful.
If there is any part that needs better explained let me know.
Andy