Problem_all_the_time

calender.cpp

Oct 6th, 2022
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.85 KB | None | 0 0
  1. #include "calender.hpp"
  2.  
  3.  
  4. // 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
  5.  
  6. /* CONTRACT
  7. This is my main function
  8. Pre-condition: It interacts with the user and asks for data via input
  9. 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
  10. */
  11. int main() {
  12. int askDay,month,start,end,year;
  13. std::cout << "Enter Day in number form: ";
  14. std::cin >> askDay;
  15. std::cout << '\n';
  16. std::string askMonth,nameOfDay;
  17. std::cout << "Enter Month: ";
  18. std::cin >> askMonth;
  19. std::cout << '\n';
  20. std::cout << "Choose a year to start at: ";
  21. std::cin >> start;
  22. std::cout << '\n';
  23. std::cout << "Choose the ending of it:";
  24. std::cin >> end;
  25. std::cout << '\n';
  26. calender Mycalender;
  27. Mycalender.GetDays(askDay);
  28. Mycalender.GetMonth(askDay, month, askMonth, nameOfDay);
  29. Mycalender.GetYear(start, end, year);
  30. std::cout << nameOfDay << " " << askMonth << " ";
  31. std::cout << askDay << "/" << month << "/" << year;
  32. std::cout << '\n';
  33. return 0;
  34. }
  35.  
  36. /* CONTRACT:
  37. default contructor. A quarter of it's data will be based on the user and the user will not know.
  38. Users will meet conditional expression based off the given conditional expressions
  39. Pre-condition: It takes the input of the user and compares it's own data with the users data.
  40. @Post-condition: Day must be < 0 and less than 31. Start must be less than end
  41. */
  42. calender::calender() {
  43. // If the pointer does not work, add int &copyOfIterator into the constructor
  44. aCalenderBag Bag;
  45. for ( int numDays = 1; numDays < 32; numDays++ ) {
  46. for (int j = 0; j < 32; j++) { Bag.Days[j] = numDays; }
  47. }
  48. for ( int i = 0; i < 20000; i++ ) { Bag.Years[i] = 0; }
  49.  
  50. }
  51.  
  52. /* CONTRACT
  53. This is my GetDays function.
  54. 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
  55. Post-condtion: it will store the data if it is correct and return back to main
  56. */
  57. int calender::GetDays(int &Day) {
  58. // Create a constructor only and only when you're allocating memory on stack
  59. // When you don't know the size of an array, you need to allocate memory dymanically
  60. aCalenderBag Bag;
  61. int i = 0;
  62. if ((Day > 0) && (Day < 32)) { return Day; }
  63. while ( i < 34 ) {
  64. if (Day != Bag.Days[i]) {
  65. i++;
  66. if ( i == 31 ) {
  67. std::cout << "Invalid Number Detected!";
  68. std::cout << '\n';
  69. i = 34;
  70. }
  71. }
  72. }
  73. while ( Day < 0 ) {
  74. std::cout << "Please choose a number higher than 0";
  75. std::cin >> Day;
  76. if ( Day > 0 ) { return calender::GetDays(Day); }
  77. }
  78. while ( Day > 31 ) {
  79. std::cout << "Please choose something lower than 31";
  80. std::cin >> Day;
  81. if ( Day < 31) { return calender::GetDays(Day); }
  82. }
  83. return 0; // STUB
  84. }
  85.  
  86. /* CONTRACT
  87. This is my GetMonth function
  88. Pre-condition: It compares the users input and if the input is not correct, it will recrusive call itself until there is a match
  89. 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
  90.  
  91. */
  92. void calender::GetMonth(int &day, int &month, std::string &Month, std::string &nameOfDay) {
  93. aCalenderBag Bag;
  94. // Add more here. Make sure if the user chooses 28 along with feb
  95. if ( Month == Bag.Months[0]) { month = 9; }
  96. else if ( Month == Bag.Months[1] ) { month = 10; }
  97. else if ( Month == Bag.Months[2] ) { month = 11; }
  98. else if (Month == Bag.Months[3] ) { month = 12; }
  99. else if ( Month == Bag.Months[4] ) { month = 1; }
  100. else if ( Month == Bag.Months[5] ) { month = 2; }
  101. else if ( Month == Bag.Months[6] ) { month = 3; }
  102. else if ( Month == Bag.Months[7] ) { month = 4; }
  103. else if ( Month == Bag.Months[8] ) { month = 5; }
  104. else if (Month == Bag.Months[9] ) { month = 6; }
  105. else if (Month == Bag.Months[10] ) { month = 7; }
  106. else if ( Month == Bag.Months[11] ) { month = 8;}
  107. else {
  108. std::cout << "Not A valid Month! " << "Re-enter a valid date: " << '\n';
  109. std::cin >> Month;
  110. return calender::GetMonth(day,month,Month,nameOfDay);
  111. }
  112. calender::AssignNameOfDay(day,nameOfDay);
  113. return;
  114. }
  115.  
  116. /* CONTRACT
  117. This is my AssignNameOfDay function
  118. Pre-condition: it will only be called if calender::GetMonth passes all of the checks. Otherwise it will not be called
  119. post-condition: It will assign the nameOfDay variable depending on variable day
  120. */
  121. void calender::AssignNameOfDay(int &day, std::string &nameOfDay) {
  122. aCalenderBag Bag;
  123. if ( day == 1 ) {
  124. nameOfDay = Bag.nameOfDay[0];
  125. return;
  126. }
  127. else if ( day == 2 ) {
  128. nameOfDay = Bag.nameOfDay[1];
  129. return;
  130. }
  131. else if ( day == 3 ) {
  132. nameOfDay = Bag.nameOfDay[2];
  133. return;
  134. }
  135. else if ( day == 4 ) {
  136. nameOfDay = Bag.nameOfDay[3];
  137. return;
  138. }
  139. else if ( day == 5 ) {
  140. nameOfDay = Bag.nameOfDay[4];
  141. return;
  142. }
  143. else if ( day == 6 ) {
  144. nameOfDay = Bag.nameOfDay[5];
  145. return;
  146. }
  147. else if ( day == 7 ) {
  148. nameOfDay = Bag.nameOfDay[6];
  149. return;
  150. }
  151. //
  152. if ( day == 8 ) {
  153. nameOfDay = Bag.nameOfDay[0];
  154. return;
  155. }
  156. else if ( day == 9 ) {
  157. nameOfDay = Bag.nameOfDay[1];
  158. return;
  159. }
  160. else if ( day == 10 ) {
  161. nameOfDay = Bag.nameOfDay[2];
  162. return;
  163. }
  164. else if ( day == 11 ) {
  165. nameOfDay = Bag.nameOfDay[3];
  166. return;
  167. }
  168. else if ( day == 12 ) {
  169. nameOfDay = Bag.nameOfDay[4];
  170. return;
  171. }
  172. else if ( day == 13 ) {
  173. nameOfDay = Bag.nameOfDay[5];
  174. return;
  175. }
  176. else if ( day == 14 ) {
  177. nameOfDay = Bag.nameOfDay[6];
  178. return;
  179.  
  180. }
  181. //
  182. if ( day == 15 ) {
  183. nameOfDay = Bag.nameOfDay[0];
  184. return;
  185. }
  186. else if ( day == 16 ) {
  187. nameOfDay = Bag.nameOfDay[1];
  188. return;
  189. }
  190. else if ( day == 17 ) {
  191. nameOfDay = Bag.nameOfDay[2];
  192. return;
  193. }
  194. else if ( day == 18 ) {
  195. nameOfDay = Bag.nameOfDay[3];
  196. return;
  197. }
  198. else if ( day == 19 ) {
  199. nameOfDay = Bag.nameOfDay[4];
  200. return;
  201. }
  202. else if ( day == 20 ) {
  203. nameOfDay = Bag.nameOfDay[5];
  204. return;
  205. }
  206. else if ( day == 21 ) {
  207. nameOfDay = Bag.nameOfDay[6];
  208. return;
  209. }
  210. //
  211. if ( day == 22 ) {
  212. nameOfDay = Bag.nameOfDay[0];
  213. return;
  214. }
  215. else if ( day == 23 ) {
  216. nameOfDay = Bag.nameOfDay[1];
  217. return;
  218. }
  219. else if ( day == 24 ) {
  220. nameOfDay = Bag.nameOfDay[2];
  221. return;
  222. }
  223. else if ( day == 25 ) {
  224. nameOfDay = Bag.nameOfDay[3];
  225. return;
  226. }
  227. else if ( day == 26 ) {
  228. nameOfDay = Bag.nameOfDay[4];
  229. return;
  230. }
  231. else if ( day == 27 ) {
  232. nameOfDay = Bag.nameOfDay[5];
  233. return;
  234. }
  235. else if ( day == 28 ) {
  236. nameOfDay = Bag.nameOfDay[6];
  237. return;
  238. }
  239. //
  240. if ( day == 29 ) {
  241. nameOfDay = Bag.nameOfDay[0];
  242. return;
  243. }
  244. else if ( day == 30 ) {
  245. nameOfDay = Bag.nameOfDay[1];
  246. return;
  247. }
  248. else if ( day == 31 ) {
  249. nameOfDay = Bag.nameOfDay[2];
  250. return;
  251. }
  252. else if ( day == 32 ) {
  253. nameOfDay = Bag.nameOfDay[3];
  254. return;
  255. }
  256. }
  257.  
  258. /* CONTRACT
  259. This is my GetYear function.
  260. 20000 is the max Bag.Years[] can handle. There's if-statement to check if the user has exceeded the max of elements
  261. Pre-condition: Five conditions you must follow:
  262. * start < end
  263. * start < 0
  264. * start > end
  265. * end > 20000
  266. * start > 20000
  267. * If enters data incorrectly, it will execute one of these following conditions
  268. Post-condition: It will make sure whatever input the user gives is checked and will compare the data with the private data
  269. */
  270. void calender::GetYear(int &start, int &end, int &year) {
  271. // there's a bug around in this code. I am trying to find it. October 5, 2022
  272. aCalenderBag Bag;
  273. int tempStart = start; int tempEnd = end;
  274. int numYears = (tempEnd - tempStart) + 1;
  275. while ( end < start ) {
  276. // The bug occurs around here somewhere. With certain combinations it'll pop up
  277. end = 0;
  278. std::cout << "Invalid number!, Please choose a number greater than the start of the year" << '\n';
  279. std::cin >> end;
  280. if ( end > start ) { return calender::GetYear(start,end,year); }
  281. }
  282. while (start < 0 ) {
  283. std::cout << "Invalid number!, Please choose a number greater than 0: " << '\n';
  284. std::cin >> start;
  285. if ( start > 0 ) { return calender::GetYear(start,end,year); }
  286. }
  287. while ( start > end ) {
  288. std::cout << "Invalid number!, Please choose something a number less than the end year: ";
  289. std::cin >> start;
  290. int newStart = start;
  291. if ( newStart < end ) { return calender::GetYear(start,end,year); }
  292. }
  293. while ( end > 20000 ) {
  294. // This works
  295. numYears = 0;
  296. std::cout << "Error! The end of the year you have choses is too high " << '\n';
  297. std::cout << "Re-enter end of the year: " << '\n';
  298. std::cin >> end;
  299. if ( end < 20000 ) { return calender::GetYear(start,end,year); }
  300. }
  301. while ( start > 20000 ) {
  302. std::cout << "Re-enter start of year " << '\n';
  303. std::cin >> start;
  304. if ( start < 20000 ) { return calender::GetYear(start,end,year); }
  305.  
  306. }
  307. calender::AssignYears(start, end, Bag);
  308. calender::Print(numYears, Bag);
  309. std::cout << "Please select a year: ";
  310. std::cin >> year;
  311. bool Check = calender::CheckUserAnswer(numYears, start, end, year, Bag);
  312. if ( Check == false ) { return calender::GetYear(start,end,year); }
  313. else if ( Check == true ) { return; }
  314. }
  315.  
  316. /* CONTRACT
  317. This is my Print function
  318. Pre-condition: none
  319. Post-condition: it prints the values from start to end
  320. */
  321. void calender::Print(int &iter, aCalenderBag &Bag) const {
  322. std::cout << "This is what you can choose from: " << '\n';
  323. for (int i = 0; i < iter; i++) { std::cout << Bag.Years[i] << '\n'; }
  324. return;
  325. }
  326. /* CONTRACT
  327. This is my AssignYears function
  328. Pre-condition: start <= end
  329. Post-condition: It takes the start and end and starts populating the array based on the input the user gave
  330. */
  331. void calender::AssignYears(int &start, int &end, aCalenderBag &Bag) {
  332. int j = 0;
  333. for (int i = start; i <= end; i++) {
  334. Bag.Years[j] = i;
  335. j += 1;
  336. }
  337. Bag.Years[j + 1] = end;
  338. return;
  339. }
  340.  
  341. /* CONTRACT
  342. This is my CheckUserAnswer
  343. Pre-condtion: i < iter, counter == iter
  344. 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
  345. */
  346. bool calender::CheckUserAnswer(int &iter, int &start, int &end, int &year, aCalenderBag &Bag) const {
  347. int counter = 0;
  348. for (int i = 0; i < iter; i++) {
  349. if ( year != Bag.Years[i] ) {
  350. counter += 1;
  351. if ( counter == iter ) {
  352. std::cout << "Error! Not within the given time frames!" << '\n';
  353. std::cout << "Re-enter start of year " << '\n';
  354. std::cin >> start;
  355. std::cout << "Re-enter end of the year " << '\n';
  356. std::cin >> end;
  357. return false;
  358. }
  359. }
  360. else { return true; }
  361. }
  362.  
  363. return 0; // STUB
  364. }
  365.  
  366.  
  367.  
  368.  
  369.  
Advertisement
Add Comment
Please, Sign In to add comment