Varible 1

User Input in C++

You can also take user input at runtime, which is not difficult at all. For this, you can use cin stream, which stands for console input.

Example 1:

#include  
using namespace std;

int main() {
    int number;
    char ch;

    cout << "Enter a number: ";
    cin >> number;
    cout << "Enter a character: ";
    cin >> ch;
    cout << "number = " << number << " character = " << ch;
    return 0; 
}

Input No 1:

12

Input No 2:

d

Output:

number = 12 character = d

Explanation:

  • We have used >> operator after cin, which is called the input extraction operator.
  • This operator is used to take input from the keyboard using the cin stream.
  • When you execute this program, the system will pause after printing the first line.
  • As soon as you enter a number, it will be assigned to the variable, and then the system will ask for the second input.
  • Finally, the result will be displayed.

Multiple Inputs in One Line:

You can also take multiple inputs in a single line, like this:

char ch, ch1;
cin >> ch >> ch1;

Problem with example 1:

However, in reality, if you run the program in example 1, it may not wait for the character input and produce unexpected results.

Why Does This Happen?

When you input 5 and press Enter, the input stream stores:

5\n

which means the number 5 is assigned to number but the newline character (\n) remains in the input buffer.

Now, when cin >> ch; executes, instead of waiting for a character input from the user, It may reads the leftover newline (\n) from the buffer. As a result, ch is assigned \n, and the program may not wait for user input.

How to Fix it?

Add cin.ignore() before reading ch to clear the buffer:

cin.ignore();
cin >> ch;

String Input in C++

Taking string input using cin has a limitation.

  • cin is not ideal for taking multi-word strings because it considers spaces (' ') as input separators.
  • This means if you try to enter a sentence with spaces, only the first word will be stored.

Solution 1: Using Array of characters:

To take multi-word string input, you can use an array of characters.

#include 
using namespace std;

int main()
{
    char name[50];
    cout << "Enter a string: ";
    cin.get(name, 50);
    cout << "You entered: " << name << endl;
    return 0;
}

Input:

My name is Rameen

Output:

My name is Rameen
  • This code declares a character array to store a string (max 50 characters)
  • Here, name is a variable that will store the full string input, including spaces.
  • Finally, the entered name will be printed on the screen.

Solution 2: Using getline() (Better Approach)

Instead of using character arrays (char[]), you can use C++’s string type with getline():

#include 
#include 
using namespace std;

int main()
{
    string name;
    cout << "Enter a string: ";
    getline(cin, name);  // Reads a full line of input including spaces
    cout << "You entered: " << name << endl;
    return 0;
}

Input:

My name is Rameen

Output:

My name is Rameen

Advantages of getline() over cin.get()

  • Easier to use
  • It is safer because it works with string, so there’s no need to manually allocate a fixed array size (char[50] has a limit). As a result, it avoids buffer overflows.

Note: If getline() follows a cin >> statement, add cin.ignore() to clear the buffer before calling getline() as we did in the example 1.

No Comments

Leave a Reply