Click to See Complete Forum and Search --> : flushing input buffer using cin


kuhns_m
February 9th, 2003, 04:44 PM
I am having a problem if someone inputs a sentence with spaces in it.
Here is some code:

const int MAXNAME = 20; // maximum no. of characters in name
const int MAXADDR = 30; // maximum no. of characters in address

struct Node
{
char strName[MAXNAME];
char strAddress[MAXADDR];
int age;
Node* link;
};
typedef Node* Node_Ptr;


void main()
{

Node temp_ptr;
char choice;

cout << "Would you like to enter a record?(y/n)" << endl;
cin >> choice;

cin.clear();

cout << "Enter Name: (lastname,firstname)" << endl;
//cin >> temp_ptr.strName;
cin.getline(temp_ptr.strName, MAXNAME);

cout << "Enter Age: " << endl;
cin >> temp_ptr.age;

cout << "Enter Address: " << endl;
cin >> temp_ptr.strAddress;

return;
}

If I use cin >> temp_ptr.strName I only get the string to the first blank space. If I use cin.getline(temp_ptr.strName, 20); the execution doesn't stop. Like there's something in the buffer, but I clear the cin before I as for the name. The endl will flush the output buffer after the cout.
I know I could read the data in seperately(breaking at white spaces), but know I'm curious.
Thanks in advance

Philip Nicoletti
February 9th, 2003, 06:11 PM
cin.getline(temp_ptr.strName, MAXNAME);
cin.ignore(MAXNAME,'\n');

kuhns_m
February 9th, 2003, 06:36 PM
#include <iostream.h>
#include <stdlib.h>
#include <string.h>


const int MAXNAME = 20; // maximum no. of characters in name
const int MAXADDR = 30; // maximum no. of characters in address

struct Node
{
char strName[MAXNAME];
char strAddress[MAXADDR];
int age;
Node* link;
};
typedef Node* Node_Ptr;


void main()
{

Node temp_ptr;
char choice;
char *s;
cout << "Would you like to enter a record?(y/n)" << endl;
cin >> choice;

//cin.clear();

cout << "Enter Name: (lastname,firstname)" << endl;
cin.getline(temp_ptr.strName, MAXNAME);
cin.ignore(MAXNAME,'\n');
//cin >> temp_ptr.strName;
//cin.getline(temp_ptr.strName, MAXNAME);
//getline(cin, temp_ptr.strName);

cout << "Enter Age: " << endl;
cin >> temp_ptr.age;

cout << "Enter Address: " << endl;
cin >> temp_ptr.strAddress;

return;
}
This still didn't work for me. Weird.

Philip Nicoletti
February 9th, 2003, 06:58 PM
Put a larger number in the ignore() line :


#include <limits>

//
//
// etc
//
cin.ignore(std::numeric_limits<int>::max(),'\n');


(and you might want to you the standard header
<iostream> , instead of the non-standard <iostream.h>)

kuhns_m
February 9th, 2003, 07:07 PM
i used
cin.ignore(9999999,'\n');
is that big enough?
I am using Visual C++ 6.0.
This is a nightmare

Philip Nicoletti
February 9th, 2003, 09:07 PM
Is it working now? It works for under ubder VC++ 5.

I don't know if there is an "easier way". I rarely do
terminal i/o ... pretty much exclusive file i/o (or GUI).

Also, I assume that you are going to write some type of link list.
Why not use std::list ?

doumalc++
February 11th, 2003, 02:31 AM
Is there any better way to clear the buffer than using cin.ignore()?
I am finding cin.ignore to be very rudimentary.

I mean, what if the user goes into an input spree, what is the programmer to do to handle such issue?

Plus, there might be more than a return character in the buffer.

whoke
February 12th, 2003, 12:04 AM
add
cin.seekg(0,ios::end);
between
cin >> choice and cin.getline()

indiocolifa
February 13th, 2003, 07:18 AM
I'm stuck with the same problem...

(tried the same code in VC 6.0, VC 7.0, DJGPP GNU C++, MinGW !!!!!)

What is the problem with cin.getline???
Seems that cin "remembers" the old buffer, and doesnt wait for user input.
Also tried char* and string data types.

Can I get easier way for string input (no scanf like NOT C, but c++)

thanks!

whoke
February 13th, 2003, 11:18 PM
This is my code:

#include <iostream>
#include <cstdlib>
#include <cstring>


const int MAXNAME = 20; // maximum no. of characters in name
const int MAXADDR = 30; // maximum no. of characters in address

struct Node
{
char strName[MAXNAME];
char strAddress[MAXADDR];
int age;
Node* link;
};
typedef Node* Node_Ptr;

using namespace std;

int main()
{

Node temp_ptr;
char choice;
char *s;
cout << "Would you like to enter a record?(y/n)" << endl;
cin >> choice;
cin.seekg(0,ios::end); //throw other input
cin.clear(); //important

cout << "Enter Name: (lastname,firstname)" << endl;
cin.getline(temp_ptr.strName, MAXNAME);

cout << "Enter Age: " << endl;
cin >> temp_ptr.age;

cout << "Enter Address: " << endl;
cin >> temp_ptr.strAddress;
cout<<temp_ptr.strName <<endl;
cout<<temp_ptr.age <<endl;
cout<<temp_ptr.strAddress <<endl;
system("pause");
return 0;
}

cin>>choice will set cin status error, so any functions with cin executed error.so add line cin.clear();