Thread: Leading zeroes

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Leading zeroes

    So I had a program that made a number out of entered digits, but it doesn't work. Here's what it should print:
    Code:
    This program creates a number one digit at a time.
    Signal the end of the number with a negative value.
    
    Enter a number: 4
    Enter a number: 11
    Enter a number: 5
    Enter a number: 73
    Enter a number: 6
    Enter a number: 24
    Enter a number: -1
    
    You entered a 3 digit number: 456
    but it doesn't work.
    My code is:
    Code:
    /*
    * Create a number by entering single digits similar to a calculator.
    * If numbers other than 0 thru 9 are entered they should be ignored.
    * At the end, display the number of digits and the final number.
    */
    
    
    #include<iostream>
    using namespace std;
    
    
    int main()
    {
    // give instructions
    cout << "This program creates a number one digit at a time." << endl;
    cout << "Signal the end of the number with a negative value." << endl;
    
    
    int number = 0;
    int cin2 = 0;
    int cinvar = 0;
    // PUT CHECK FOR LEADING ZEROES (ZEROES BEFORE A NUMBER)
    // begin reading digits until negative entered
    int count = 0;
    int digit = 0;
    while (true) {
    cout << "Enter a number: ";
    cin >> digit;
    if(digit != 0) {
             cinvar++;
             }
             else {
                  cin2++;
                  }
    if(cin2 < cinvar) {
     if (digit < 0) {
    break;
    }       
    if (digit >= 0 && digit<=9) {
    number = number + digit;
    count++;
    
    
    }
    }
    // count digits
    
    
    // check to see if we are done
    if (digit < 0) {
    break;
    }// add to our number
    if (digit > 0 && digit<=9) {
    number = number*10 + digit;
    count++;
    
    
    }
    
    
    }
    
    
    // final report
    // count = count - 1;
    cout << endl << "You entered a " << count << " digit number: " << number << endl;
    }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    What does your code do that's different than the expected behavior?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to figure out how to indent code soon.
    Indent style - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Salem View Post
    You need to figure out how to indent code soon.
    You are trying to talk to a stone wall. This guy does not understand what indentation means even if told a hundred times. That, or it goes in through one ear and out the next.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User setleaf's Avatar
    Join Date
    Dec 2014
    Location
    Virginia/USA
    Posts
    47
    I would like to try my hand at helping, but I'm not sure what you're trying to achieve.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    To clarify, the code is printing the wrong number of digits for some cases (but not others), and the final output has too many digits.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Kelton2 View Post
    To clarify, the code is printing the wrong number of digits for some cases (but not others), and the final output has too many digits.
    Would you please post your output?

    I only ask because I'm not going to copy and past your code, compile it and run it.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Let's review:
    Code:
        cin >> digit;
    
        if (digit < 0) {
            break;
        }// add to our number
        if (digit >= 0 && digit<=9) {
            number = number*10 + digit;
            count++;
        }
    In this code, we first check if the number is less than zero because if it is, the program needs to stop asking for input. If instead we received input that is in the range of digits, it becomes part of the number, otherwise nothing happens. Count here would correctly count the digits.

    First you need to realize that input like 00, 01, 02, 05, and so forth will not make a difference, by default. The only snag is if the input is 0 right from the start.

    That is a simple check:
    Code:
        if( number == 0 && digit == 0 ) 
           continue; // get another digit
    My advice is to cut the cruft.
    Last edited by whiteflags; 01-24-2015 at 01:37 PM.

  9. #9
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    So I would replace
    Code:
    cin >> digit;
    
    if (digit < 0) {
        break;
    }// add to our number
    if (digit >= 0 && digit<=9) {
        number = number*10 + digit;
        count++;
    }
    

    with
    Code:
    
    
    Code:
    [COLOR=black !important]if( number == 0 && digit == 0 ) [/COLOR]
    cin >> digit;
    

    ?


  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You would be adding code, not deleting. The changes I want you to make would allow you to do something your code couldn't before. The words in my post should make the necessary changes clear.

  11. #11
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    So I would add that if statement somewhere in that other block of code?

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Right. If you understand what that if does, integrating it is easier. The topic is called "leading zeros". So, if the number is set to zero in the beginning of the program, number has not changed, and the current input is zero, we have, by induction, found a leading zero. All we do in that situation is ignore the current input.

  13. #13
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Still not working:
    Code:
    /*
    * Create a number by entering single digits similar to a calculator.
    * If numbers other than 0 thru 9 are entered they should be ignored.
    * At the end, display the number of digits and the final number.
    */
     
     
    #include<iostream>
    using namespace std;
     
     
    int main()
    {
    // give instructions
    cout << "This program creates a number one digit at a time." << endl;
    cout << "Signal the end of the number with a negative value." << endl;
     
     
    int number = 0;
    int cin2 = 0;
    int cinvar = 0;
    // PUT CHECK FOR LEADING ZEROES (ZEROES BEFORE A NUMBER)
    // begin reading digits until negative entered
    int count = 0;
    int digit = 0;
    while (true) {
    cout << "Enter a number: ";
    cin >> digit;
    if(digit != 0) {
             cinvar++;
             }
             else {
                  cin2++;
                  }
    if(cin2 < cinvar) {
     if (digit < 0) {
    break;
    }       
    if (digit >= 0 && digit<=9) {
    number = number + digit;
    count++;
     
     
    }
    }
    // count digits
     
     
    // check to see if we are done
    if (digit < 0) {
    break;
    }// add to our number
    if( number == 0 && digit == 0 ) 
       continue; 
    if (digit > 0 && digit<=9) {
    number = number*10 + digit;
    count++;
     
     
    }
     
     
    }
     
     
    // final report
    // count = count - 1;
    cout << endl << "You entered a " << count << " digit number: " << number << endl;
    }

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually I'm certain that the portion of code that was edited is enough to make the whole program work. Here is the output to prove it:

    This program creates a number one digit at a time.
    Signal the end of the number with a negative value.
    Enter a number: 0
    Enter a number: 0
    Enter a number: 0
    Enter a number: 0
    Enter a number: 1
    Enter a number: 21
    Enter a number: 2
    Enter a number: 22
    Enter a number: 3
    Enter a number: 56
    Enter a number: 4
    Enter a number: -1

    You entered a 4 digit number: 1234

    You are clearly working with too much code. As I said before, you need to cut the cruft.

  15. #15
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    OK, but what exactly in my program is causing it to not work? You said that you managed to make it work by implementing the same, so what's wrong in mine?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove leading zeroes
    By hss1194 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2012, 04:13 PM
  2. Pad leading zeroes for float in printf
    By Maroli5 in forum C Programming
    Replies: 3
    Last Post: 05-04-2011, 04:40 PM
  3. Computing trailing zeroes of a factorial.
    By msh in forum C Programming
    Replies: 2
    Last Post: 09-15-2010, 06:10 PM
  4. Removing zeroes from float
    By lollerpoller in forum C Programming
    Replies: 1
    Last Post: 05-14-2004, 03:33 AM
  5. Initilizing global array to all zeroes
    By Durafei in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2003, 12:33 PM