The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. |
![]() |
Home ![]() ![]() |
||
See also: Return Values, Variables | ||
![]() ![]() ![]() ![]() ![]() ![]() |
||
Outputting Variables
You can output the value of a variable using the same commands we used to output simple values. int hour, minute;char colon; hour = 11; minute = 59; colon = ':'; cout << "The current time is "; cout << hour; cout << colon; cout << minute; cout << endl; 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:59When 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"; 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: int hour, minute;char colon; hour = 11; minute = 59; colon = ':'; cout << "The current time is " << hour << colon << minute << endl; On one line, this program outputs a string, two integers, a character, and the special value endl. Very impressive!
|
||
Home ![]() ![]() |