Hamumu Software Hamumu Software Hamumu Software
Name
Password Register
Latest Journal update: May 12, 2013
Old 06-16-2008, 02:10 PM   #1
AtkinsSJ
Legs sold separately.
Moderator
 
AtkinsSJ's Avatar
 
Join Date: Aug 2004
Location: In a robot, destroying Tokyo.
Posts: 4,775
Default C++ Questions

In order to not unfairly hijack Ducky's thread, I thought I'd create a new topic for me to ask my fairly-regular questions on how to do stuff in C++.

Firstly, is there a built-in function for converting an integer to a string? If not, I could always try to write one myself, but a ready-made function would be very helpful.
__________________

Hamupload |C.H.E.E.S.E. Unpickled
AtkinsSJ is offline   Reply With Quote
Old 06-16-2008, 02:43 PM   #2
SpaceManiac
Veteran Programmer
 
SpaceManiac's Avatar
 
Join Date: Oct 2007
Location: Elsewhere
Posts: 2,880
Send a message via Skype™ to SpaceManiac
Default Re: C++ Questions

As far as I know, there isn't, but this should work well. Put it in a file, say num2string.h
PHP Code:
#include <string>
namespace num2string {
    
typedef unsigned long int BigNum;
    
typedef unsigned int Num;
 
    
std::string numbers[] = {"zero""one""two""three""four""five""six""seven""eight""nine"};
 
    
BigNum power(int baseint exp)
    {    
// this works for normal cases with positive exponents
        
if(exp == 0) return 1;            // x^0 = 1
        
if(exp == 1) return base;        // x^1 = x
        
return base power(base,exp-1);
    }
 
    
std::string num2string(int n) {
        
std::string str = ("negative " "");
        
Num num = (? -n);
        
Num y;
        for(
0power(10,y) <= numy++);
        
// y now contains the highest power plus 1
        
for(Num p 1>= 0p--)
        {
            
// calculate digit
            
int x = (num power(10,p)) % 10;
            
// add to string
            
str += numbers[x] + " ";
    
/* this is necessary; an unsigned long will wrap around and become a very large number
       (which IS >= 0), causing infinite looping. However, in this case it never gets that
       far because 10^(maximum unsigned long value) will always overflow! */
            
if (== 0) break;
        }
        return 
str.trim();
    }

A simple program using this code might resemble the following.
PHP Code:
#include <string>
#include <iostream>
#include "num2string.h"
using namespace std;
 
int main()
{
    
int num;
    
string str;
    
cin >> num;
    
str num2string::num2string(num);
    
cout << str;
    return 
0;

Thanks to Mr.Onion for the tip on str.trim()!
SpaceManiac is offline   Reply With Quote
Old 06-16-2008, 04:38 PM   #3
Jamul
Marshmallow Head
The Boss
 
Jamul's Avatar
 
Join Date: Jun 2003
Location: Anza, CA
Posts: 6,183
Default Re: C++ Questions

But there is... sprintf!
__________________
Jamul is offline   Reply With Quote
Old 06-16-2008, 06:41 PM   #4
SpaceManiac
Veteran Programmer
 
SpaceManiac's Avatar
 
Join Date: Oct 2007
Location: Elsewhere
Posts: 2,880
Send a message via Skype™ to SpaceManiac
Default Re: C++ Questions

Ah yes, my solution is for numbers to words. If you just need character representations, Jamul's idea is best.
Code:
sprintf(buffer,"%d",num);
I think that's how it works... I don't use the C standard library much. Just the C++ one (yay std::string!)
SpaceManiac is offline   Reply With Quote
Old 06-16-2008, 07:39 PM   #5
Ducky
King of the pond!
 
Ducky's Avatar
 
Join Date: Dec 2004
Location: Duck Pond, CT
Posts: 343
Send a message via MSN to Ducky
Default Re: C++ Questions

Quote:
Originally Posted by Jamul View Post
But there is... sprintf!
Aww...Atkins...Jamul posted in your thread. Lucky guy. LOL

-Ducky (VENI, VIDI, ROLEX...I came, I saw, - what time it was!)
__________________

Mallard Memory 3 and Duck Wars --- Stopped
Learning new C++ programming library now.
Ducky is offline   Reply With Quote
Old 06-20-2008, 12:27 PM   #6
AtkinsSJ
Legs sold separately.
Moderator
 
AtkinsSJ's Avatar
 
Join Date: Aug 2004
Location: In a robot, destroying Tokyo.
Posts: 4,775
Default Re: C++ Questions

Oh, cool! I didn't realise you could print to something other than the screen. Thanks.
__________________

Hamupload |C.H.E.E.S.E. Unpickled
AtkinsSJ is offline   Reply With Quote
Old 06-20-2008, 07:34 PM   #7
Ducky
King of the pond!
 
Ducky's Avatar
 
Join Date: Dec 2004
Location: Duck Pond, CT
Posts: 343
Send a message via MSN to Ducky
Default Re: C++ Questions

Quote:
Originally Posted by AtkinsSJ View Post
Oh, cool! I didn't realise you could print to something other than the screen. Thanks.
Oh yeah..there is even fprintf (To print to files!)

- Ducky (VCRs are irrelevant. - Locutus of RCA)
__________________

Mallard Memory 3 and Duck Wars --- Stopped
Learning new C++ programming library now.
Ducky is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Site Map
Copyright 2013, Hamumu Software