After we have compiled the program above, we can run it. The result will be something like Enter current year then press RETURN.
1996
Enter your current age in years.
36
Enter the year for which you wish to know your age.
2001
Your age in 2001: 41
The first, third, fifth and seventh lines above are produced on the screen by the program. In general, the program statement cout << Expression1 << Expression2 << ... << ExpressionN;
will produce the screen output Expression1Expression2...ExpressionN
The series of statements cout << Expression1;
cout << Expression2;
...
...
cout << ExpressionN;
will produce an identical output. If spaces or new lines are needed between the output expressions, these have to be included explicitly, with a " " or a "\n" respectively. The expression endl can also be used to output a new line, and in many cases is preferable to using "\n" since it has the side-effect of flushing the output buffer (output is often stored internally and printed in chunks when sufficient output has been accumulated; using endl forces all output to appear on the screen immediately).
The numbers in bold in the example screen output above have been typed in by the user. In this particular program run, the program statement cin >> year_now;
has resulted in the variable year_now being assigned the value 2001 at the point when the user pressed RETURN after typing in "2001". Programs can also include assignment statements, a simple example of which is the statement another_age = another_year - (year_now - age_now);
Hence the symbol = means "is assigned the value of". ("Equals" is represented in C++ as ==.)
Friday, July 11, 2008
1.6 Very Simple Input, Output and Assignment
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment