From ff17076e9ad50ae75025a52d6391c412aef2436d Mon Sep 17 00:00:00 2001 From: antonioialazzo Date: Mon, 10 Nov 2025 11:32:32 +0100 Subject: [PATCH 1/2] feat: solutions to exercises 1 and 2 --- exercises/ex1.cpp | 70 +++++++++++++++++++++++------------------------ exercises/ex2.cpp | 47 +++++++------------------------ 2 files changed, 44 insertions(+), 73 deletions(-) diff --git a/exercises/ex1.cpp b/exercises/ex1.cpp index 22069589..0a371cf2 100644 --- a/exercises/ex1.cpp +++ b/exercises/ex1.cpp @@ -7,42 +7,40 @@ int main() { int week; - /* Input week number from user */ - cout << "Enter week number(1-7): " << endl; - cin >> week; - - if (week == 1) - { - cout << "Monday" << endl; - } - else if (week == 2) - { - cout << "Tuesday" << endl; - } - else if (week == 3) - { - cout << "Wednesday" << endl; - } - else if (week == 4) - { - cout << "Thursday" << endl; - } - else if (week == 5) - { - cout << "Friday" << endl; - } - else if (week == 6) - { - cout << "Saturday" << endl; - } - else if (week == 7) - { - cout << "Sunday" << endl; - } - else - { - cout << "Invalid input! Please enter week number between 1-7." << endl; - } + do{ + /* Input week number from user */ + cout << "Enter week number(1-7): " << endl; + cin >> week; + + switch (week) { + + case 1: + cout << "Monday" << endl; + break; + case 2: + cout << "Tuesday" << endl; + break; + case 3: + cout << "Tuesday" << endl; + break; + case 4: + cout << "Thursday" << endl; + break; + case 5: + cout << "Friday" << endl; + break; + case 6: + cout << "Saturday" << endl; + break; + case 7: + cout << "Sunday" << endl; + break; + default: + cout << "----------------------------\nInvalid input, please retry!" << endl; + + } + + } while(week > 7 || week < 1); return 0; } \ No newline at end of file diff --git a/exercises/ex2.cpp b/exercises/ex2.cpp index 9e8cd8e4..b2eb8ec5 100644 --- a/exercises/ex2.cpp +++ b/exercises/ex2.cpp @@ -10,42 +10,15 @@ using namespace std; int main() { int week; - - cout << "Enter week number(1-7): " << endl; - cin >> week; - - if (week == 1) - { - cout << "Monday" << endl; - } - else if (week == 2) - { - cout << "Tuesday" << endl; - } - else if (week == 3) - { - cout << "Wednesday" << endl; - } - else if (week == 4) - { - cout << "Thursday" << endl; - } - else if (week == 5) - { - cout << "Friday" << endl; - } - else if (week == 6) - { - cout << "Saturday" << endl; - } - else if (week == 7) - { - cout << "Sunday" << endl; - } - else - { - cout << "Invalid input! Please enter week number between 1-7." << endl; - } - + do{ + cout << "Enter week number(1-7): " << endl; + cin >> week; + if (week > 7 || week < 1){ + cout << "Invalid input! Please enter week number between 1-7." << endl; + } + } while(week > 7 || week < 1); + week--; + string arr[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}; + cout << arr[week]<< endl; return 0; } \ No newline at end of file From f40b723982d2c2a74789ba4d0aea95b8289098e8 Mon Sep 17 00:00:00 2001 From: antonioialazzo Date: Mon, 10 Nov 2025 12:00:03 +0100 Subject: [PATCH 2/2] style: remove trailing whitespace and redundant endl --- exercises/ex1.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/exercises/ex1.cpp b/exercises/ex1.cpp index 0a371cf2..53c7c692 100644 --- a/exercises/ex1.cpp +++ b/exercises/ex1.cpp @@ -14,32 +14,32 @@ int main() switch (week) { - case 1: - cout << "Monday" << endl; + case 1: + cout << "Monday"; break; case 2: - cout << "Tuesday" << endl; + cout << "Tuesday"; break; - case 3: - cout << "Tuesday" << endl; + case 3: + cout << "Tuesday"; break; case 4: - cout << "Thursday" << endl; + cout << "Thursday"; break; - case 5: - cout << "Friday" << endl; + case 5: + cout << "Friday"; break; case 6: - cout << "Saturday" << endl; + cout << "Saturday"; break; - case 7: - cout << "Sunday" << endl; + case 7: + cout << "Sunday"; break; default: - cout << "----------------------------\nInvalid input, please retry!" << endl; + cout << "----------------------------\nInvalid input, please retry!"; } - + cout << endl; } while(week > 7 || week < 1); return 0;