Thread: counting digits
View Single Post
Old Apr 10th, 2007, 11:03 AM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
I have a problem with overkilling the simple things I guess... So I won't kill all of your fun, I'll leave the leading zero issue up to you to solve (if there is even a potential for it to be a problem)

#include <iostream>
#include <sstream>

int CountDigits (int source);
std::string itos (int num);

int main (void)
{
    int x = 0;
    int y = 12345;
    
    x = CountDigits(y);
    
    std::cout << "There are " << x << " digits in " << y << std::endl;
    std::cin >> y;
    
    return 0;
}

int CountDigits (int source)
{
    int count = 0;
    std::string z = itos(source);
    return z.length();
}

std::string itos (int num)
{
   std::ostringstream myStream;
   myStream << num << std::flush;
   return (myStream.str());
}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote