0
{
    long long l;

    do
    {
        l = get_long_long("Number: ");
    }
    while (l < 0);

    float count = 0;

    long long m = l;

    while( m != 0)
    {
        m /= 10;
        count++;
    }

    float n = count;

    for (float i = 1; i < n; i = (i + 2))
    {

        printf("%lld\n", ((l / (pow(10.00, i))) % 10));
    }
}

I am trying to count every other number from second from the last number using this formula. I know the "pow" function only takes floats and doubles as inputs so I changed the variable to floats. Yet, it is still giving me a

"error: invalid operands to binary expression ('double' and 'double')

printf("%lld\n", ((l / (pow(10.00, i))) % 10));"

error when I try to compile it. What can be fixed to make this work?

Thank you!

2
  • I'm curious why you chose to add an operation that would introduce floating point operations and vars (and their imprecision) into this? Instead of dividing the original number by a float, you could take advantage of the loop and divide by 10 on each pass, keeping a running track of the result, thus keeping everything in integers.
    – Cliff B
    Jan 9 '19 at 18:37
  • Originally, I was trying to shorten the function as much as possible and take care of all the necessary calculation in one step with an exponent. Found out that "pow" function only takes floats and doubles as inputs and changed my variables accordingly. I ended up overcomplicating things :(
    – K_Oh
    Jan 9 '19 at 19:39
1
test.c:35:49: error: invalid operands to binary expression ('double' and 'double')
        printf("%lld\n", ((l / (pow(10.00, i))) % 10));
                          ~~~~~~~~~~~~~~~~~~~~~ ^ ~~
1 error generated.

It's really telling you that there's a problem with the modulo operator. See where the pointer is under the % operator?

Modulo is an integer operation. It doesn't work with floats, so it fails.

Next, it depends on how you change things to see what goes wrong next. I removed the modulo operation completely and it gave a different error - the format char is for a long long, but the equation gives a double. More implicit casting.

I would suggest that you do the calculations before doing the printf statement. Further, do it in steps until it works. Breaking the steps apart will help identify where the problems lie and give you a better understanding of what works and why certain things don't work. Then, if you want, you can merge the lines of code together until you consolidate them as much as you wish.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I see. With the info, I changed the loop to " for (int i = 1; i < ((n + 1) / 2); i++) { printf("%lld\n", l % 10); l /= 100; } "and it did the trick. Although I'm still not sure if there will be any bugs due to the loop condition but so far so good. Thank you!
    – K_Oh
    Jan 9 '19 at 19:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .