Thread: Replace words in string

  1. #16
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    Ok, got an update here... still got a few errors.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    	string strInput;
    
    	cout << "\nPlease enter a gender specific sentence\n";
    	
    	cout << "or enter `I'm done' to exit the program.\n";
    		  
    		 getline(cin, strInput);  //gets the user input
    		  
    		  do
    		  	{
    			string::size_type loc = strInput.find("he", "him", 0);
    
    			if( loc = string::npos )
    			string strInput.replace("he", "he or she")||
    			string strInput.replace("him", "him or her")
    			cout << strInput;
    		
    			else 
    			cout << strInput;
    			
    			}
    			while 
    				(strInput != "I'm Done");
    				
    	return 0;
    	}
    		  
    	
    
    }
    Errors include:
    - Error E2285 lab6.cpp 29: Could not find a match for 'string::find(char *,char *,int)' in function main()

    - Warning W8060 lab6.cpp 31: Possibly incorrect assignment in function main()

    - Error E2108 lab6.cpp 32: Improper use of typedef 'string' in function main()

    - Warning W8004 lab6.cpp 39: 'loc' is assigned a value that is never used in function main()

    Thanks for replying

  2. #17
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    We also weren't assigned a book for the class, so it's difficult to have to go to outside resources to find examples ofeverything. =(

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    if you want do process several statements inside if-block use
    Code:
    if
    {
    ...
    }
    note that your strInput is not changed inside the loop, how do you plan to get the new input from the user?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #19
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    Ok, still thats easy to fix, just move the do up...

    Code:
    int main()
    {
    	string strInput;
    
    do
    {
    	cout << "\nPlease enter a gender specific sentence\n";
    	
    	cout << "or enter `I'm done' to exit the program.\n";
    		  
    		 getline(cin, strInput);  //gets the user input
    		  
    		  
    			string::size_type loc = strInput.find("he", "him", 0);
    
    			if( loc = string::npos )
    			string strInput.replace("he", "he or she")||
    			string strInput.replace("him", "him or her")
    			cout << strInput;
    		
    			else 
    			cout << strInput;
    			
    			}
    			while 
    				(strInput != "I'm Done");
    				
    	return 0;
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM