User input to string class based strings – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#3023 siteicon   siteicon   siteicon  

User input to string class based strings

Collecting user input to string class string objects using cin and getline()

Learning Objectives

  • Learning to collect whole sentences into the string class based objects.
  • Learning why cin fails and getline(cin,<string object>) works.

Source Code

TC++ #3023

Source Code

  Related (?) :

Run Output

Enter a sentence : Great people think alike.
Enter another sentence : An apple a day keeps the doctor away.
An apple a day keeps the doctor away.

Code Understanding

string s; //object s of string class declared

cout<<“Enter a sentence : “; cin>>s;
Collecting a sentence into string object using cin. It will collect first word only due to limitation of cin

cout<<s<<endl; //This will print the first word only of given sentence

This will help to ignore the last newline (n) entered after the cin input so that next getline function can operate well. Without this you won’t be able to collect next input. 250 is an arbitray long value assuming that previously given cin input is less than 250 chars.

cout<<“Enter another sentence : “;  //Prompt for next sentence

This will collect the whole sentence from the console and put it into the s the string class object declared earlier.

This will print the newly updated value of s.



  • getline(cin,<string object> of string class is different from cin.getline(<char array>,<size>) used for null terminated string. The getline method of string class doesn’t need any size parameter as it can collect an arbitrarily long string.
  • cin.ignore needs to be carfully used for ignoring long range of white spaces after the expected input. The ideal method of cin.ignore is as follows.
    cin.ignore(std::numeric_limits<std::streamsize>::max(), ‘n’);
    We have not given it here as it is too verbose and is difficult to comprehend for many students.

Common Errors

  • Student often confuse between cin.getline(<char array>,<size>) and getline(cin,<string object>) regarding where what is used. The best way to remember is that one with string class doesn’t need a size parameter.

Suggested Filename(s): strclip.cpp


sunmitra| Created: 25-Jan-2018 | Updated: 25-Jan-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues