Detecting two consecutive spaces in string

This is a discussion on Detecting two consecutive spaces in string within the C++ Programming forums, part of the General Programming Boards category; Define 'doesn't work'....

  1. #16
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Define 'doesn't work'.

  2. #17
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    When I run the program and type in "Test Test" -- which has two whitespaces in the middle -- it ignores and goes to next function. What should happen is the loop catches that there is two consecutive whitespaces in the string and prompt the end user to re-enter data.

  3. #18
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Oh, my mistake. If you need to prevent two spaces then -

    Code:
    cout << endl << endl << " Please enter a valid item name: ";
        getline(cin, itemName,'\n');		
    	while (itemName.find("  ")!=itemName.npos)
    	{
    		cout << endl << endl << " Please enter a valid item name: ";
    		getline(cin, itemName,'\n');			
    	}

  4. #19
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Thanks. I appreciate it.

  5. #20
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Still having a problem with getline. I think it's because cin enters the fail state for some reason. Below is function and output. Any help would be greatly appreciated.

    --------------------

    Code:
     
     void PrintRecordName()
     {
      // Precondition:
      // Postcondition:
     
      string itemName;
     
       cout << endl << " You entered option 2" << endl << endl;
       system("pause");
       system("cls");
       cout << endl << " Note: You may have to hit enter twice " << endl;
       cout << endl << " Please enter item name: "; 
       getline(cin, itemName,'\n');
       
       while (itemName.find("  ") != itemName.npos)
       {
        cout << endl << endl << " Please enter a valid item name: ";
        getline(cin, itemName,'\n');   
       }
     
       cout << endl << endl << " You entered: " << itemName << endl << endl;
       system("pause");
       system("cls");
     
       //GetFile();
     
       //system("pause");
     
      return;
     } // ends PrintRecordName function
    ----------------------

    Here's the output I get:

    -----------------------

    Note: You may have to hit enter twice

    Please enter item name: testing testing

    Please enter a valid item name: testing testing

    You entered:

    Press any key to continue . . .

    ---------------------

    I don't understand why the while loop is breaking. The only thing I think of is that cin is in the fail state. I've tried cin.clear() before prompting in the while loop but this also doesn't work.

    Out of answers

  6. #21
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Well, I figured out what the problem is: there is a bug in the VC++ 6.0 version of the header files defining the getline function. Therefore, the code above should work, but it doesn't in Microsoft's VC++ 6.0.



    Anybody know of an alternative method for reading a line of characters as a string. Maybe through a loop. I'll try and get back.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21