hi I wanna find out the number of integers from an input number. For example if i enter 84075, the output should be 5.

this is what i came up with.....im new to c++ still>.<

#include <iostream>
using namespace std;

int main()
{
    int numCount, number;
    bool integer;
    cout << " Enter an integer: " << endl;
    cin >> number;
    if(number == ' ') 
        {
            integer = 0;
        }
        else if(number != ' ' )
        {
            numCount = 1;
            integer = 1;
            numCount++;
        }
    cout << numCount << " integers " << endl;  
}

Recommended Answers

All 11 Replies

you need to use a loop and divide the number by 10 until the result is 0.

you can delete lines 10 thru 19 because they are not relevent to the program requirements. The answer to the program has nothing to do with a space.

hmm...i cant figure out whats wrong with my loop....i dont get it>.<

#include <iostream>
using namespace std;



int main()
{
    int number, digitCount = 0, totalDigits = 0;
    cout << "Enter an integer: " << endl;
    cin >> number;
    
        
if (number >= 10)
  {         
    number = number / 10;
    digitCount++;
    totalDigits += digitCount;
  }
else
    
    cout << "Number of Digits: " << totalDigits << endl;
  
    
    return 0;
}

You don't have a loop!
The "if" executes the conditional, then drops out.
You want to use "while".
Get rid of totaldigits (unneccessary).
test for a remainder of 0.

>i cant figure out whats wrong with my loop
I know what's wrong with your loop: It doesn't exist! Compare this with your existing program:

#include <iostream>
using namespace std;

int main()
{
  int number, digitCount = 0;
  cout << "Enter an integer: " << endl;
  cin >> number;

  while ( number > 0 )
  {         
    number = number / 10;
    digitCount++;
  }
  
  cout << "Number of Digits: " << digitCount << endl;

  return 0;
}

Also note that there's a special case for if you enter 0.

oohh ok...

then how do i use the if-else statements correctly?

>then how do i use the if-else statements correctly?
Don't. You were using it correctly syntax-wise, but since the problem called for a loop, an if..else wasn't the correct construct.

#include<iostream.h>
#include<conio.h>

void main()
{
 char num[' '];
 int i;
 int count = 0;
 cout<<"enter the number\n";
 cin>>num;
 for(i=0;num[i]!='\0';i++)
  count++;
 cout<<count;
 getch();
}

That's actually pretty bad, ITech.

>#include<iostream.h>
This isn't standard, and many modern compilers will refuse to accept it.

>#include<conio.h>
This isn't standard, many compilers may not support it, and there's no need for it in your program.

>void main()
main returns void. No excuses.

>char num;
That's just awful. Why in the world did you think that using a character literal for the array size was a good idea? The actual value is unpredictable, and probably too much anyway.

>cin>>num;
There are two problems here. First you aren't protecting against buffer overflow, so if I type more than ' ' characters, cin will happily trash your memory beyond the legitimate size of num. Second, you aren't disallowing non-digits, so this program doesn't even do what it claims, which is to count the number of digits in an integer.

>cout<<count;
The output is a magic number. It's a good idea to tell your user what to enter and what the result means.

>getch();
Unnecessary.

That's actually pretty bad, ITech.

>#include<iostream.h>
This isn't standard, and many modern compilers will refuse to accept it.

>#include<conio.h>
This isn't standard, many compilers may not support it, and there's no need for it in your program.

>void main()
main returns void. No excuses.

>char num;
That's just awful. Why in the world did you think that using a character literal for the array size was a good idea? The actual value is unpredictable, and probably too much anyway.

>cin>>num;
There are two problems here. First you aren't protecting against buffer overflow, so if I type more than ' ' characters, cin will happily trash your memory beyond the legitimate size of num. Second, you aren't disallowing non-digits, so this program doesn't even do what it claims, which is to count the number of digits in an integer.

>cout<<count;
The output is a magic number. It's a good idea to tell your user what to enter and what the result means.

>getch();
Unnecessary.

Another thing, where is the namespace std?

Another thing, where is the namespace std?

It wasn't used with that old version of iostream.h

I dont know C++ but there is an easier way taht works for me in both C# and Java.

Integer number = 555;

System.out.println(number.toString().toCharArray().length);

This will put the number 555 into string "555" which in Char array is [5][5][5], and length of which is 3 which is gives you the digit count in a number. Saves you all the loop or methods.

I am sure there is toString() and toCharArray() equivalent in C++.

commented: Answer to a 2-year-old thread -5
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.