2.6. Outputting Variables

You can output the value of a variable using the same commands we used to output simple values. After observing the output, try inputting your own time!

This program outputs the current time, according to the values you provide for hour and minute.

This program creates two integer variables named hour and minute, and a character variable named colon. It assigns appropriate values to each of the variables and then uses a series of output statements to generate the following:

The current time is 11:59

When we talk about “outputting a variable,” we mean outputting the value of the variable. To output the name of a variable, you have to put it in quotes. For example: cout << "hour"; The output of this statement is as follows.

hour

As we have seen before, you can include more than one value in a single output statement, which can make the previous program more concise:

This program does the same thing as the previous, but the print statements have been condensed to one line. This is better style.

On one line, this program outputs a string, two integers, a character, and the special end of line character. Very impressive!

Construct a main function that assigns “Hello” to the variable h, then prints out h’s value.

More to Explore

You have attempted of activities on this page