Accepting user input is a fundamental aspect of interactive programming. In C++, you can use the cin
object, which stands for “character input,” to receive input from the user. Let’s explore how to do this with some examples.
Basic User Input
To accept basic user input, you use the std::cin
object along with the extraction operator (>>
). Here’s a simple example:
Example: Accepting an Integer Input
#include <iostream>
int main() {
int age; // Declare an integer variable to store user input
std::cout << "Enter your age: "; // Prompt the user
std::cin >> age; // Accept user input and store it in the variable 'age'
std::cout << "You entered: " << age << std::endl; // Display the input
return 0;
}
In this example:
std::cout << "Enter your age: ";
displays a message asking the user to enter their age.std::cin >> age;
waits for the user to input a value and stores it in theage
variable.std::cout << "You entered: " << age << std::endl;
prints the entered age.
Accepting Multiple Inputs
You can also accept multiple inputs from the user in a single line.
Example: Accepting Two Integers
#include <iostream>
int main() {
int num1, num2;
std::cout << "Enter two numbers separated by a space: ";
std::cin >> num1 >> num2;
std::cout << "You entered: " << num1 << " and " << num2 << std::endl;
return 0;
}
In this example:
std::cin >> num1 >> num2;
reads two integers from the user input, separated by spaces, and stores them innum1
andnum2
.
Accepting String Input
For string inputs, you can use the std::cin
object for single words or std::getline
for entire lines.
Example: Accepting a Single Word
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
Example: Accepting an Entire Line
#include <iostream>
#include <string>
int main() {
std::string fullName;
std::cout << "Enter your full name: ";
std::getline(std::cin, fullName);
std::cout << "Hello, " << fullName << "!" << std::endl;
return 0;
}
In this example:
std::getline(std::cin, fullName);
reads an entire line of input, including spaces, and stores it in thefullName
variable.
Handling Different Data Types
You can handle various data types by declaring variables of the appropriate type and using std::cin
accordingly.
Example: Accepting Different Data Types
#include <iostream>
int main() {
int age;
float height;
char grade;
std::string name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Enter your height: ";
std::cin >> height;
std::cout << "Enter your grade: ";
std::cin >> grade;
std::cout << "Enter your name: ";
std::cin.ignore(); // Clear the newline character left in the input buffer
std::getline(std::cin, name);
std::cout << "You entered:\n";
std::cout << "Age: " << age << "\n";
std::cout << "Height: " << height << "\n";
std::cout << "Grade: " << grade << "\n";
std::cout << "Name: " << name << "\n";
return 0;
}
In this example:
std::cin.ignore();
clears the newline character left in the input buffer after readinggrade
, ensuringstd::getline
works correctly.
Conclusion
Accepting user input in C++ is straightforward using std::cin
and std::getline
. These tools allow you to read various types of data from the user, making your programs interactive and versatile. Experiment with different types of input to become more comfortable with these techniques. Happy coding!