C++ Tutorial 5: Input and Specialized output

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 except 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”.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s