Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Truncate the char and clean the buffer.

Status
Not open for further replies.

swaroop

Programmer
Feb 4, 2001
100
US
How can I use ignore() and truncate to take 20 char from user input in a name[40] variable.....

When I try to use

cin.get(name,20,'\n');
cin.getline(name,20,'\n');


cin.get(name,20,'\n').get();
cin.getline(name,20,'\n').get();

cin.get(name,20,'\n').ignore(20);
cin.getline(name,20,'\n').ignore(20);

It is not cleaning buffer in while loop.

Thanks in advance
 
Hi,
Do like the following, it will solv your problem.

#include <iostream.h>
int main()
{
char name[100];
while(1) // To terminate the loop press Ctrl C
{
cin.get(name, 20, '\r');
cout << name << endl;
cin.ignore(1000, '\n');
}
return 0;
}

Regards,
Maniraja S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top