Hi, I did some C++ assignments and was confused about a certain code.

#include <iostream>
#include <iomanip>
using namespace std;


int main()

{
    
    long result, input, num, count;
    count = 0;
    
    cout << "key in number";
    cin >> input;
    while (input>0)
          { count++;
          input /=10;
          
          }
          
    cout << count;
    
    cout << "\n";
    system("pause");
    return 0;

}

For this code, I'm trying to find how many digits for the number at input.
However, I can't get the answer for 9999999999 (10 digits) but it works fine for 9 digits and below.
I know it's easy to explain but I can't figure it out. Sorry about that =\

Recommended Answers

All 2 Replies

9999999999 cannot be stored in a long typed variable. You will have to use a double typed variable. However if you use double , you will have to change the condition inside the while statement. I will leave you to figure that out.

Edit:
Or use long long . Much easier.

Aaaa I see, forgot about the different types of variables and their limits.
Was using int before I tried long.

Thanks again Wolfpack!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.