So I am trying to find the number of digits in a given int. The way I opted to find the digits was to divide the given int by powers of 10. Since I would be dividing by integers, the code would eventually reach a 0 when 10 is raised to a high enough power. There would be a tracker that tracks every iteration of this and that tracker would be able to tell me how digits there were.
#include <cmath>
#include <iostream>
int findDigits(int x)
{
//tracks the digits of int
int power{ 0 };
for (int i = 0; x / pow(10, i) != 0; i++) //the condition is not working as it should
{
//I checked the math expression and it is getting the intended result of zero at i = 3
int quotient = x / pow(10, i);
//I then checked the condition and it is becoming false when the expression results in 0
bool doesWork;
if (x / pow(10, i) != 0)
doesWork = true;
else
doesWork = false;
power = i;
}
return power;
}
int main()
{
std::cout << "157 has " << findDigits(157) << " digits";
return 0;
}