0

I hope the code is self explanatory.

#include <iostream>
#include <cmath>
using namespace std;
int digits(int inp); //Prototyping correct?

int main()
{
    cout << digits(10);//I intend to have a cin statement here
                       //Set to a constant for checking, getting 24683872. Expected 2.
}

int digits(int inp)
{
    float a = 10;
    int i = 1;
    int n;
    int m_d = 1 ;
    while (m_d >= 1) //Keeps dividing by higher powers of 10 to find digits.
    {
        n = int(pow(a, i));
        m_d = inp / n;
        i++;
    }
    cout << (i-1);
}
Luke Peterson
  • 8,584
  • 8
  • 45
  • 46
Vaibhav
  • 13
  • 2
  • What exactly is your question? – Luke Peterson Mar 08 '15 at 07:43
  • 1
    There is a very clear answer at http://stackoverflow.com/a/10886429/434551. – R Sahu Mar 08 '15 at 07:46
  • The problem here is that your digits function doesn't return anything - the function prints `2` and then main appends a random integer. Instead of `cout`, at the end of the function, `return i - 1`;. This isn't a duplicate - you should however receive a compiler warning. – StuartLC Mar 08 '15 at 07:50
  • @StuartLC: Exactly what I was missing. Stupid, I know. Thanks a lot! – Vaibhav Mar 08 '15 at 10:16

0 Answers0