Thread: Question about cin.getline

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    10

    Question about cin.getline

    Hi
    I was wondering how to prevent someone from typing in more than the set number of characters in an array...i have been using:

    cin.getline (name, 50)

    but it enters a continious loop if more than 50 chars are entered, just wodndering if theres a way around this?

    thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    There is.
    Post your code that is looping infinately when over 50 chars are entered.

    gg

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The easy way is to use getline(cin,name) where name is a std::string. If, for some reason the first 49 characters are the only ones that are significant you can use resize(). If you must make life difficult for yourself
    Code:
    // Leave newline in stream, regardless of
    // success, then use ignore to discard 
    // any extra(plus the newline).
    cin.get(name,50);
    cin.ignore(numeric_limits<int>.max(),'\n');
    
    
    // Check the status of failbit, if true discard
    // any characters in the buffer up to and 
    // including newline
    if(!cin.getline(name,50)) {
        cin.clear();
        cin.ignore(numeric_limits<int>().max(),'\n');
    }
    The first version is simpler, but doesn't give you a chance to perform any extra processing if the line was too long for your buffer. With the second you have a chance to react to the excessive input in a more constructive manner.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    Hi
    Thanks for replies...have been playing around with:
    cin.ignore(numeric_limits<int>.max(),'\n');
    but cant get it to work.......my code is below....the original problem still is there, where if you enter over 10 chars it enters an infite loop...
    it gives 'correct' when Bart and Fred are entered but when John and Steve are entered it gives correct, but then wrong...

    really new to c++ so theyre probably newb errors =/

    thanks

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    	
    	char name [4][10] =	{"John","Steve","Bart","Fred"};
    	char getname [10];
    	bool correct = false;
    
       do
    	{
    		cout << "name:"; 
    		cin.getline(getname,10,'\n');
    		
    
    		for(int a = 0; a < 4; a++) 
    		{
    			int bname = strcmp (getname, name[a]);
    
    			if (bname == 1 && a == 3)
    			{
    				cout << "wrong";
    				cout << endl;
    				correct = false;
    			}
    
    
    			if (bname == 0)
    			{
    				cout << "correct";
    				cout << endl;
    				cout << endl;
    				correct = true;
    			}
    		}
    	}while (!correct);
    
    	return 0;
    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    When getline fills the buffer (ie. it doesn't find the target character), it sets an error flag that must be cleared before anything else can be done with the stream. You can clear the flags with cin.clear(), then remove the extraneous characters with cin.ignore():
    Code:
    #include <iostream>
    #include <limits>
    using namespace std;
    
    int main()
    {
      char name [4][10] = {"John","Steve","Bart","Fred"};
      char getname [10];
      bool correct = false;
      
      do
      {
        cout << "name:"; 
        cin.getline(getname,10,'\n');
        if (!cin.good())
        {
          cin.clear();
          cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        
        for(int a = 0; a < 4; a++) 
        {
          int bname = strcmp (getname, name[a]);
          
          if (bname == 1 && a == 3)
          {
            cout << "wrong";
            cout << endl;
            correct = false;
          }
          
          
          if (bname == 0)
          {
            cout << "correct";
            cout << endl;
            cout << endl;
            correct = true;
          }
        }
      }while (!correct);
      
      return 0;
    }
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    strcmp(a,b) returns -1 if a<b and 1 iff a>b You are only checking if a > b and then only setting correct to false if the name you entered is greater than "Fred" . You want to compare getname to each name in your list and stop when you find a match. thus:
    Code:
    correct = false;
    for(int i=0;i<4 && !correct;++i) correct = strcmp(getname,name[i]))==0;

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    thanks everyone...really appreciated
    sorted

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM