🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C++ Notes
Topic #35

User Input Strings


User Input Strings

It is possible to use the extraction operator >> on cin to store a string entered by a user:

Example

  string firstName;
cout << "Type your first name: ";
cin >>
  firstName;
  // get user input from the keyboard
cout << "Your name is: " <<
  firstName;

// Type your first name: John
// Your name is: John

However, cin considers a space (whitespace, tabs, etc) as a terminating character, which means that it can only store a single word (even if you type many words):

Example

  string fullName;
cout << "Type your full name: ";
cin >>
  fullName;
cout << "Your name is: " <<
  fullName;

// Type your full name: John Doe
// Your name is: John

From the example above, you would expect the program to print "John Doe", but it only prints "John". That's why, when working with strings, we often use the getline() function to read a line of text. It takes cin as the first parameter, and the string variable as second:

Example

  string fullName;
cout << "Type your full name: ";
  getline (cin, fullName);
cout << "Your name is: " <<
  fullName;

// Type your full name: John Doe
// Your name is: John Doe

Want to go beyond the notes?

Join CodingNow 2.0's C++ course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

User Input Strings – FAQs

Quick answers about learning User Input Strings in C++.

This free note from CodingNow 2.0 explains User Input Strings in C++ — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C++ topic on CodingNow 2.0, including User Input Strings, is 100% free with no signup required.
With focused practice, most students grasp User Input Strings in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now