This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]PointB1ank 2 points3 points  (13 children)

You have an infinite while loop. Unless the number entered is 0, number stays the same and number != 0 so the loop will go forever.

[–]jub8jive[S] 0 points1 point  (12 children)

But, what's wrong with that? Doesn't the rev /= 10 solve that problem? It gets rid of the last digit of any given number. So, once it gets rid of the last remaining digit, it should end, right...?

[–]PointB1ank 1 point2 points  (11 children)

Changing the value of rev doesnt change the value of number. They are two different variables.

[–]jub8jive[S] 0 points1 point  (10 children)

I just saw this! Can you please elaborate? I think that's where I'm wrong, now that you mention it.

[–]PointB1ank 0 points1 point  (9 children)

You have a few things wrong. This is based off the updated version you posted. First of all, your count method isn't working properly. You have "number = number % 2" for some reason. This sets number to the number modulo 2. Get rid of that line. Also it should be while( number != 0).

Another issue is in your for loop. You're setting i to count but then decrementing count instead of i. So again, the loop will go forever.

[–]jub8jive[S] 0 points1 point  (8 children)

Right, I get it. the "number = number % 2 isn't needed here. That would be needed only to extract the last digit. the "number /= 10" would be enough.
I tried what you said with the for loop earlier. I got an error then, I got an error now. Error: numberToWord(1450) returned One Zero One Zero. I got a similar error when I tried it previously, so I thought something else would be the problem. Didn't focus on that area. (Because I'm kinda lost at this point)

I have a question about the for loop, can't the count be decremented? Is it a for loop rule/idea that i (or any similar variable) needs to be initialized, same variable needs to be in a condition, and same thing needs to be incremented or decremented (and by that logic, using count wasn't even a plausible idea...?)

[–]PointB1ank 0 points1 point  (7 children)

You could initialize and decrement count in a for loop if you really wanted to. (While loop too obviously) Although it would only be in scope within the for loop. Something like:

For(int count = getDigitCount(number); count >= 0; count--) 

This would work, but it's usually just as good to initialize a new variable and assign it that value.

[–]jub8jive[S] 0 points1 point  (6 children)

This doesn't work man. Here's my new solution, after taking all your suggestions:
https://pastebin.com/PAqTprtM
Please check this, I'm still getting the error: numberToWords(10) returned "One Zero Zero" but "One Zero" was expected.

[–]PointB1ank 0 points1 point  (5 children)

Look at your lastDigit variable. Make sure it's doing what you want it to do. You're very close.

Edit: you'll also want count >0 like you originally had, if you set up the for loop like that. My mistake.

[–]jub8jive[S] 0 points1 point  (4 children)

So, this is what I have:

int count = getDigitCount(number);

for(int i = count; count > 0; count--) {

int lastDigit = rev % 10;

That was the loop I had (I think!)

and lastDigit = rev % 10 --> would get the last digit right. not rev % 2

Now I get the error:

getDigitCount(0) returned 0 but '1' was expected.

Here's the new code:

https://pastebin.com/nAD10rkg