C++ User Input
User input is an essential part of many programs, allowing the user to provide data to the program at runtime. In C++, user input is typically handled using the std::cin
object. This chapter will guide you through the basics of receiving user input in C++ and provide examples to illustrate different scenarios.
1. Basic User Input
To receive user input, you use the std::cin
object along with the extraction operator >>
. Here’s an example:
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old." << std::endl;
return 0;
}
Explanation:
#include <iostream>
: Includes the input-output stream library needed forstd::cin
andstd::cout
.std::cout << "Enter your age: ";
: Prompts the user to enter their age.std::cin >> age;
: Reads the user input and stores it in the variableage
.std::cout << "You are " << age << " years old." << std::endl;
: Outputs the age entered by the user.
2. Receiving Different Data Types
You can receive input of various data types using std::cin
. Here are some examples:
2.1. Integer Input
#include <iostream>
int main() {
int number;
std::cout << "Enter an integer: ";
std::cin >> number;
std::cout << "You entered: " << number << std::endl;
return 0;
}
2.2. Floating-Point Input
#include <iostream>
int main() {
float number;
std::cout << "Enter a floating-point number: ";
std::cin >> number;
std::cout << "You entered: " << number << std::endl;
return 0;
}
2.3. Character Input
#include <iostream>
int main() {
char letter;
std::cout << "Enter a character: ";
std::cin >> letter;
std::cout << "You entered: " << letter << std::endl;
return 0;
}
2.4. String Input
For string input, you can use the std::cin
object, but it only reads a single word by default. To read a full line of text, you can use std::getline
.
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello, " << name << "!" << std::endl;
std::cin.ignore(); // Ignore remaining newline character
std::cout << "Enter your full name: ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
3. Handling Multiple Inputs
You can handle multiple inputs by chaining std::cin
operations.
#include <iostream>
int main() {
int day, month, year;
std::cout << "Enter your birthdate (day month year): ";
std::cin >> day >> month >> year;
std::cout << "Your birthdate is: " << day << "/" << month << "/" << year << std::endl;
return 0;
}
4. Input Validation
It’s important to validate user input to ensure that your program behaves correctly. Here is a simple example of input validation:
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
while (!(std::cin >> age)) {
std::cin.clear(); // Clear the error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore invalid input
std::cout << "Invalid input. Please enter a valid age: ";
}
std::cout << "You are " << age << " years old." << std::endl;
return 0;
}
Conclusion
This chapter covered the basics of handling user input in C++. By using std::cin
and std::getline
, you can read input from the user and process it in your programs. Additionally, input validation helps ensure that your programs run smoothly and handle unexpected input gracefully. Practice these techniques to become proficient in managing user input in C++.