C++ 04: cin Functions
1) cin.ignore() :
The cin.ignore() function is used which is used to ignore or clear one or more characters from the input buffer.
To get the idea about ignore() is working, we have to see one problem, and its solution is found using the ignore() function. The problem is like below.
Sometimes we need to clear the unwanted buffer, so when next input is taken, it stores into the desired container, but not in the buffer of previous variable. For example, after entering into the cin statement, we need to input a character array or string. So we need to clear the input buffer, otherwise it will occupy the buffer of previous variable. By pressing the “Enter” key after the first input, as the buffer of previous variable has space to hold new data, the program skips the following input of container.
Write a program to read Date of birth of a person and display it.
#include <iostream>
using namespace std;
main(){ int D, M, Y; cout << "Enter your Date of birth (dd/mm/yyyy):"; cin >> D; cin.ignore(); cin >> M; cin.ignore(); cin >> Y;
cout << "DOB : " << D << "-" << M << "-" << Y << endl;}Output:
Enter your Date of birth (dd/mm/yyyy):14/2/2004
DOB : 14-2-20042) cin.getline(); :
The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered. While doing so the previously stored value in the string object str will be replaced by the input string if any.
The getline() function can be represented in two ways:
1.Write a program to read a name of a person and display it.
#include <iostream>using namespace std;main(){/* Write a Name of a preson and Display it */char name[20];cout << "Enter Your Name :";/*cin >> name;//this will not take whole name and will stop when there is space in input*/cin.getline(name, 20);cout << "Name = " << name << endl;}
Output:
Enter Your Name : Jack Fedrick Harris
Name = Jack Fedrick Harris
2. Write a program to read address of a person and print it.
#include <iostream>using namespace std;main(){/* Multi line address read */char add[100];cout << "Enter your address:(Type # to complete)" << endl;cin.getline(add, 99, '#');cout << "Address = " << add << endl;}
Output:
Enter your address:(Type # to complete) Google's Office 340 Main Street Los Angeles, CA 90291 United States. # Address = Google's Office 340 Main Street Los Angeles, CA 90291 United States.
3) cin.sync() :
What is a buffer?
A temporary storage area is called buffer. All standard input and output devices contain an input and output buffer. In standard C/C++, streams are buffered, for example in the case of standard input, when we press the key on keyboard, it isn’t sent to your program, rather it is buffered by operating system till the time is allotted to that program.
For e.g. the below program will produce error due to buffer.
#include <iostream>using namespace std;main(){// Program with Errorchar fn[50];int rn;char d[50];cout << "Enter fullname :";cin.getline(fn, 49);cout << "\nEnter roll no:";cin >> rn;cout << "Enter department: ";cin.getline(d, 49);cout << "Name:" << fn << endl;cout << "Roll No:" << rn << endl;cout << "Department:" << d << endl;}
Output:
Enter fullname :James Fedrick Harris Enter roll no:2302 Enter department: Name:James Fedrick Harris Roll No:2302 Department:
Solution of above problem:
Typing “cin.sync()” after the “cin” statement discards all that is left in buffer. Though “cin.sync()” does not work in all implementations (According to C++11 and above standards).
#include <iostream>using namespace std;main(){// Program with Solution Using sync() Function*************char fn[50];int rn;char d[50];cout << "Enter full name :";cin.sync();cin.getline(fn, 49);cout << "\nEnter roll no:";cin >> rn;cout << "Enter department: ";cin.sync();cin.getline(d, 49);cout << "Name:" << fn << endl;cout << "Roll No:" << rn << endl;cout << "Department:" << d << endl;}
Output:
Enter full name :James Fedrick Harris Enter roll no:2302 Enter department: Computer Science Name: James Fedrick Harris Roll No:2302 Department: Computer Science
Comments
Post a Comment