Thread: convert from integer to string

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

    convert from integer to string

    hi, can someone give some idea on how to convert the integer that bring up from the functions and make the integers into one strings..

    for example

    sqrtOfMonth(month); // this solution is 2
    cout<<numPrimeFactor(year)<<endl;// this solution is 2
    largestPrimeFactor(year);// this solution is 283
    octalDay(day);// this solution is 35
    cout<<counter;// this solution is 6

    how can i make these single numbers and convert it to one string.
    and i want the output to be like 22 28 33 56.. in string..

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you could use stringstreams...
    Code:
    #include <iostream>	//for standard I/O
    #include <sstream>	//for stringstreams
    #include <string>	//for strings
    
    int main()
    {	
    	std::string line;	//this will hold the output
    	std::stringstream ss(std::ios::out);	//this will format the output
    
    	ss<<2<<2<<283<<35<<6;	//put the integers onto the stream
    	line=ss.str();		//output the stream into line
    	ss.str("");		//clear the stream
    
    	for(unsigned int i=0;i<line.length();i++)	//loop through each char
    	{
    		if(i%2==0 && i>0)	//for every two
    		{
    			ss<<' ';	//put a space on the stream
    		}
    		ss<<line.at(i);		//put the character on the stream
    	}
    
    	line=ss.str();	//save the stream to line
    	
    	std::cout<<line<<std::endl;	//output line
    	return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    hi, i still need help .. can you help....can we discuss using email...
    my email is [email protected]

    waiting for ur email ... so that i can tell you my problem

    thank you ..
    Last edited by peter_hii; 04-06-2006 at 09:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM
  5. FAQ how do i convert a string to an integer?? (C)
    By Unregistered in forum FAQ Board
    Replies: 1
    Last Post: 12-02-2001, 05:03 PM