Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Print 7 Days of week by taking user input?

Tags:

c++

codeblocks

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]

enter image description here

like image 707
Arnold-Baba Avatar asked May 06 '20 17:05

Arnold-Baba


2 Answers

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.

like image 77
cigien Avatar answered Sep 23 '22 11:09

cigien


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);
    }
}
like image 22
Саша Avatar answered Sep 20 '22 11:09

Саша