# Thread: My own fuction which count digits in number doesn't work as it should :/

1. ## My own fuction which count digits in number doesn't work as it should :/

Hi,
I wrote a program with function my own function which count the digits of entered number. The problem is whatever i type it shows 0 digits.Why is that?

Hoping for any help Code:
```#include <iostream>

using namespace std;

int cikCipari (int skaitlis, int cipars);
int main()
{

int x = 0;
int sk = 0;
cout << "Enter a number: ";
cin >> x;

cout << "There are " << sk << " digits." <<endl;
return 0;
}

int cikCipari (int skaitlis, int cipars){

int cik = 0;
//int skaits = 0;
// int skaitlis = 0;
while (skaitlis != 0){
skaitlis /= 10;
cout << cik;
cik++;
}

if (skaitlis == 0){
cik = 1;
}
return cik;
}``` 2. Originally Posted by Solarwin Hi,
I wrote a program with function my own function which count the digits of entered number. The problem is whatever i type it shows 0 digits.Why is that?

Hoping for any help Code:
```#include <iostream>

using namespace std;

int cikCipari (int skaitlis, int cipars);
int main()
{

int x = 0;
int sk = 0;
cout << "Enter a number: ";
cin >> x;

cout << "There are " << sk << " digits." <<endl;
return 0;
}

int cikCipari (int skaitlis, int cipars){

int cik = 0;
//int skaits = 0;
// int skaitlis = 0;
while (skaitlis != 0){
skaitlis /= 10;
cout << cik;
cik++;
}

if (skaitlis == 0){
cik = 1;
}
return cik;
}```
You're not actually calling your function anywhere within main(); sk remains set to 0 because you aren't setting it to anything else.

Once you fix that, your second issue is the if() statement in your code. That if statement is guaranteed to always execute, because you can't get past the while() statement until skaitlis == 0.

So what will happen is you will always return 1. 3. Originally Posted by Cat You're not actually calling your function anywhere within main(); sk remains set to 0 because you aren't setting it to anything else.

Once you fix that, your second issue is the if() statement in your code. That if statement is guaranteed to always execute, because you can't get past the while() statement until skaitlis == 0.

So what will happen is you will always return 1.
Thanks, but i dont get it what do i have to do with my if to solve second issue. 4. Couple of options, but the first that comes to mind is to check for >= 10 in the while() loop (essentially, count all digits except the last) and then always add 1 before returning the value. 5. Originally Posted by Cat Couple of options, but the first that comes to mind is to check for >= 10 in the while() loop (essentially, count all digits except the last) and then always add 1 before returning the value.
I am very new to this, i tried many ways all of them ended with -1.:/
Code:
```int cikCipari (int skaitlis){

int cik = 0;
//int skaits = 0;
skaitlis = 0;

while (skaitlis != 0){
skaitlis /= 10;
cik++;
for(int i=0;i>= 10;i++){
skaitlis+=1;
}
}

if (skaitlis == 0){
cik = 1;
}

return cik;
}``` 6. Sorry, I meant like this:

Code:
```int cikCipari (int skaitlis){
int cik = 0;

while (skaitlis >=10){
skaitlis /= 10;
cik++;
}
return cik + 1;
}```
The while() loop now counts all but the last digit, which you take care of at the end. Be careful not to set skaitlis to 0 at the beginning of your function, too - it's already initialized to the value passed in to the function. 7. Originally Posted by Cat Sorry, I meant like this:

Code:
```int cikCipari (int skaitlis){
int cik = 0;

while (skaitlis >=10){
skaitlis /= 10;
cik++;
}
return cik + 1;
}```
The while() loop now counts all but the last digit, which you take care of at the end. Be careful not to set skaitlis to 0 at the beginning of your function, too - it's already initialized to the value passed in to the function.
THANK YOU very much u helped alot, Cat.  8. The best way I've found to solve it is to use a do..while loop like this:
Code:
```int digitLength(unsigned int val)
{
int digits = 0;
do {
val /= 10;
++digits;
} while (val > 0);
return digits;
}``` Popular pages Recent additions 