plzz need help with remove() method

This is a discussion on plzz need help with remove() method within the C++ Programming forums, part of the General Programming Boards category; this is a homework assignment i have read this chapter many times over it does not explain how to do ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Post plzz need help with remove() method

    this is a homework assignment i have read this chapter many times over it does not explain how to do exactly what i need to do. i have worked on this program for almost two weeks. this class is online and that makes it harder for me. i would appreciate any help or input someone is willing to give me. the program is suppose to ask the user for their full name with spaces between first, middle, last after the user inputs their full name. the program should display their full name followed by their 3 letter initials. i can't figure out how to search for the spaces in the name input and keep the first char and delete the rest. my book only gives examples where a name is predetermined and the length is known. in my program this information will not be known. if anyone will help i would appreciated. thanks

    [tag]
    Code:
    //Purpose:
    //Author:
    //Date:
    //Input:
    //Output:
    
    #include <iostream>
    #include <string>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    
    
    int main ()
    {	//declare variables
    	string name = " ";
    	string initials = "";
    	string currentChars = "";
    	int numChars = 0;
    	int subscript = 0;
    	
    	// get name
    	cout << "Enter your full name include spaces between first,middle,last: ";
    	cin >> name;
    	cout << name;
    
    	//determine number of characters
    	numChars = static_cast<int>(name.length());
    
    	while (subscript < numChars)
    	{
    		currentChars = name.substr(subscript, 1);
    		if (currentChars == " ")
    
    		{
    			name.erase(subscript, 1);
    			numChars = numChars -1;
    		}
    		else
    			subscript = subscript + 1;
    		//end if
    		//end while
    	}
    	//dispaly initials
    	cout << "Initials: " << initials << endl;
    	
    	return 0;
    }
    [
    [tag]

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Don't modify name at all. Just examine the characters in name one at a time and each time you find an "initial" add it to initials.

    A simple way to do this is by scanning the name string using empty while loops that compare each letter to ' ' (blank space).

    Did you know that you can access the characters in the string using the [] operator? name[0] is the first character. name[numChars-1] is the last.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, why bother with a cast here:
    Code:
    numChars = static_cast<int>(name.length());
    when you could just make numChars & subScript unsigned int (or more specifically size_t) types and not have to worry about casting to an int.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 06:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 04:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 03:22 PM

Tags for this Thread


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