The number I'm looking for is valid when it's positive, and nine digits. I used ( (value >= 100000000) && (value <= 999999999) ) first, which worked okay, but did not account for leading zeroes. How do I validate a nine digit number, using only math and no functions, with possible leading zeroes?

The number I'm looking for is valid when it's positive, and nine digits. I used ( (value >= 100000000) && (value <= 999999999) ) first, which worked okay, but did not account for leading zeroes. How do I validate a nine digit number, using only math and no functions, with possible leading zeroes?

What you have isn't a number then. Numbers in base 10 can't begin with leading zero's. You have a bunch of digits, only.

Normally, I'd ask what data type "value" is (let's say it's a char array), then just use functions like strtol() to convert it to a long int, and Bob's your Uncle.

By far the easiest way is to treat 000042 (for instance), as a bunch of digits in a char array, and "walk" through the char array from right to left, and multiply each digit with it's column, according to the base 10 numbering system:

Loop starts here:

Far right column is * 1 (or just add), so '2' becomes 2
next column to the left is 10's, so 4 * 10 = 40, add the 2 and you have 42, your new subtotal.

Loop stops when you reach the end of the string.

You won't know if a 0 might have a leading digit further to the left of it, and thus be a legit part of the number, or just another leading left side zero.

In any case 0 * 100 + 42 is still 42, so we're good.

Start with strlen(), before you convert it.

Salem, he says he wants to use no functions. Could just count them up, but then that's the same thing as going through it with the index iterator.

The number I'm looking for is valid when it's positive, and nine digits. I used ( (value >= 100000000) && (value <= 999999999) ) first, which worked okay, but did not account for leading zeroes. How do I validate a nine digit number, using only math and no functions, with possible leading zeroes?

if you're making GTE or LTE comparisons, then you're obviously using "value" as a numeric value (probably an integer) and *not* a string.

there are no leading zeros in any value. it's a meaningless concept, as a numeric value, because you could conversely say there are infinite number of leading zeros in every value.

now you can *enter* leading zeros in a number, if you want, but it doesnt change the fact that 000123456 is the *exact same value* as 123456. there are only six digits in this value when expressed as base10. no matter how many zeros were entered.

similarly you can *print* a value with leading zeros, typically done so to format numeric data in an aesthetically pleasing manner. or you can print with leading spaces. It doesn't matter: there's no difference, and the "value" is still the same value, regardless of how it's printed.

printf("[%d]", value);    // prints: [123456]
printf("[%09d]", value);  // prints: [000123456]
printf("[%9d]", value);   // prints: [   123456]

in every case, value is still 123456. six digits as a base10 number.


the only time leading zeros becomes meaningful is if you are counting the number of CHARACTERS in a string. if you require that there be nine and only nine numeric characters in your input string, then you need to count each character and verify that each character is a numeric value 0-9. afterwards you can convert the entire string to a number, and not worry about its length or what range the value falls in, because you've already validated that it has exactly nine digits from 0-9.


.

Converting a string to an int (without functions) is no harder than working out the length of the string (without functions).

In fact, you can pretty much cover it with the same code.

something along these lines, i suppose.

#define NUMBER_BASE     10  // change to other base as needed

while (*str != '\0')
{
    ++length;
    value *= NUMBER_BASE;
    value += *str++ - '0';
}

This will not work without a few key lines of code to perform proper initialization, which I purposely removed so this is for illustrative purposes. It also needs a lot of error checking to account for all sorts of potential bad input.


.

adhasd I completely forgot that I couldn't use arrays. The only possible things I can do are loops and if statements : |

something along these lines, i suppose.

#define NUMBER_BASE     10  // change to other base as needed

while (*str != '\0')
{
    ++length;
    value *= NUMBER_BASE;
    value += *str++ - '0';
}

This will not work without a few key lines of code to perform proper initialization, which I purposely removed so this is for illustrative purposes. It also needs a lot of error checking to account for all sorts of potential bad input.


.

I don't understand the *str part--haven't learned it yet I suppose. Is there a way to do this without using #define NUMBER_BASE (and *str) though? I haven't even learnt #define yet, new to C.

yeah, you can do it without using a #define.

what that does is allow you the flexiblity to easily change the code to do other conversions such as from base2 binary or base16 hex. just delete the define and replace the macro name with 10 if you don't want it.

as for doign it with a pointer (*str), i suppose you can do it with an array (str) instead. i wouldn't ever do it that way. sometimes i wonder if they would do better to teach students pointers first...

yeah, you can do it without using a #define.

what that does is allow you the flexiblity to easily change the code to do other conversions such as from base2 binary or base16 hex. just delete the define and replace the macro name with 10 if you don't want it.

as for doign it with a pointer (*str), i suppose you can do it with an array (str) instead. i wouldn't ever do it that way. sometimes i wonder if they would do better to teach students pointers first...

Is there any other way to do it without using an array? I'm only allowed to use loops and if statements >_>

Is there any other way to do it without using an array? I'm only allowed to use loops and if statements >_>

well, you can do it with spit and duct tape if you want, i suppose.

but if you want to do it in C, you've got to use something. It won't just magically sort itself out for you.

well, you can do it with spit and duct tape if you want, i suppose.

but if you want to do it in C, you've got to use something. It won't just magically sort itself out for you.

Yeah, that's why I'm asking here. I've been googling for days and came up with nothing that's within the boundaries of what I'm allowed to do and what I know how to do. Thanks for all the help, but I really can't utilize the suggestions or take anything useful away from it with all the limitations--knowledge-wise or project-wise.

sorry i didnt understand your limitations.

so ... then use getchar() in a while loop that breaks on a newline.

each successive character retrieved in the while loop increments the length counter and the character's numeric decimal value is added to the total value, after the total value is first multiplied by 10

just like in the example i gave you using the pointer method.

you have all the answers you need to do this, right here in this thread.


.

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.