Thread: Program crash ?!

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    53

    Program crash ?!

    Hi everyone ! I was bored and decided to make my own version of itoa that would work for a std::string (since I work so often with it and always tell myself how how sad it is it wasn't yet overloaded for strings). Well I coded something but the program crashes when I call my function, could someone tell me where the bug is ? It's not a big function, it's only 6 lines :P
    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    
    template <class int_type>
    inline char* itoa_t(std::string& buf, const int_type nb) {
    	for(int i=0; pow(10, i)<nb; ++i)
    		buf += char((nb/(10*(i-1)))+'0');
    	return const_cast<char*>(buf.c_str());
    }
    
    int main(void) {
    	std::string blah = "";
    	itoa_t(blah, 123);
    	return 0;
    }
    Thank you all

  2. #2
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    You divide by 0 when i = 1.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Well nevermind, I just found out, but could anyone tell me why I get junk with the following code ? It output "½½"
    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    
    #define VALIDATE(a) (((a)==0)?1:a)
    
    template <class int_type>
    inline const char* itoa_t(std::string& buf, const int_type nb) {
    	for(int i=1; pow(10, i)<nb; ++i)
    		buf += char((nb/VALIDATE((10*(i-1))%10))+'0');
    	return const_cast<char*>(buf.c_str());
    }
    
    int main(void) {
    	std::string blah = "";
    	itoa_t(blah, 123);
    	std::cout << blah;
    	return 0;
    }
    For the most curious ones, the error occurred(spelling?) because it would divide by zero on the first run of the for loop.
    *EDIT* Beat me to it lol

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Now it outputs 326 with the following code, why ?
    Code:
    template <class int_type>
    inline char* itoa_t(std::string& buf, const int_type nb) {
    	buf += char(nb%10+'0');
    	for(int_type i=1; pow(10, i)<nb; ++i)
    		buf += char((nb/(10*i)%10)+'0');
    	return const_cast<char*>(buf.c_str());
    }
    *EDIT* I know that the string will be backwards but I will take care of that after, it should still output "321".

  5. #5
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    I don't want to take the time to look at your code.

    Here's my personal itoa function, converted to your template function:

    Code:
    template <class int_type>
    inline char* itoa_t(std::string& buf, const int_type nb) 
    {
    	std::string buffer;
    	int num = abs(nb);
    	const unsigned int numdigits = (log((float)num) / log(10.0f)) + 1;
    	unsigned int dig_buf;
    	
    	for(int_type i = 1; i <= numdigits; ++i)
    	{
    		dig_buf = static_cast<int>(pow(10.0f, i-1));
    
    		buffer = (num % static_cast<int>(pow(10.0f, i))) / dig_buf + '0';
    		buf += buffer;
    	}
    
    	if(nb < 0)
    	{
    		buf += '-';
    	}
    
    	std::reverse(buf.begin(), buf.end());
    	return const_cast<char*>(buf.c_str());
    }
    Maybe you can get ideas from that?

    It's longer and probably takes longer to execute, but I've never had to use itoa in a performace-critical part of code.
    Last edited by Kybo_Ren; 12-21-2004 at 08:44 PM.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> buf += char((nb/(10*i)%10)+'0');

    on the first iteration 10 * i == 10, then it's 20, 30, 40, etc. you want to divide nb by 10, then 100, 1000, 10000, etc.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    How stupid am I, thank you mate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char vs int - program crash!!
    By Goldrak in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 08:17 PM
  2. My program causes my compiler to crash
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 04:06 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM