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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// How do you access an element within a std::string?
// oceanmachine
// I can't figure out the last part of this assignment
// ("Read the user's date of birth: 2 digits for the month and 2 digits for
// the day..."). I'm hoping someone here can help. Here's the prompt:
// "Write a program that reads the current date from the user in 10/3/2017
// format and then generates a random date such as 9/30. Make sure you use
// correct limits for the number of days for each month when generating
// the date. For example, 9/30 is valid, but not 9/31. To check for
// the month of February, use the current year entered by the user,
// since the number of days in February depends on if the year is leap
// year or not. Months 1, 3, 5, 7, 8, 10 and 12 are 31 days, month 2 is 29 days
// in a leap year and 28 in a non leap year and the rest of the months
// are 30 days. A year is a leap year if it's divisible by 400 or if not,
// it's divisible by 4 but not 100. If the generated date is the same as or
// following or preceding one of the following dates, generate another date:
// 1/1, 7/4, 11/25 and 12/25. If the second generated date is again the same
// or next to one of those dates, let it be. Next, read the user's date of
// birth: 2 digits for the month and 2 digits for the day into a c-std::string
// that has room for the 4 digits entered and then print the date of birth
// in October 3 format."
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
struct Mydate {
int day;
int month;
int year;
};
bool excludeDates(int, int);
int main()
{
// "...Write a program that reads the current date from the user in
// 10/3/2017 format..."
std::cout << "\nEnter today's date as month/day/year: ";
std::string userdt;
std::cin >> userdt;
std::string::size_type pos = userdt.find_first_of("/");
std::string tmpstr = userdt.substr(0, pos);
Mydate userdate;
userdate.month = std::stoi(tmpstr);
tmpstr = userdt.substr(pos+1, userdt.find("/", pos+1));
userdate.day = std::stoi(tmpstr);
pos = userdt.find_last_of("/") + 1;
tmpstr = userdt.substr(pos);
userdate.year = std::stoi(tmpstr);
// "...and then generates a random date such as 9/30..."
srand(time(0));
Mydate rnddate;
rnddate.year = userdate.year;
int firstattempt = 0;
do
{
rnddate.month = rand() % 12 + 1;
switch(rnddate.month)
{
case 2:
// A year is a leap year if it's divisible by 400 or if not,
// it's divisible by 4 but not 100
if(rnddate.year % 400
|| (rnddate.year % 4 && !rnddate.year % 100))
{
// leap year
rnddate.day = rand() % 29 + 1;
}
else
{
rnddate.day = rand() % 28 + 1;
}
break;
case 4:
case 6:
case 9:
case 11:
rnddate.day = rand() % 30 + 1;
break;
default:
rnddate.day = rand() % 31 + 1;
break;
}
firstattempt++;
} while(excludeDates(rnddate.day, rnddate.month) && firstattempt<2);
char slash = '/';
std::cout << "\nGenerated: " << rnddate.month << slash << rnddate.day
<< std::endl;
// "... Next, read the user's date of birth: 2 digits for the month and
// 2 digits for the day into a c-string that has room for the 4 digits
// entered and then print the date of birth in October 3 format..."
std::cout << "\nEnter your date of birth as mm/dd: ";
char bday[6];
std::cin.ignore(); // <-- this will ignore one single character!
std::cin.getline(bday, 6);
int month = int(bday[0]-48) * 10 + int(bday[1]-48);
std::vector<std::string> months = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
std::cout << months.at(month-1) << ' '
<< int(bday[3]-48) * 10 + int(bday[4]-48) << '\n';
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
bool excludeDates(int day, int month)
{
// If the generated date is the same as or following or preceding
// one of the following dates, generate another date:
// 1/1, 7/4, 11/25 and 12/25.
switch(day)
{
case 1:
case 2:
case 31:
if(month == 1 || month == 12) return true;
break; // pointless, but in case of corrections...
case 3:
case 4:
case 5:
if(month == 7) return true;
break;
case 24:
case 25:
case 26:
if(month == 11 || month == 12) return true;
break;
}
return false;
}
|