Team 2550
Technical Documentation
|
|
As you work with code, outputting information is critical. The console (aka: terminal, command line, command prompt, or shell) is the easiest way to do this. C++ has a standard syntax for streaming I/O - including the console and files.
You can output any single value by using the <<
operator.
using namespace std;
is important here. The namespace feature of C++ exists in order to prevent name conflicts. If that line is omitted, every instace of something within the std
namespace must be prefixed with std::
. For example, std::cout << x;
. For now, this is fairly insignificant, but namespaces become important when working with multiple files.Any operation that yields a simple data type (boolean, integer, float) will be evaluated before it is output, so, the cout << 2 + 2;
will output 4
. If you want a statement to be output literally (such as 2 + 2
instead of 4
), place the text in double quotes (cout << "2 + 2";
).
Another feature worth noting is that the char
datatype outputs the corresponding character. The value of the char variable is output as its corresponding character on the ASCII chart. In order to output the raw value of a character, use `static_cast<int>('a')`.
char
constant (const char ...
). Double quotes allow for entire strings of text and are much more complex.Escape codes are single characters that allow you to output nonprinting characters. For example, in C++, strings are always enclosed in quotes. Placing a quote prematurely will terminate the string. Escape codes fix this.
Escape Sequence | Output |
---|---|
\' | single quote |
\" | double quote |
\? | question mark |
\\ | backslash |
\0 | null character, this is often used to denote the end of a transmission or string |
\a | audible bell - sends a system beep sound |
\b | backspace |
\f | form feed - new page |
\n | new line, this is very useful |
\r | carriage return |
\t | horizontal tab |
\v | vertical tab |
\unnnn | outputs a unicode character |
"This string \"has a quotation" and makes the computer beep. "
There are also several operations that you can run on the stream itslef. Assumes that the line using namespace std;
is at the top of the file.
Command | Action | Example |
---|---|---|
boolalpha & noboolalpha | toggles the numeric and text format for bool values | cout << boolalpha << true; will output true insetad of 1 . |
showpoint & noshowpoint | toggles the decimal point on x.0 values | cout << showpoint << 0; outputs 0.0 instead of 0 |
showpos & noshowpos | shows the + sign in front of positive numbers | cout << showpos << 3.14; outputs +3.14 |
fixed & scientific | outputs floating point numbers in either scientific or fixed notation | cout << scientific << 0.01; outputs 1.000000e-02 |
left | sets the alignment of fixed width output to left | cout << left ...; |
right | sets the alignment of fixed width output to right | cout << right ...; |
internal | sets the alignment of fixed width output with multiple components to have whitespace in the middle | cout << internal << scientific << 0.01 outputs 1.000000*****e-2 where * is set as the fill character (see iomanip) |
uppercase & nouppercase | toggles uppercase letters for values expressed in scientific notation | cout << uppercase << scientific << 23.145; outputs 2.3145E-1 |
flush | outputs the buffer early, which be used for long processes | cout << "this is output first" << flush << "and this is output later"; |
endl | creates a new line and runs flush | cout << a << endl << b; outputs a and b on separate lines assuming they are defined |
Including iomanip
(#include <iomanip
) allows for even finer control of the output. Here are the most notable features.
Command | Action | Example |
---|---|---|
setfill | Sets the fill character for numeric output (often used in combination with setw). | cout << setfill( '\* '); sets the fill character to be an asterisk, note that the argument is a character (enclosed in single quotes) and not a string (enclosed in double quotes). |
setprecision | Sets the precision of floating point numbers, estimates | cout << setprecision(2) << 1.25; outputs 1.3 . |
setwidth | Sets the width of the next input or output field | cout << setfill( '0 ') << setw(5) << 2; outputs 00002 . |
Input is very similar to output, but it has some quirks. With the following code segment...
int x, y, z; cin >> x, y, z;
The input should be formatted like this
1 2 3
The data can then be used for any purpose later in the program.
cin
is whitespace delimited. There can be any number of tabs, spaces, or new lines in the input. Once all of the values have been entered, the user must press enter one last time to send the data.Finally, the user is also able to input data before it is even asked. See the example below.
<<
is known as the insertion operator and >>
is the extraction operator. They are evaluated after other operations.There will not be an error if the user inputs both operands at the first cin
statement. The input is a stream (hence, iostream). Once the first value is read in, the second is stored until it is requested by the program. If it is not needed, it is simply discarded.
In addition to cin
, which is whitespace-delimited, there are also several functions that can be used with the input stream.
SomeFunction();
.SomeFunction(x);
.a = SomeFunction(x, y);
.Example | Action |
---|---|
cin.get(); | Reads one value from the buffer (stream) and discards it. |
cin.get(x); | Reads one value into x. |
cin.ignore(); | Ignores the next character in the buffer. |
cin.ignore(100); | Ignores the next 100 characters in the buffer. |
cin.ignore(100, '\n '); | Ignores the next 100 characters, stops early if a newline is found. |
z=cin.peek(); | Copies the value at the top of the stream into z without discarding it from the buffer. cin.get() . removes the value from the stream, cin.peek() does not. |
cin.putback(x); | Puts x back into the stream. |
If invalid data is provided when reading into cin, the stream will enter a "fail" state and stop reading data. The .clear()
function will return the stream to a usable state. You'll then need to clear the buffer using the .ignore()
function.
cin.clear(); cin.ignore();
An input buffer enters the fail state if the user provides the wrong type of data. For instance, if a float
is supposed to be an int
, the stream will enter a fail state and not read data until the problem is fixed.
The following code will check to make sure that cin
is not in the fail state...
if (!cin) //checks to see if cin is in a fail state { cin.clear(); cin.ignore();