Input and Output

Table of Contents

c1_u04_InputOutput_terminal-hi.png


Most programs feature interactivity in some way or another. This can be moving a mouse around and clicking on things, tapping on a touch screen, using a gamepad joysticks and buttons, but we’re going to be mostly interfacing with our C++ programs through keyboard input to our programs. Feedback from our C++ programs will come in the form of text output to the screen as well. We are writing terminal programs.

Enter first number: 1300
Enter second number: 37
1300 + 37 = 1337

Our programs run in what is called a console, shell, or terminal. These terms are largely interchangable, and is usually a text-based view with a black background and white text - though you can usually customize the look of your terminal. :)


1. Displaying information to the console

How we display text varies in different programming languages, but each generally has some kind of output statement. There are also different types of data we may want to display on the screen, such as a literal value (some hard-coded number or text), or displaying the value stored in some variable.

Description Python Lua C++
Printing "Error!" to the console print( "Error!" ) print( "Error!" ) cout << "Error!" << endl;
Printing the value of a variable sum print( sum ) print( sum ) cout << sum << endl;
Printing the label "Sum:" and the variable value print( "Sum:", sum ) print( "Sum:", sum ) cout << "Sum: " << sum << endl;

Good design: Usually if you're going to be displaying a variable's value to the screen then you should also be displaying a label for it ahead of time.

If we just display a variable without any label like this:

cout << errorCode << endl;

Then the user of the program might just see a number on the screen and be confused as to what it means…

20

Whenever we're displaying a variable's value, make sure to tell the user what it is!

cout << "Error code: " << errorCode << endl;

2. Adding more vertical whitespace

Good user interface design means making our programs readable, even if they're just programs with a bunch of text. In some cases, you might want to add more spacing between sections of output to make it more readable:

Bad design: This output is all "smushed together" and it can be hard to visually tell where a region begins.

PIZZA HAT
Enter your sauce choice:
1. Tomato
2. Alfredo
3. Buffalo
> 3
Enter your meat choice:
1. No meat
2. Pepperoni
3. Sausage
> 1
Enter your veggies choice:
1. No veggies
2. Pineapple
3. Onions
> 2
Thank you for your purchase!

Good design: The program has been updated to have more spacing between sections.

PIZZA HAT

Enter your sauce choice:
1. Tomato
2. Alfredo
3. Buffalo
> 3

Enter your meat choice:
1. No meat
2. Pepperoni
3. Sausage
> 1

Enter your veggies choice:
1. No veggies
2. Pineapple
3. Onions
> 2

Thank you for your purchase!

You can tell your program to add additional whitespace by either printing an empty string with Python and Lua, or to use the endl (end-line) command for C++ for each time you want some additional vertical space.

Description Python Lua C++
Print an empty line print( "" ) print( "" ) cout << endl;

3. Getting input from the user

There will be different ways of getting user input from the keyboard based on the type of data we want to receive. Additionally, it is good design to display a message to the user to let them know what kind of information we want to receive before actually writing the input command.

3.1. Getting input in Python:

Python has an input() function that allows us to ask the user a question AND get the user's response, all in one line of code. By default, int() receives string data, not numbers! So if we want int or float data, we need to convert from string.

Get string data in Python
my_name = input( "What is your name? " )
Get integer data in Python
my_age = int( input( "What is your age? " )
Get float data in Python
my_gpa = float( input( "What is your gpa? " )

3.2. Getting input in Lua:

Lua uses the io.read() function to read in data from the keyboard. This command doesn't ask the user a question beforehand, so make sure to use a print() statement to tell the user what you're looking for.

Get string data in Lua
print( "What is your name?" )
my_name = io.read( "*l" )
Get numeric (int/float) data in Lua
print( "What is your age?" )
my_age = io.read( "*n" )

print( "What is your gpa?" )
my_gpa = io.read( "*n" )

3.3. Getting input in C++

C++ has two different ways to get input. cin >> can be used for numbers and strings, but for a string it will only allow one word - no spaces! If we want to read in a string with spaces, we need to use the getline command instead. There is also an additional wrench thrown into our input endeavors when we're switching between cin >> and getline( ), just to make our lives a bit more interesting.

Get a one-word string in C++
cout << "What is the state abbreviation? ";
string abbr;
cin >> abbr;
Get a string with whitespace in C++
cout << "What is your name? ";
string name;
getline( cin, name );
Get numeric data in C++
cout << "What is your age? ";
int age;
cin >> age;

cout << "What is your gpa? ";
float gpa;
cin >> gpa;
Clearing the buffer, going from cin >> to getline
If we're going from using cin with the stream operator (cin >> VAR;) to a getline( cin, VAR ); statement, in this particular scenario we need to add a cin.ignore(); to clear out the input buffer:
cout << "Enter your age: ";
int age;
cin >> age;

cout << "Enter your name: ";
string name;
cin.ignore();
getline( cin, name );

It ONLY needs to happen in this one specific scenario!! If you leave it off here, then the program will skip your getline input. If you put cin.ignore(); in the wrong place, then the program will ask for input but probably truncate the very first letter entered in.

First input Then Second input Use cin.ignore(); in between?
cin >> VAR; then getline( cin, STR ); YES
getline( cin, STR1 ); then getline( cin, STR2 ); NO
cin >> VAR1; then cin >> VAR2; NO
getline( cin, STR ); then cin >> VAR; NO

4. Escape sequences

If we're writing some text within a string literal (that is, between two double quotes " ") we can use some special commands for additional formatting. These commands begin with a backslash \ and then a character:

Command Description
\t Insert a tab
\n Insert a new line
\" Insert a double quote
\' Insert a single quote

This can be useful if you need to put a quote inside your string literal. For example:

print( "And then Frank said \"I don't know calculus, I'm just a dog!\"" )

5. Example programs

5.1. Getting two numbers and doing addition

Python
num1 = float( input( "Enter the first number: " ) )
num2 = float( input( "Enter the second number: " ) )
result = num1 + num2
print( "The result is:", result )
Lua
print( "Enter the first number:" )
num1 = io.read( "*n" )

print( "Enter the second number:" )
num2 = io.read( "*n" )

result = num1 + num2
print( "The result is:", result )
C++
cout << "Enter the first number: ";
float num1;
cin >> num1;

cout << "Enter the second number: ";
float num2;
cin >> num2;

float result = num1 + num2;
cout << "The result is: " << result << endl;

Author: Rachel Wil Sha Singh

Created: 2023-09-12 Tue 20:11

Validate