C语言输入输出与循环语句,如何成?
摘要:Basic InputOutput The example programs of the previous sections provided little interaction with the user, if any at al
目录Basic Input/OutputStandard output (cout)Standard input (cin)cin and stringsstringstreamStatements and flow controlSelection statements: if and elseIteration statements (loops)The while loopThe do-while loopThe for loopRange-based for loopJump statementsThe break statementThe continue statementThe goto statementReferences
Basic Input/Output
The example programs of the previous sections provided little interaction with the user, if any at all. They simply printed simple values on screen, but the standard library provides many additional ways to interact with the user via its input/output features. This section will present a short introduction to some of the most useful.
C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen, the keyboard or a file. A stream is an entity where a program can either insert or extract characters to/from. There is no need to know details about the media associated to the stream or any of its internal specifications. All we need to know is that streams are a source/destination of characters, and that these characters are provided/accepted sequentially (i.e., one after another).
The standard library defines a handful of stream objects that can be used to access what are considered the standard sources and destinations of characters by the environment where the program runs:
stream
description
cin
standard input stream
cout
standard output stream
cerr
standard error (output) stream
clog
standard logging (output) stream
We are going to see in more detail only cout and cin (the standard output and input streams); cerr and clog are also output streams, so they essentially work like cout, with the only difference being that they identify streams for specific purposes: error messages and logging; which, in many cases, in most environment setups, they actually do the exact same thing: they print on screen, although they can also be individually redirected
