C++ || Display Today’s Date Using a Switch
If statements, char’s and strings have been previously discussed, and this page will be more of the same. This program will demonstrate how to use a switch to display today’s date, converting from mm/dd/yyyy format to formal format (i.e January 5th, 2012).
This same program can easily be done using if statements, but sometimes that is not always the fastest programming method. Switch statements are like literal light switches because the code “goes down a line” to check to see which case is valid or not, just like if statements. You will see why switches are very effective when used right by examining this program.
NOTE: On some compilers, you may have to add #include < cstdlib> in order for the code to compile.
====== TODAY’S DATE USING A SWITCH ========
So to start our program out, lets define the variables.
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> using namespace std; int main() { // Declare Variables int month; int day; int year; char backSlash; } |
Notice on line 9 there is a variable named “backslah.” This program was designed to display the date in this format.
Enter today's date: 10/7/2008
October 7th, 2008
So the only way to achieve that was to add a “place holder” variable during the user input process, which is demonstrated below.
1 2 3 |
// Collect Data cout << "Enter today's date: "; cin >> month >> backSlash >> day >> backSlash >> year; |
In line 3, you can see the format that the user will input the data in. They will input data in mm/dd/yyyy format, and having the “backslash” placeholder there will make that possible.
After the user enters in data, how will the program convert numbers into actual text? Next comes the switch statements.
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 |
// Check month switch(month) { case 1: { cout << "tJanuary "; break; } case 2: { cout << "tFebruary "; break; } case 3: { cout << "tMarch "; break; } case 4: { cout << "tApril "; break; } case 5: { cout << "May "; break; } case 6: { cout << "tJune "; break; } case 7: { cout << "tJuly "; break; } case 8: { cout << "tAugust "; break; } case 9: { cout << "tSeptember "; break; } case 10: { cout << "tOctober "; break; } case 11: { cout << "tNovember "; break; } case 12: { cout << "tDecember "; break; } default: { cout << "t" << month <<" is not a valid monthn"; exit(1); // this forces the program to quit } } |
Line 2 contains the switch declaration, and its comparing the variable of “month” to the 12 different cases that is defined within the switch statement. So this piece of code will “go down the line” comparing to see if the user inputted data is any of the numbers, from 1 to 12. If the user inputted a number which does not fall between 1 thru 12, the “default” case will be executed, prompting the user that the data they inputted was invalid, which can be seen in line 64. Notice line 67 has an exit code. This program will force an exit whenever the user enters invalid data.
Line 7 is very important, because that forces the computer to “break” away from the selected case whenever it is done examining the piece of code. It is important to add the break in there to avoid errors, which may result in the program giving you wrong output as the answer.
Next we will add another switch statement to convert the day of the month to have a number suffix (i.e displaying the number in 1st, 2nd, 3rd format). This is very similar to the previous switch statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Check Day switch(day) { case 1: case 21: case 31: { cout << day << "st, "; break; } case 2: case 22: { cout << day << "nd, "; break; } case 3: case 23: { cout << day << "rd, "; break; } default: { cout << day << "th, "; break; } } |
This block of code is very similar to the previous one. Line 2 is declaring the variable ‘day’ to be compared with the base cases, line 7 and so forth has the break lines, but line 4, 9 and 14 are different. If you notice, line 4, 9 and 14 are comparing multiple cases in one line. Yes with switch statements, you can do that. Just like you can compare multiple values in if statements, the same cane be done here. So this switch is comparing the number the user inputted, with the base cases, adding a suffix to the end of the number.
So far we have obtained data from the user, compared the month and day using switch statements and displayed that to the screen. Now all we have to do is output the year to the user. This is fairly simple, because the year is not being compared, you are just simply using a cout to display data to the user.
1 2 |
// Display Year cout << year << endl; |
Adding all the code together should give us 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
#include <iostream> using namespace std; int main() { // Declare Variables int month; int day; int year; char backSlash; // Collect Data cout << "Enter today's date: "; cin >> month >> backSlash >> day >> backSlash >> year; // Check month switch(month) { case 1: { cout << "tJanuary "; break; } case 2: { cout << "tFebruary "; break; } case 3: { cout << "tMarch "; break; } case 4: { cout << "tApril "; break; } case 5: { cout << "May "; break; } case 6: { cout << "tJune "; break; } case 7: { cout << "tJuly "; break; } case 8: { cout << "tAugust "; break; } case 9: { cout << "tSeptember "; break; } case 10: { cout << "tOctober "; break; } case 11: { cout << "tNovember "; break; } case 12: { cout << "tDecember "; break; } default: { cout << "t" << month <<" is not a valid monthn"; exit(1); } } // Check Day switch(day) { case 1: case 21: case 31: { cout << day << "st, "; break; } case 2: case 22: { cout << day << "nd, "; break; } case 3: case 23: { cout << day << "rd, "; break; } default: { cout << day << "th, "; break; } } // Display Year cout << year << endl; // Terminate return 0; } // http://programmingnotes.freeweq.com |
Once compiled, you should get this as your output
Enter today's date: 1/5/2012
January 5th, 2012
Leave a Reply