Adding extra characters to output

This is a discussion on Adding extra characters to output within the C++ Programming forums, part of the General Programming Boards category; I have a SSN inputed by the user. What is the best way to added dashes to that number before ...

  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 08: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, 05:46 PM
  2. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 02:00 PM
  3. Adding Strings/File Output
    By Dual-Catfish in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2001, 02:30 AM
  4. output of characters
    By deleeuw in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2001, 12:17 AM

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