User Input in the Terminal

Learn how to get user input from the terminal, allowing our program to respond to use requests
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

3D art showing a character in a fantasy environment
Ryan McCombe
Ryan McCombe
Posted

So far, our programs have been completely non-interactive. Lets see how we can get input from our users, letting them control how our software runs.

Just like std::cout is a stream of output, std::cin is a stream of input from our user.

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.

There are 3 components to this - first, we need to #include <string> from the standard library:

#include <string>

int main () {}

Then, in one of our functions, we need to create a std::string to store our user input:

#include <string>

int main () {
  using namespace std;
  string UserInput;
}

Finally, we call std::getline, passing in the std::cin stream, and the string we created to store their input:

#include <iostream>
#include <string>

int main () {
  using namespace std;
  string UserInput;
  
  cout << "Enter some text: ";
  getline(cin, str);
  cout << "You entered: " << str;
}

Now, our program will pause at the 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!

Lets see a bigger example of this. The following code lets our user pick what type of Character they want to play as:

#include <iostream>
#include <string>

using namespace std;

class Character {
public:
  Character(string ClassName)
	: ClassName { ClassName } {
  }
  void SetName(string NewName) {
	Name = NewName;
  }
  string Name;
  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") {
  }
};

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

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

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

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

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

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

  PlayerCharacter->SetName(SelectName());

  cout << endl
	   << 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: Stabby

Stabby the Rogue is entering the arena!

Was this lesson useful?

Ryan McCombe
Ryan McCombe
Posted
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

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:

  • 66 Lessons
  • Over 200 Quiz Questions
  • Capstone Project
  • Regularly Updated
  • Help and FAQ
Next Lesson

C++ Arrays using std::vector

An introduction to how we can store collections of objects into a single container, using the std::vector class
3D art showing a character in a fantasy environment
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved