Thread: Adding extra characters to output

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    Adding extra characters to output

    I have a SSN inputed by the user. What is the best way to added dashes to that number before I display it back on the screen?

    input 123456789
    output needs to look like 123-45-6789

    Thanks,

  2. #2
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hmm...

    This little program should give you the idea :
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string makeformat(string oldstring)
    {
    	string answer;
    	
    	answer = oldstring.substr(0, 3) + "-" + oldstring.substr(3,2) + "-" + oldstring.substr(5, 4);
    	
    	return answer;
    }
    
    int main()
    {
    	string string1 = "123456789";
    	string newstring;
    	
    	newstring = makeformat(string1);
    	
    	cout << newstring << endl;
    
                    return 0;
    }
    Last edited by moonwalker; 07-11-2003 at 07:39 AM.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    Thanks, that worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting characters and output to text file
    By odb1 in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2003, 04:46 PM
  2. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM
  3. Adding Strings/File Output
    By Dual-Catfish in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2001, 01:30 AM
  4. output of characters
    By deleeuw in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:17 PM