std::cin
and std::getline
So far, our programs have been completely non-interactive. Let's see how we can get input from our users, letting them control how our software runs.
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:
std::cin
for this.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!
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!
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.
In our next lesson, we will delve into the world of arrays using std::vector
. The key topics we’ll cover include:
std::vector
: Introducing std::vector
as an implementation of a dynamic array, and the difference between dynamic and static arrays.std::vector
: Learn how to declare std::vector
, initialize it with values, and understand its dynamic nature.std::vector
, including using the subscript operator.std::vector
: How to iterate through std::vector
using various methods, including range-based for loops.std::vector
.— Refreshed Content
— First Published
Cppreference - std::cin, std::wcin
https://en.cppreference.com/w/cpp/io/cin
Cppreference - std::getline
https://en.cppreference.com/w/cpp/string/basic_string/getline
Cppreference - Standard library header <iostream>
https://en.cppreference.com/w/cpp/header/iostream
cplusplus - Basic Input/Output - cplusplus.com
http://www.cplusplus.com/doc/tutorial/basic_io/
This lesson introduces the fundamentals of capturing user input, using std::cin
and std::getline
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way