Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,326 Categories: 12

Total Tutorial Views: 41,405,347

C++ Beginner's Tutorial: Expanding Hello, World.

Tutorial Name: C++ Beginner's Tutorial: Expanding Hello, World.  

Category: PC Tutorials

Submitted By: Nissan

Date Added:

Comments: 0

Views: 2,501

Related Forum: PC Building Forum

Share:

Expanding Hello, World!


Now that weve learnt how variables work we can start to incorporate them into our programs. But the problem is, all we can do is output and set the values of variables. That isnt really very useful in creating a proper program. What we really need are ways to manipulate variables and receive input from the userLuckily, thats whats coming next!

The Iostream Library

Weve already learnt that the iostream library can output data, but it can also receive input data too. The command to do this is very similar to the cout statement; its cin. There is one important difference however; when using cin the operator after it is reversed. Instead of being << it is >>. This will probably be confusing to remember at first, but you just need to learn that the open part of the operator faces the end of the stream; the source of a river is the small bit, and the mouth is much wider. Note that when actually running the program you will need to press the enter key after inputting the desired information, like a normal terminal/cmd prompt.
cin is used in almost exactly the same way as cout:


cout << "string";
cout << var;
 
cin >> var;


Now, as you can see it is very simple to get the user's input using the iostream library and thus produce some slightly more useful programs. iostream is much more powerful than that, however; you can combine multiple statements into one. For example, to output two strings or variables you would normally need to write two statements. However, it is possible to combine multiple cout statements together and produce much shorter pieces of code. All you need to do it just add another operator for each item you are outputting or inputting:


cout << "Hello, " << "World!";


You can also combine cin statements together in the same way to receive several pieces of input in a row. Enter still needs to be pressed after each one, however, and you cannot mix input and output on one line of code.

The New Program

Now that we can input and output data we can make some more useful programs. In fact, we are going to do that right now by improving our 'Hello, World!' program to include some user interaction. Once again, we will look over the code step-by-step:


#include <iostream>
using namespace std;
 
int userAge = 0;
char userFavLetter = '';
 
int main()
{
 
    cout << "Hello there!\n" << "How old are you, in years? ";
    cin >> userAge;
    cout << "You're really " << userAge << " years old?\n";
    cout << "Well then, what is your favourite letter of the alphabet?\n";
    cin >> userFavLetter;
    cout << "I like the letter " << userFavLetter << " too!";
     
    return 0;
}


As per usual we have declared the that we are using the iostream library in the standard namespace. After that you will see some variable declarations, but they are slightly different to before: we have combined an assignment with the declaration. This is perfectly acceptable and is a very useful feature. In fact, it has it's own name: initialisation. Initialisation is a recommended and standard practice, so I encourage you to do it. It's less effort, after all!
The next line is main, and then we are into the code. As you can see it is all just standard input and output, but we will go through it step-by-step for clarification. First, the program outputs a greeting and then asks the player how old they are. The little '\n' character is an escape character, and means that the program should move on to the next line, hence it's name; a newline character. There are many different escape characters, and are all identified by the preceding backslash. Escape characters are never printed by the program, but instead trigger some other kind of action, such as moving to the next line or printing a tab.
The next line stores whatever the user entered into the userAge variable, which if you cast your eye up you will see is an integer variable. After receiving this input the program spits it back out at the user in the midst of a sentence exclaiming their age. This process is then repeated, but this time the computer asks the user for their favourite letter instead, before storing it in a character variable. main then ends and returns a value of 0, terminating the program.

There, I hope that wasn't too difficult to understand, and I also hope that you can already see the power of the iostream library, despite the fact that we used it to do some rather pointless things. Still, at least we can write more useful programs now!

Next tutorial will be on Operators

Ratings

Current rating: 10.00 by 2 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"C++ Beginner's Tutorial: Expanding Hello, World." :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.