CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    41

    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

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Code:
    cin.getline(temp_ptr.strName, MAXNAME);
    cin.ignore(MAXNAME,'\n');

  3. #3
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    41
    #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.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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>)

  5. #5
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    41
    i used
    cin.ignore(9999999,'\n');
    is that big enough?
    I am using Visual C++ 6.0.
    This is a nightmare

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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.

  7. #7
    Join Date
    Feb 2003
    Posts
    53
    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.

  8. #8
    Join Date
    Feb 2003
    Location
    china
    Posts
    2

    flushing input buffer using cin

    add
    cin.seekg(0,ios::end);
    between
    cin >> choice and cin.getline()
    whoke

  9. #9
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615

    Angry 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!

  10. #10
    Join Date
    Feb 2003
    Location
    china
    Posts
    2

    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
  •  





Click Here to Expand Forum to Full Width

Featured