The Issue is when first time I run my Program and enter 5 than program will return Friday and after this press y to restart the operation without closing the console and enter 9 the program will return the default error message and will also return Friday which is a bad error in my code and I cannot Identify what makes it wrong. The Code:
#include <iostream>
#include <limits>
using namespace std;
enum Dayofweek {monday = 1 , tuesday = 2, wednesday = 3, thursday = 4, friday = 5, saturday = 6, sunday = 7};
string Day (Dayofweek);
int main()
{
int i;
char resTart;
Dayofweek d = monday;
do
{
cin.clear();
system ("cls");
cout << "Enter the day of a week:[1] [2] [3] [4] [5] [6] [7]: ";
while (!(cin >> i))
{
//system ("cls");
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system ("cls");
cout << "Invalid Input detected, Only numbers are allowed. 1, 2, 3, 4 ,5 ,6 ,7. Try Again." << '\n';
cout << "Enter the day of a week: ";
}
cout << Day (Dayofweek (i)) << '\n';
do
{
cin.clear();
cout << "Do you want to Continue [Y/n]" << '\n';
cin >> resTart;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system ("cls");
}while (resTart != 'Y' && resTart != 'y' && resTart != 'N' && resTart != 'n' );
}while (resTart == 'Y' || resTart == 'y');
}
string Day (Dayofweek d)
{
switch (d)
{
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
default:
cout << "Invalid Input detected, Only numbers are allowed in limit to 7 days of week, Try Again." << '\n';
}
}
OutPut in Console: Enter the day of a week:1 [2] [3] [4] [5] [6] [7]: 9 Invalid Input detected, Only numbers are allowed. 1, 2, 3, 4 ,5 ,6 ,7. Try Again. Friday Do you want to Continue [Y/n]
The Day
function is supposed to return a string
. However, you are not returning a string
in the default
case of the switch statement, which invokes undefined behaviour (in your case, the function returned "Friday", but anything can happen).
You need to return something for the program to be well-defined:
string Day (Dayofweek d)
{
switch (d)
{
case 1:
return "Monday";
// etc ...
case 7:
return "Sunday";
default:
cout << "Invalid Input detected, Only numbers are allowed in limit to 7 days of week, Try Again." << '\n';
return "not a day";
}
}
I strongly recommend turning on all warnings in your compiler (e.g. with gcc
, and clang
, you can pass -Wall
as a compilation flag). The compiler will let you know that you are doing something that might be wrong.
Just return a null string when the execution end's with an invalid value.
string Day (Dayofweek d)
{
string null;
switch (d)
{
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
default:
cout << "Invalid Input detected, Only numbers are allowed. 1, 2, 3, 4 ,5 ,6 ,7. Try Again." << '\n';
return string (null);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With