C++ Tutorial 5: Input and Specialized output

C++ has an interesting way of doing output:

Code

cout << "SomeThingHere" << variable << endl;




cout also has a brother called cerr. Cerr works the same way but it is used for errors instead of just normal output, it will also log the message to the console on most systems so:

Code

cerr << "Error: Some Error Happened" << endl;




If you look in the logs you will find that Program: Error: Some Error Happened or something like that depending on your system.

To get input in C++ you use cin. Now here is an example project combining both cout, cerr, and cin.

Code

int main()
{
    while (true)
    {
        cout << "Please Enter a Command! Type Help for Commands" << endl;
        string userInput = "";
        cin >> userInput;
        if (userInput == "help")
        {
            cout << "nnHelp -- Displays This" << endl;
        }
        else
        {
            cerr << "Error: Invalid Command";
        }
    }
    return 0;
}




Cin works like this:

cin >> variable;

notice << versus >>;

<< is sending the text to cout while >> is sending the input to the user :).

Finally There is one more thing:

clog

clog is the same as cerr exc

ept that it is used for logs no errors. So:

Code

clog << "Logged" << endl;




would create a message in the logs on most sytems which is “Logged”.

What Do You Think on This ? Say Here