string stops at first letter

This is a discussion on string stops at first letter within the C++ Programming forums, part of the General Programming Boards category; Hey, can anyone help me figure out why the program keeps stopping with the first letter instead of following through ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    22

    string stops at first letter

    Hey, can anyone help me figure out why the program keeps stopping with the first letter instead of following through the string??
    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    bool isVowel (char);
    string noVowelString (string);
    
    
    int main ()
    {
    	string str;
    
    
    	cout<<"Enter a sentence: ";
    	cin>>str;
    	cout<<endl;
    
    
    	cout<<"The sentence with the vowels removed is "<<noVowelString(str)<<endl;
    
    
    	
        system("pause");
        return 0;
    }
    
    
    bool isVowel (char ch)
    {
    	switch (ch)
    	{
    	case 'A':
    	case 'E':
    	case 'I':
    	case 'O':
    	case 'U':
    	case 'a':
    	case 'e':
    	case 'i':
    	case 'o':
    	case 'u':
    		return true;
    	default:
    		return false;
    	}
    }
    
    
    string noVowelString (string tStr)
    {
    	string :: size_type len=tStr.length();
    
    
    	bool foundVowel = false;
    
    
    	string :: size_type i;
    
    
    	for (i = 0; i < tStr.length; i++)
    	{
    		if (isVowel (tStr[i]))
    		{
    			tStr.erase (i, 1);
    			foundVowel = true;
    		}
    
    
    		else
    			tStr = tStr.substr (i, 1);
    	}
    	return tStr;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    You can't use the extraction operator>> to extract sentences. The extraction operator>> stops processing the string when it encounters a white space character. If you want your string to contain white space characters I recommend you try using the getline() function.

    Jim

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You're also modifying the string as you iterate over it, which spells disaster.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change one letter from a string
    By gunitinug in forum C Programming
    Replies: 4
    Last Post: 12-01-2011, 03:05 AM
  2. Replies: 12
    Last Post: 03-07-2011, 12:24 AM
  3. counting letter occurences in a string
    By pjr5043 in forum C++ Programming
    Replies: 35
    Last Post: 05-05-2008, 09:18 PM
  4. Remove a letter from a string
    By StarOrbs in forum C++ Programming
    Replies: 6
    Last Post: 04-05-2005, 03:03 PM
  5. checking if a string contains a letter
    By lonbgeach in forum C Programming
    Replies: 2
    Last Post: 04-07-2003, 03:40 PM

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