Print the name of day How can I improve this code? It takes a number in the range of [1,7] and prints its corresponding day.

#include "stdafx.h"
#include &lt;iostream&gt;     // std::cout
#include &lt;vector&gt;       // std::vector
#include &lt;string&gt;

using std::cin;
using std::cout;
using std::vector;
using std::string;



int main()
{
    vector&lt;string&gt; name_day = { "Saturday" ,"Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" };
    int n ;

    cout &lt;&lt; "This program takes a number in the range of" &lt;&lt; "\n" &lt;&lt; "[1 , 7] and prints its corresponding day." &lt;&lt; "\n" &lt;&lt; "............................................................." &lt;&lt; "\n";


    while (true) 
    {
        bool flag = true;

        cout &lt;&lt; "Input a number : ";
        cin &gt;&gt; n;

        if (!cin) //If input is wrong.
        {
            // reset failbit
            cin.clear();
            cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
        }

        //If the condition is not satisfied .

        else
        {
            for (int i = 0; i &lt;= 6; i++)
            {
                if (n-1 == i)
                {
                    cout &lt;&lt; name_day[i] &lt;&lt; "\n" &lt;&lt; "-----------------------" &lt;&lt; "\n";;
                    flag = false;
                }

            }//end of for 
        }


        if (flag) { cout &lt;&lt; "Bad Entery !." &lt;&lt; "\n" &lt;&lt; "-----------------------" &lt;&lt; "\n"; }


    }//end of while 


    system("pause");
}
Here are some things that may help you improve your program.

Isolate platform-specific code

If you must have stdafx.h, consider wrapping it so that the code is portable:

#ifdef WINDOWS
#include "stdafx.h"
#endif


Make sure you have all required #includes

The code uses std::numeric_limits but doesn't #include &lt;limits&gt;.  A program may compile without the explicit #include because some other header might happen to include it, but that is not guaranteed by the standard.  To compile reliably, the documented #include files should be used.

Don't use system("pause")

There are two reasons not to use system("cls") or system("pause").  The first is that it is not portable to other operating systems which you may or may not care about now.  The second is that it's a security hole, which you absolutely must care about.  Specifically, if some program is defined and named PAUSE or pause, your program will execute that program instead of what you intend, and that other program could be anything.  First, isolate these into a seperate functions pause() and then modify your code to call those functions instead of system.  Then rewrite the contents of those functions to do what you want using C++.  For example:

void pause() {
    getchar();
}


Use const where practical

The name_day vector is not and should not be modified, so it should be declared const.  Better yet, it could be declared static const.

Use better naming

The vector named name_day is well named because it is descriptive of the contents.  However, flag is not a good name because it doesn't suggest the meaning of the variable in the context of the program.

Use string concatenation

The main routine includes this very long line:

std::cout &lt;&lt; "This program takes a number in the range of" &lt;&lt; "\n" &lt;&lt; "[1 , 7] and prints its corresponding day." &lt;&lt; "\n" &lt;&lt; "............................................................." &lt;&lt; "\n";


Each of those is a separate call to operator&lt;&lt; but they don't need to be.  Another way to write that would be like this:

std::cout &lt;&lt; "This program takes a number in the range of\n" 
             "[1 , 7] and prints its corresponding day.\n"
             ".............................................................\n";


This reduces the entire menu to a single call to operator&lt;&lt; because consecutive strings in C++ (and in C, for that matter) are automatically concatenated into a single string by the compiler.  It is also more readable.

Fix the spelling error

The error string currently says "Bad Entery !."  With corrected punctuation and spelling, that becomes "Bad entry!".  However, it would be even better if the error message suggested what was actually wrong and gave some suggestion to the user about what to do about it.

Use indexing rather than a loop

The current code has this peculiar bit of code:

for (int i = 0; i &lt;= 6; i++)
{
    if (n-1 == i)
    {
        std::cout &lt;&lt; name_day[i] &lt;&lt; "\n" &lt;&lt; "-----------------------" &lt;&lt; "\n";;
        flag = false;
    }

}//end of for 


Instead of a loop to look up the day, why not simply use an index?  Here's one way to do it:

while (true) {
    std::cout &lt;&lt; "Input a number : ";
    std::cin &gt;&gt; n;
    if (std::cin &amp;&amp; n &gt;= 1 &amp;&amp; n &lt;= 7) {
        std::cout &lt;&lt; name_day[n-1] &lt;&lt; "\n----------------\n";
    } else {
        std::cout &lt;&lt; "Bad entry: The number must be in the range 1-7 inclusive\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
    }
}


Note that this will flush the input stream on any error which differs from the behavior of the current code.

Consider the user of the code

There is currently no graceful way to end the program.  It would be nicer if, for instance, a value of 0 or maybe -1 were used as a signal to end the program. 

Think carefully about using using

There is nothing inherently wrong with these lines:

using std::vector;
using std::string;


However, since each is only used once, I'd probably omit them and simply use the full namespace in the code:

static const std::vector&lt;std::string&gt; name_day{ 
    "Saturday" ,"Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" };


Consider using constexpr

Because it is a vector of strings, one can't make name_day a constexpr value, but it could be simply by making a minor change:

static constexpr const char* name_day[]{ 
    "Saturday" ,"Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" };


This also means that the #include &lt;vector&gt; and #include &lt;string&gt; are no longer needed.