User Input in the Terminal

This lesson introduces the fundamentals of capturing user input, using std::cin and std::getline
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, Unlimited Access
Abstract art representing computer programming
Ryan McCombe
Ryan McCombe
Updated

Up until now, our C++ programs have been running on their own, without any interaction from users. But what if we want to create more dynamic and responsive software?

In this lesson, we'll explore how to capture user input, allowing our programs to adapt and respond to the choices made by those using them.

This is a crucial step in creating interactive applications, from simple command-line tools to complex games and utilities.

Using std::cin and std::getline

Just like std::cout is a stream of output, std::cin is a stream of input from our user. Just like std::cout, it is also available within the <iostream> header

There are numerous ways to interact with std::cin. The most straightforward is using std::getline to get a line of text from our users.

std::getline is part of <string> and accepts two arguments:

  • The input stream, representing where the data is coming from. To let the user input text in the terminal, we’d use std::cin for this.
  • A reference to a std::string, which will be populated with what the user entered.

Below, we show this in action:

#include <iostream>
#include <string>

int main() {
  std::string UserInput;

  std::cout << "Enter some text: ";
  std::getline(std::cin, UserInput);
  std::cout << "You entered: " << UserInput;
}

Now, our program will pause at the std::getline call, and wait for user input. Typing something on our keyboard and hitting return will yield output like the following:

Enter some text: Hello!
You entered: Hello!

A Practical Example

Let's see a bigger example of this. The following code lets our users pick what type of Character they want to play as:

#include <iostream>
#include <string>

class Character {
public:
  Character(const std::string& ClassName)
	: ClassName { ClassName } {
  }
  void SetName(const std::string& NewName) {
	Name = NewName;
  }
  std::string Name;
  std::string ClassName;
};

class Warrior : public Character {
public:
  Warrior() : Character("Warrior") {}
};

class Rogue : public Character {
public:
  Rogue() : Character("Rogue") {}
};

class Wizard : public Character {
public:
  Wizard() : Character("Wizard") {}
};

std::string SelectClass() {
  std::cout << "What class do you want to play?"
    << "\n- Enter 1 for Warrior"
    << "\n- Enter 2 for Rogue"
    << "\n- Enter 3 for Wizard"
    << "\n\nMake your selection: ";

  std::string ClassString;
  std::getline(std::cin, ClassString);
  return ClassString;
}

std::string SelectName() {
  std::cout << "Enter your name: ";

  std::string Name;
  std::getline(std::cin, Name);
  return Name;
}

int main() {
  Character* PlayerCharacter;
  Warrior PlayerWarrior;
  Rogue PlayerRogue;
  Wizard PlayerWizard;

  std::string SelectedClass{SelectClass()};
  if (SelectedClass == "1") {
	  PlayerCharacter = &PlayerWarrior;
  } else if (SelectedClass == "2") {
	  PlayerCharacter = &PlayerRogue;
  } else {
	  PlayerCharacter = &PlayerWizard;
  }

  PlayerCharacter->SetName(SelectName());

  std::cout
    << PlayerCharacter->Name << " the "
    << PlayerCharacter->ClassName
    << " is entering the arena!";
}

After running this code, and providing the requested input, we might see something like this:

What class do you want to play?
- Enter 1 for Warrior
- Enter 2 for Rogue
- Enter 3 for Wizard

Make your selection: 2
Enter your name: Grifter
Grifter the Rogue is entering the arena!

Summary

In this lesson, we explored the basics of obtaining user input in C++ using std::cin and std::getline. Through practical examples, we learned how to make our programs interactive and responsive to user commands.

Preview of the Next Lesson

In the upcoming lesson, we'll delve into bitwise operators and bit flags. This topic involves manipulating data at the binary level, which is essential for certain types of low-level programming, optimization, and handling of compact data structures. We’ll cover:

  • Binary Basics and Object Representation: Gain an understanding of binary numbers and how data is represented as a series of binary digits, or bits.
  • Understanding Bitwise Operators: Learn about bitwise AND, OR, XOR, NOT, and how they are used to manipulate individual bits.
  • Bit Flags and Bit Masks: Explore how to use bitwise operators to create and manipulate sets of options, often known as bit flags.
  • Shifting Bits: Understand the use of left and right shift operators and their impact on data.
  • Practical Applications: See real-world examples of how bitwise operations are used in areas like graphics programming, system programming, and optimizing memory usage.

Was this lesson useful?

Next Lesson

Bitwise Operators and Bit Flags

Unravel the fundamentals of bitwise operators and bit flags in this practical lesson
3D Character Concept Art
Ryan McCombe
Ryan McCombe
Updated
Lesson Contents

User Input in the Terminal

This lesson introduces the fundamentals of capturing user input, using std::cin and std::getline

3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, Unlimited Access
Odds and Ends
3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 57 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Next Lesson

Bitwise Operators and Bit Flags

Unravel the fundamentals of bitwise operators and bit flags in this practical lesson
3D Character Concept Art
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved