|
-
February 9th, 2003, 05:44 PM
#1
flushing input buffer using cin
I am having a problem if someone inputs a sentence with spaces in it.
Here is some code:
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
-
February 9th, 2003, 07:11 PM
#2
Code:
cin.getline(temp_ptr.strName, MAXNAME);
cin.ignore(MAXNAME,'\n');
-
February 9th, 2003, 07:36 PM
#3
#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.
-
February 9th, 2003, 07:58 PM
#4
Put a larger number in the ignore() line :
Code:
#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>)
-
February 9th, 2003, 08:07 PM
#5
i used
cin.ignore(9999999,'\n');
is that big enough?
I am using Visual C++ 6.0.
This is a nightmare
-
February 9th, 2003, 10:07 PM
#6
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 ?
Last edited by Philip Nicoletti; February 9th, 2003 at 10:50 PM.
-
February 11th, 2003, 03:31 AM
#7
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.
-
February 12th, 2003, 01:04 AM
#8
flushing input buffer using cin
add
cin.seekg(0,ios::end);
between
cin >> choice and cin.getline()
whoke
-
February 13th, 2003, 08:18 AM
#9
same problem
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!
-
February 14th, 2003, 12:18 AM
#10
did you try it?
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();
whoke
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|