Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "calender.hpp"
- // Eevery month besides February which has 28 days (29 days if it is leap year) will add this later after more testing. October 4, 2022
- /* CONTRACT
- This is my main function
- Pre-condition: It interacts with the user and asks for data via input
- Post-condition: It will output the month, type of day, the day in a integer, month in a integer, and year in a integer format
- */
- int main() {
- int askDay,month,start,end,year;
- std::cout << "Enter Day in number form: ";
- std::cin >> askDay;
- std::cout << '\n';
- std::string askMonth,nameOfDay;
- std::cout << "Enter Month: ";
- std::cin >> askMonth;
- std::cout << '\n';
- std::cout << "Choose a year to start at: ";
- std::cin >> start;
- std::cout << '\n';
- std::cout << "Choose the ending of it:";
- std::cin >> end;
- std::cout << '\n';
- calender Mycalender;
- Mycalender.GetDays(askDay);
- Mycalender.GetMonth(askDay, month, askMonth, nameOfDay);
- Mycalender.GetYear(start, end, year);
- std::cout << nameOfDay << " " << askMonth << " ";
- std::cout << askDay << "/" << month << "/" << year;
- std::cout << '\n';
- return 0;
- }
- /* CONTRACT:
- default contructor. A quarter of it's data will be based on the user and the user will not know.
- Users will meet conditional expression based off the given conditional expressions
- Pre-condition: It takes the input of the user and compares it's own data with the users data.
- @Post-condition: Day must be < 0 and less than 31. Start must be less than end
- */
- calender::calender() {
- // If the pointer does not work, add int ©OfIterator into the constructor
- aCalenderBag Bag;
- for ( int numDays = 1; numDays < 32; numDays++ ) {
- for (int j = 0; j < 32; j++) { Bag.Days[j] = numDays; }
- }
- for ( int i = 0; i < 20000; i++ ) { Bag.Years[i] = 0; }
- }
- /* CONTRACT
- This is my GetDays function.
- Pre-condition: It will check if the number whatever the user typed in and will compare it with the data I have hardcoded in my calender.hpp file
- Post-condtion: it will store the data if it is correct and return back to main
- */
- int calender::GetDays(int &Day) {
- // Create a constructor only and only when you're allocating memory on stack
- // When you don't know the size of an array, you need to allocate memory dymanically
- aCalenderBag Bag;
- int i = 0;
- if ((Day > 0) && (Day < 32)) { return Day; }
- while ( i < 34 ) {
- if (Day != Bag.Days[i]) {
- i++;
- if ( i == 31 ) {
- std::cout << "Invalid Number Detected!";
- std::cout << '\n';
- i = 34;
- }
- }
- }
- while ( Day < 0 ) {
- std::cout << "Please choose a number higher than 0";
- std::cin >> Day;
- if ( Day > 0 ) { return calender::GetDays(Day); }
- }
- while ( Day > 31 ) {
- std::cout << "Please choose something lower than 31";
- std::cin >> Day;
- if ( Day < 31) { return calender::GetDays(Day); }
- }
- return 0; // STUB
- }
- /* CONTRACT
- This is my GetMonth function
- Pre-condition: It compares the users input and if the input is not correct, it will recrusive call itself until there is a match
- Post-condition: It will compare the hard coded data I have typed and it will Assign the name of day by calling the AssignNameOfDay function
- */
- void calender::GetMonth(int &day, int &month, std::string &Month, std::string &nameOfDay) {
- aCalenderBag Bag;
- // Add more here. Make sure if the user chooses 28 along with feb
- if ( Month == Bag.Months[0]) { month = 9; }
- else if ( Month == Bag.Months[1] ) { month = 10; }
- else if ( Month == Bag.Months[2] ) { month = 11; }
- else if (Month == Bag.Months[3] ) { month = 12; }
- else if ( Month == Bag.Months[4] ) { month = 1; }
- else if ( Month == Bag.Months[5] ) { month = 2; }
- else if ( Month == Bag.Months[6] ) { month = 3; }
- else if ( Month == Bag.Months[7] ) { month = 4; }
- else if ( Month == Bag.Months[8] ) { month = 5; }
- else if (Month == Bag.Months[9] ) { month = 6; }
- else if (Month == Bag.Months[10] ) { month = 7; }
- else if ( Month == Bag.Months[11] ) { month = 8;}
- else {
- std::cout << "Not A valid Month! " << "Re-enter a valid date: " << '\n';
- std::cin >> Month;
- return calender::GetMonth(day,month,Month,nameOfDay);
- }
- calender::AssignNameOfDay(day,nameOfDay);
- return;
- }
- /* CONTRACT
- This is my AssignNameOfDay function
- Pre-condition: it will only be called if calender::GetMonth passes all of the checks. Otherwise it will not be called
- post-condition: It will assign the nameOfDay variable depending on variable day
- */
- void calender::AssignNameOfDay(int &day, std::string &nameOfDay) {
- aCalenderBag Bag;
- if ( day == 1 ) {
- nameOfDay = Bag.nameOfDay[0];
- return;
- }
- else if ( day == 2 ) {
- nameOfDay = Bag.nameOfDay[1];
- return;
- }
- else if ( day == 3 ) {
- nameOfDay = Bag.nameOfDay[2];
- return;
- }
- else if ( day == 4 ) {
- nameOfDay = Bag.nameOfDay[3];
- return;
- }
- else if ( day == 5 ) {
- nameOfDay = Bag.nameOfDay[4];
- return;
- }
- else if ( day == 6 ) {
- nameOfDay = Bag.nameOfDay[5];
- return;
- }
- else if ( day == 7 ) {
- nameOfDay = Bag.nameOfDay[6];
- return;
- }
- //
- if ( day == 8 ) {
- nameOfDay = Bag.nameOfDay[0];
- return;
- }
- else if ( day == 9 ) {
- nameOfDay = Bag.nameOfDay[1];
- return;
- }
- else if ( day == 10 ) {
- nameOfDay = Bag.nameOfDay[2];
- return;
- }
- else if ( day == 11 ) {
- nameOfDay = Bag.nameOfDay[3];
- return;
- }
- else if ( day == 12 ) {
- nameOfDay = Bag.nameOfDay[4];
- return;
- }
- else if ( day == 13 ) {
- nameOfDay = Bag.nameOfDay[5];
- return;
- }
- else if ( day == 14 ) {
- nameOfDay = Bag.nameOfDay[6];
- return;
- }
- //
- if ( day == 15 ) {
- nameOfDay = Bag.nameOfDay[0];
- return;
- }
- else if ( day == 16 ) {
- nameOfDay = Bag.nameOfDay[1];
- return;
- }
- else if ( day == 17 ) {
- nameOfDay = Bag.nameOfDay[2];
- return;
- }
- else if ( day == 18 ) {
- nameOfDay = Bag.nameOfDay[3];
- return;
- }
- else if ( day == 19 ) {
- nameOfDay = Bag.nameOfDay[4];
- return;
- }
- else if ( day == 20 ) {
- nameOfDay = Bag.nameOfDay[5];
- return;
- }
- else if ( day == 21 ) {
- nameOfDay = Bag.nameOfDay[6];
- return;
- }
- //
- if ( day == 22 ) {
- nameOfDay = Bag.nameOfDay[0];
- return;
- }
- else if ( day == 23 ) {
- nameOfDay = Bag.nameOfDay[1];
- return;
- }
- else if ( day == 24 ) {
- nameOfDay = Bag.nameOfDay[2];
- return;
- }
- else if ( day == 25 ) {
- nameOfDay = Bag.nameOfDay[3];
- return;
- }
- else if ( day == 26 ) {
- nameOfDay = Bag.nameOfDay[4];
- return;
- }
- else if ( day == 27 ) {
- nameOfDay = Bag.nameOfDay[5];
- return;
- }
- else if ( day == 28 ) {
- nameOfDay = Bag.nameOfDay[6];
- return;
- }
- //
- if ( day == 29 ) {
- nameOfDay = Bag.nameOfDay[0];
- return;
- }
- else if ( day == 30 ) {
- nameOfDay = Bag.nameOfDay[1];
- return;
- }
- else if ( day == 31 ) {
- nameOfDay = Bag.nameOfDay[2];
- return;
- }
- else if ( day == 32 ) {
- nameOfDay = Bag.nameOfDay[3];
- return;
- }
- }
- /* CONTRACT
- This is my GetYear function.
- 20000 is the max Bag.Years[] can handle. There's if-statement to check if the user has exceeded the max of elements
- Pre-condition: Five conditions you must follow:
- * start < end
- * start < 0
- * start > end
- * end > 20000
- * start > 20000
- * If enters data incorrectly, it will execute one of these following conditions
- Post-condition: It will make sure whatever input the user gives is checked and will compare the data with the private data
- */
- void calender::GetYear(int &start, int &end, int &year) {
- // there's a bug around in this code. I am trying to find it. October 5, 2022
- aCalenderBag Bag;
- int tempStart = start; int tempEnd = end;
- int numYears = (tempEnd - tempStart) + 1;
- while ( end < start ) {
- // The bug occurs around here somewhere. With certain combinations it'll pop up
- end = 0;
- std::cout << "Invalid number!, Please choose a number greater than the start of the year" << '\n';
- std::cin >> end;
- if ( end > start ) { return calender::GetYear(start,end,year); }
- }
- while (start < 0 ) {
- std::cout << "Invalid number!, Please choose a number greater than 0: " << '\n';
- std::cin >> start;
- if ( start > 0 ) { return calender::GetYear(start,end,year); }
- }
- while ( start > end ) {
- std::cout << "Invalid number!, Please choose something a number less than the end year: ";
- std::cin >> start;
- int newStart = start;
- if ( newStart < end ) { return calender::GetYear(start,end,year); }
- }
- while ( end > 20000 ) {
- // This works
- numYears = 0;
- std::cout << "Error! The end of the year you have choses is too high " << '\n';
- std::cout << "Re-enter end of the year: " << '\n';
- std::cin >> end;
- if ( end < 20000 ) { return calender::GetYear(start,end,year); }
- }
- while ( start > 20000 ) {
- std::cout << "Re-enter start of year " << '\n';
- std::cin >> start;
- if ( start < 20000 ) { return calender::GetYear(start,end,year); }
- }
- calender::AssignYears(start, end, Bag);
- calender::Print(numYears, Bag);
- std::cout << "Please select a year: ";
- std::cin >> year;
- bool Check = calender::CheckUserAnswer(numYears, start, end, year, Bag);
- if ( Check == false ) { return calender::GetYear(start,end,year); }
- else if ( Check == true ) { return; }
- }
- /* CONTRACT
- This is my Print function
- Pre-condition: none
- Post-condition: it prints the values from start to end
- */
- void calender::Print(int &iter, aCalenderBag &Bag) const {
- std::cout << "This is what you can choose from: " << '\n';
- for (int i = 0; i < iter; i++) { std::cout << Bag.Years[i] << '\n'; }
- return;
- }
- /* CONTRACT
- This is my AssignYears function
- Pre-condition: start <= end
- Post-condition: It takes the start and end and starts populating the array based on the input the user gave
- */
- void calender::AssignYears(int &start, int &end, aCalenderBag &Bag) {
- int j = 0;
- for (int i = start; i <= end; i++) {
- Bag.Years[j] = i;
- j += 1;
- }
- Bag.Years[j + 1] = end;
- return;
- }
- /* CONTRACT
- This is my CheckUserAnswer
- Pre-condtion: i < iter, counter == iter
- Post-condition: it will make the user re-enter start and end if the year they chose was not within the time frame that they chose
- */
- bool calender::CheckUserAnswer(int &iter, int &start, int &end, int &year, aCalenderBag &Bag) const {
- int counter = 0;
- for (int i = 0; i < iter; i++) {
- if ( year != Bag.Years[i] ) {
- counter += 1;
- if ( counter == iter ) {
- std::cout << "Error! Not within the given time frames!" << '\n';
- std::cout << "Re-enter start of year " << '\n';
- std::cin >> start;
- std::cout << "Re-enter end of the year " << '\n';
- std::cin >> end;
- return false;
- }
- }
- else { return true; }
- }
- return 0; // STUB
- }
Advertisement
Add Comment
Please, Sign In to add comment