Thread: Rearranging Names

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    61

    Question Rearranging Names

    I have written this program first to only include first and last name..how could I know include the middle name?

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        //initializing variables
      string str;
      int i, index;
    
        //prompting user to input name
      cout << "Enter a first middle and last name: "  << endl;
      
        //telling computer to read input
      getline(cin, str); 
    
        //setting index to look at each character until space is found
      index = str.find(' ');
      
        //setting index for read the number of characters in last name once space
                                                                        //was found
      i = index + 1;
      while(str[i])
      {
        //output last name
    	  cout << str[i];
    	  i++;
      }
        //setting a comma between last and first name
      cout << ", ";
    
        //setting index for reading the number of characters in first name
      for(i = 0; i < index; i++)
    	  
          //output first name
          cout << str[i];
    
     
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Look for another space?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    Yeah I figured that out -put in order to change the output-what needs to be done to make the middle now be the last item listed?

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    Quote Originally Posted by BJtoVisualcC++ View Post
    Yeah I figured that out -put in order to change the output-what needs to be done to make the middle now be the last item listed?
    Only be concerned with today, yesterday is history, tomorrow is a mystery, today is a gift and that is why they call it the present.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    Sorry, still trying to learn how to post and post with quotes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. runtime function names
    By lruc in forum Tech Board
    Replies: 2
    Last Post: 10-11-2008, 10:51 AM
  3. reading folder names..how is it done ?
    By roalme00 in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2008, 10:34 AM
  4. Reading File Names
    By Thrack in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2007, 05:08 PM
  5. Ordering names
    By unixOZ in forum C Programming
    Replies: 1
    Last Post: 11-06-2002, 05:54 PM