Thread: Detecting two consecutive spaces in string

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    47

    Detecting two consecutive spaces in string

    Greetings All,

    My problem is explained exactly as in the thread title: Detecting two consecutive spaces in string. I'm trying to perform some error detection and I need to detect to consecutive spaces. This is what I tried but to no avail.

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

    Code:
     
    
    cout << endl << " Please enter item name: "; 
    getline(cin, itemName,'\n');
    NewItemLength = itemName.length();
    loopCount2 = 0;
    			
    while (loopCount2 > 2)
    {
         cin.get(Letter);
         if (Letter == ' ') 
         loopCount2++;
         cout << loopCount2;
    }
    ----------------------

    The cout loopCount2 is just to show if any whitespaces were picked up. Please help.

    Thanks,
    bob2509
    Last edited by bob2509; 04-20-2002 at 11:47 AM.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    while (loopCount2 > 2)

    This should be

    while (loopCount2 < 2)

    You initilized loopCount2 as 0, then looped while it was more than two. (never)

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Oh, no. Sorry.

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    even easier:
    itemName.find(" ",0)

    if this returns true, there are more than two spaces. it returns the position of that place. if not, it returns string::npos.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Two consecutive whitespaces? That's what I'm looking for.

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    if (Letter == ' ')

    Would better serve its purpose as

    if (isspace(Letter))

    Remember to #include <ctype.h>

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
    
    	string a = "aaa  zz";
    	cout << a.find("  ");
    
    
    	return 0;
    }
    Will send the position of the " " (two consecutive whitespaces mangled by the board) to the console.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    I tried to do the following but it didn't work:

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

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    should'nt it be

    cin.getline(whatever....);

  10. #10
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    std::string::find() doesn't return a bool. If it finds the parameter, it returns the position otherwise it returns std::string::npos. Try -

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

  11. #11
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    pode: getline works with string classes

    sorenson: ditto

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    classes???!!

    what classes?

  13. #13
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >what classes?

    There's a global getline function defined that is used with std::string's.

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    where show me!

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Looks like this doesn't work

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

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, 05: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-27-2003, 12:03 AM