getline() and write() functions of stream I/O – CSKC
Sign In

Search

Code Learning #CPP#2175    siteicon   siteicon  

getline() and write() functions of stream I/O

Use of functions getline() and write of cin and cout steam I/O objects.

Learning Objectives

  • Use of getline() to collect the whole line irrespective of spaces and other whitespaces.
  • Use of write() to output data to the given stream which is cout stream in this example.

Source Code

TC++ #2175

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

char s[100];
Here we declare a char array where we can collect the sentence entered by the users with a maximum limit of 100 characters. It can only be just 99 characters as the termination of a well formed string has to be done with a null () character as per rules for c-style strings.

cout<<“Enter a sentence :”;
Here we prompt for collecting the input.

cin.getline(s,100);
Here we use the getline method of cin object. using a method of an object we separate it with the dot(.) operator. Within round braces we give parameters as the char array name where the collected data would be stored and upto what maximum limit it can be stored.

cout<<“You entered “;
Here we display some message which is a part of message which is forthcoming.

int i;
The index of loop we declare outside the loop here as this index has to be used after the loop is finished. If we declare it inside as we usually do, we will not be able to use this value outside the loop because of scope limitations.

for(i=0;s[i]!=”;++i);
This is an empty loop without any instructions. This simply helps to move the index till the null () is reached. This helps to count the length of the data entered by the user. We can use string length finding functions also, but this is the raw technique.

cout<<i<<” characters. They are as follows.”<<endl;
This prints the number of characters enters along with the full message.

cout.write(s,i);
This is a way to output the content in s till the limit of number of character which is given by i in this example. This function is a method existent with the cout object which is found in iostream header.

 

Notes

  • getline() function format given here applies to c-style strings which are null terminated strings implemented by char arrays. An alternate form is used by string type data in c++ which is for the form getline(cin,str). This form shall be discussed in the topic of String Handling – string type.

Common Errors

  • Students often confuse between two types of getline as explained in the code-notes here. Practice and refer properly when you use.
  • While making length finder loop it is quite common to declare the int i=0; inside the loop. This has to be avoided as i declaration will not available outside the scope of for unless it is done before the for loop.
  • The loop given in this example is a sort of empty loop without any instructions, so remember to put semicolon (;) after the loop. In most cases this semicolon is not there as usually it has to put only after the instructions in the loop body.


Suggested Filename(s): getline-write.cpp



Share

Created: 4-Jan-2018 23:06 | Updated: 4-Jan-2018 23:24|