split number in two..

This is a discussion on split number in two.. within the C++ Programming forums, part of the General Programming Boards category; hi can someone show me how to split numbers? for example 12345678910 the output will be 12 34 56 78 ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    Question split number in two..

    hi can someone show me how to split numbers?
    for example

    12345678910

    the output will be 12 34 56 78 910

    can some one give me some idea?please?

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    substr() ?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You can use 'stringstreams':
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
    	int num = 123456;
    
    	//Turn num into a string:
    	ostringstream myOutputStr;
    	myOutputStr<<num;
    	string myStr = myOutputStr.str();
    	
    	//Get a substring of num:
    	string str1 = myStr.substr(0,2);
    	
    	//If you need to, turn the substring into an int:
    	istringstream myInputStr(str1);
    	int num1 = 0;
    	myInputStr>>num1;
    	
    	//Display the result:
    	cout<<num1<<" * 2 = "<<num1 * 2<<endl;
    	
    	return 0;
    }
    Last edited by 7stud; 04-01-2006 at 10:15 AM.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Question #1: *how* do you want to split them?

  5. #5
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    (Off Topic) Sorry but just had to ask, to 7stud:
    Is using stringstreams better (faster) then using itoa and atoi to convert integers to strings and the way arround?
    If so, why?

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> Is using stringstreams better (faster) then using itoa and atoi to convert integers to strings and the way arround? If so, why?

    Does speed denote betterness? There are many qualifications for good code.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Is using stringstreams better (faster) then using itoa and atoi to convert integers to strings and the way arround?
    itoa() isn't part of the C++ standard. MS made it up.
    Last edited by 7stud; 04-01-2006 at 08:21 PM.

  8. #8
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    atoi is part of C89...
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Yep, I was researching that, then I changed my post, and then I saw your post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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