Like Tree5Likes
  • 2 Post By Cat
  • 1 Post By Cat
  • 1 Post By Cat
  • 1 Post By iMalc

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

This is a discussion on My own fuction which count digits in number doesn't work as it should :/ within the C++ Programming forums, part of the General Programming Boards category; Hi, I wrote a program with function my own function which count the digits of entered number. The problem is ...

  1. #1
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50

    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. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Solarwin View Post
    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.
    Last edited by Cat; 02-19-2013 at 04:23 PM.
    Solarwin and iMalc like this.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by Cat View Post
    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. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.
    Solarwin likes this.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by Cat View Post
    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. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.
    Solarwin likes this.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by Cat View Post
    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. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    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;
    }
    Solarwin likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2010, 05:53 AM
  2. Count the number of vowels, consonants, digits etc.
    By kumar14878 in forum C Programming
    Replies: 3
    Last Post: 05-09-2005, 12:34 AM
  3. itoa function : doesn't work for -ve number ?????
    By gemini_shooter in forum C Programming
    Replies: 2
    Last Post: 03-22-2005, 05:50 AM
  4. count digits
    By Mariana in forum C++ Programming
    Replies: 7
    Last Post: 03-03-2004, 06:56 PM
  5. Count Number of Digits in and Integer
    By redneon in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2003, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21