Is the input number even? I just wrote this C++ code in Visual Studio. The code is working fine but I wanted to ask whether my code is written in a safe way or not.

#include "stdafx.h"


#include &lt;iostream&gt;





int input()


{


    int x;
    std::cin &gt;&gt; x;


    return x % 2;


}





int main()


{


    using namespace std;


    cout &lt;&lt; "Enter a digit" &lt;&lt; endl;

    int x = input();

    if (input == 0)




        cout &lt;&lt; "Even";


    else



        cout &lt;&lt; "Odd";


    return 0;

}
Don't import stdafx

It is Windows only and you don't need it.

Remove all those spaces

The code is hard to follow when you must scroll continuously:

#include &lt;iostream&gt;

int input() {
    int x;
    std::cin &gt;&gt; x;
    return x % 2;
}

int main() {
    using namespace std;
    cout &lt;&lt; "Enter a digit" &lt;&lt; endl;
    int x = input();
    if (input == 0)
        cout &lt;&lt; "Even";
    else
        cout &lt;&lt; "Odd";
    return 0;
}


Always use braces

Ugly bugs may hide behind not using parenthesis:

if (input == 0) {
    cout &lt;&lt; "Even";
} else {
    cout &lt;&lt; "Odd";
}


In 2015 avoid return 0

The main() function is different; the compiler writes that for you. 

Separate input and computation

If you care about having separate functions (not really necessary in this trivial case) you should define pure functions with no effect on the outside world:

bool is_even(int x) {
    return x % 2 == 0;
}


Avoid verbosity

A middle level language such as C++ must be pretty verbose, anyway for such trivial cases I propose inlining a ternary:

cout &lt;&lt; ((is_even(x)) ? "Even\n" : "Odd\n");


Don't underestimate yourself

The program works for full numbers, ask `"Enter a number:"

The icing on the cake

using namespace std; is considered bad practice (read about that on StackOverflow)

The final reviewed code

#include &lt;iostream&gt;

bool is_even(int x) {
    return x % 2 == 0;
}

int main() {
    std::cout &lt;&lt; "Enter a number:" &lt;&lt; std::endl;
    int x; std::cin &gt;&gt; x;

    std::cout &lt;&lt; is_even(x) ? "Even\n" : "Odd\n");
}