-4

I've tried while and for loops, but obviously it can't store the last digit since it's 0. So I'm wondering how I can make my program know the first digit is 0?

Thanks in advance!

dr 21
  • 21
  • 6
  • You should understand how values are stored in memory. It is not the text which it is in there! – Klaus Oct 10 '20 at 12:00
  • You don't need to explicitly store leading zeros. Variables have a fixed size. The zeros are stored in memory, e.g. the value `1` often is stored as hexadecimal `0x00000001` as 4 byte `int` (depending on compiler). The leading zeros are implicitly stored but not printed. – Thomas Sablik Oct 10 '20 at 12:04
  • @ThomasSablik I'm trying to count the ammount of 0's I have in an int, however the leading 0 isn't being counted. That's what I'm trying to find out. You might find this question silly but I'm a complete beginner. – dr 21 Oct 10 '20 at 12:06
  • Variables have a fixed size and therefore a max value. 4 byte `unsigned int` has max value `4,294,967,295`. That's 10 digits. Now you can calculate the difference, e.g. you know that the value `1234` has 6 leading zeros. You can use [numeric_limits](https://en.cppreference.com/w/cpp/types/numeric_limits) to find out this limits and use them. – Thomas Sablik Oct 10 '20 at 12:10

2 Answers2

2

It's not possible to store leading zeros of a user input in a numerical variable. You have to use a string to differ between input 001 and 01:

#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string input;
    std::cin >> input;
    
    const auto numberOfZeros = std::count(std::begin(input), std::end(input), '0');
    std::cout << "Zeros: " << numberOfZeros << '\n';
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
-1

From a theoretical point of view you need to know:

  • What is the maximum number of digits that you want to represent (e.g. if you used 10 digits then 9080 would have 6 leading zeroes, 00000 09080).
  • How many significant digits your number has (e.g. 9080 has four).

How can we do this programmatically? We just calculate the logarithm to the base of 10. If you know that the maximum number you can represent is limited, you can also perform a lookup.

Let's have a look at the lookup first (I'm not so good in C++, so syntax might vary a bit):

size_t numDigits(unsigned short myInt) {
    if (myInt == 0) {
        return 0;
    } else if (myInt < 10) {
        return 1;
    } else if (myInt < 100) {
        return 2;
    } else if (myInt < 1000) {
        return 3;
    } else if (myInt < 10000) {
        return 4;
    } else { /* unsigned short cannot be larger than 65535 */
        return 5;
    }
}

To check whether your digit starts with a zero (with the maximum number of digits you are interested in) you just compare the number of digits with the number of maximum digits, e.g. 5 in your case:

bool hasLeadingZero(unsigned short myInt, size_t maxNumDigits) {
    if (numDigits(myInt) < maxNumDigits) {
        return true;
    } else {
        return false;
    }
}

Now, you might not have a short but maybe a higher number like int or long or even long long and do not want to write all the comparisons in code. In this case, we can take the function that uses log10 from the linked stackoverflow question (I adjusted it a bit so that it's simpler to read for beginners, plus I changed the number of digits of 0 to 0, because in your situation it does not count as a digit):

size_t numDigits(unsigned i) {
    if (i == 0) {
        return 0;
    } else {
        return (size_t) log10 ((double) i) + 1;
    }
}

This should work if I understand your question correctly. Maybe needs some code adjustments, because I just typed it in the text box here.

aufziehvogel
  • 7,167
  • 5
  • 34
  • 56