#include <iostream>
#include <string>
#include <conio.h>
usingnamespace std;
int main() {
string number;
cout << "Enter a number: ";
cin >> number;
cout << "The length of the number you just entered is " << number.length() << " lol." << endl;
cout << "press any key to exit...";
_getch();
return 0;
thats such a lame way to do it! If you aren't doing this in a hurry i say take the time and think of creating your own function to take length of a number, its really fun
int x;
cin >> x;
int lengthCount = 0
for(; x != 0; x /= 10, lengthCount++);
It keeps dividing by 10, and as long as the number is not 0 it will add to the count and then divide by 10 again. If the user enters 0, it reports the length as zero, for all other single digit numbers it would report it as 1.
@time to c: Did you execute the mussaAli's program? You enter a number (an integer) and the program see that number that you entered as a string. The program is doing his job even if he works with strings. I think is more faster to work with numbers as strings.
But if you want to see the length of an integer (of a number) without working with strings, look at the Intrexa's post.
#include<iostream>
usingnamespace std;
int lengthfunction(int number); //prototype
int main()
{
int number;
cout<<"Enter the number: ";
cin>>number;
cout<<"The length of that number is: "<<lengthfunction<<"\n";
return 0;
}
int lengthfunction(int number)
{
int counter=0;
while(number)
{
number=number/10;
counter++;
}
return (counter);
}