Input and output

Table of Contents


WIP: ChatGPT converted this from my LaTeX document to orgmode so I still need to review this.

1. Interactivity and Output in C++

Most programs feature interactivity in some way or another. This can involve moving a mouse around, clicking on things, tapping on a touch screen, using a gamepad's joysticks and buttons, but we'll be primarily interacting with our C++ programs through keyboard input. Feedback from our C++ programs will come in the form of text output to the screen, as we are writing terminal programs. terminal.png

We will also delve into reading from and writing to text files (or other file formats) later on. But for now, let's focus on the terminal/console. terminal-hi.png

**Outputting Information with `cout`

The `cout` command (pronounced as "c-out" for "console-out") is used to write information to the screen. This can include outputting a string literal:

```cpp cout << "Hello, world!" << endl;

or a variable's value:

cpp

cout << yourName << endl;

or stringing multiple things together:

cpp

cout << "Hello, " << yourName << "!" << endl;

In C++, we use the output stream operator << to string together multiple items for our output.

Newlines with endl

The endl command stands for "end-line" and ensures there is a vertical space between that cout statement and the next one. For example, if we write two cout statements without endl like this:

cpp

cout << "Hello"; cout << "World";

the output will be:

HelloWorld

If we want to separate them on two different lines, we can write:

cpp

cout << "Hello" << endl; cout << "World";

And our output will be:

Hello World

Remember that in C++, a statement ends with a semicolon ;, so you can split your cout statement across multiple lines, as long as you're chaining items together with << and only adding a ; on the last line:

cpp

cout << "Name: " << name << "Age: " << age << "State: " << state << endl;

**Inputting Information with cin

When we want the user to enter a value for a variable using the keyboard, we use the cin command (pronounced as "c-in" or "console-in").

cin >> var; for Variables

For variables like int and float, you will use this format to store data from the keyboard into the variable:

cpp

cin >> VARIABLENAME;

You can also chain cin statements together to read multiple values for multiple variables:

cpp

cin >> VARIABLENAME1 >> VARIABLENAME2 >> ETC;

Strings and cin >>

When using cin >> with a string variable, keep in mind that it will only read until the first whitespace character, meaning it can't capture spaces or tabs. For example:

cpp

string name; cin >> name;

If you enter "Rachel Singh", name will contain "Rachel". To capture spaces, you need to use a different function.

Using getline(cin, var); for Strings

You can use the getline function to capture an entire line of text as a string. This is useful when you want to capture spaces and multiple words. For example:

cpp

string name; getline(cin, name);

Mixing cin >> var; and getline(cin, var);

If you mix cin >> var; and getline(cin, var);, you might encounter issues with the input buffer. To avoid this, use cin.ignore(); before getline(cin, var); if you used cin >> var; before it.

Escape Sequences

There are special characters, called escape sequences, that you can use in your cout statements:

\n: newline \t: tab \": double-quote

For example:

cpp

cout << "hello\nworld" << endl;

Output:

hello world

cpp

cout << "A\tB\tC" << endl; cout << "1\t2\t3" << endl;

Output:

css

A B C 1 2 3

cpp

cout << "He said \"Hi!\" to me!" << endl;

Output:

vbnet

He said "Hi!" to me!

mathematica

Continued Learning

In our journey of C++, we'll explore more topics, including math libraries, random numbers, and other advanced concepts.

Author: Rachel Wil Sha Singh

Created: 2023-10-01 Sun 13:20

Validate