Input Handling in C++
In our previous lessons, we learned how to display text using cout. Now, it’s time to make your programs interactive! Instead of just talking to the user, your program will now listen.
Simple Definitions
Before we dive into the code, let’s define our new tools:
Topic |
Simple Definition |
| cin | A tool (stream object) used to take information from the user’s keyboard and put it into your program. |
| iostream | The library (toolbox) that contains cin. You must include this at the top of your code to use input or output. |
| Extraction Operator (>>) | The “arrows” that point away from cin, showing that data is being extracted from the input stream and moved into a variable. |
| Variable | A named storage “container” that must be created before you can store user input in it. |
| Programmer (You) | The person who writes the code of the program. In this case you are the programmer |
| User | The person who uses the program once it is successfully compiled and executed. |
Explanation of cin
-
Why do we need cin?
While cout is for printing to the screen, cin is for taking values into the program. Imagine a calculator: you can’t just hardcode numbers; you need to ask the user, “What is the first number? What operation is to be performed? And what is the second number and so on”. cin allows your program to handle real-world data like student marks or names.
-
The Step-by-Step Process
To take input successfully, follow this logic:
- Include the Library: Ensure #include <iostream> is at the top.
- Declare a Variable: To store the value given by the user as input. (e.g., int marks;).
- Capture Input: Use cin >> marks; to store the typed value.
-
Pro Tips:
- Prompt the User: Use cout to tell the user what to do (e.g., “Please enter your marks”).
- Use of “namspace std”: Always add this line before the main function, “using namespace std” to avoid repetitive use of “std::” before cin.
-
Handling Multiple Inputs
You aren’t limited to one piece of info at a time. You can take multiple inputs in two ways:
- Separate Lines:
Chaining (The Pro Way):
Use a single cin with multiple extraction operators: cin >> math >> english >> science;.
-
The Golden Rule: Data Types Matter
The value the user types must match the variable type you defined.
- If you define an int (integer) but the user types “ABC”, the program will error or behave unexpectedly because “ABC” is a string, not a number.
- Character Limit: A char variable can only hold one alphabet or symbol. If a user types “AB” into a char variable, it can only store the ‘A’.
# include <iostream>//Mendatory header file for input and output operations using namespace std;// recommended int main(){ int num1 = 0, num2 = 0;// variable declaration to store input values. double percentage = 0; char grade; cout<<"Please enter two numbers: "; cin>>num1>>num2; // stores the first value in num1 and second value in num2 respectively cout<<"Please enter your percentage: "; cin>>percentage; cout<<"Please enter your grade: "; cin>>grade; // Showing result! cout<<"The sum of two numbers you entered is: "<<num1+num2; cout<<"Keep working hard, you obtained: "<<percentage<<" marks"; cout<<"Your grade is: "<<grade; return 0; }
Interactive “Try It Yourself” Challenges
- The Greeting Bot: Create a program that asks for a user’s favorite number (integer) and their favorite initial (character), then prints them back in a single sentence.
- The Sum Challenge: Create a program that asks for three different subject marks in one line using chaining (cin >> a >> b >> c;) and then displays their total sum.
Point to Ponder: Why do you think it is important to use a cout message before a cin statement? What would the user see if you forgot the message?.
Pro-Tip: For more advanced tips, check out the Official C++ Documentation.
