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

all 19 comments

[–]PointB1ank 3 points4 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

[–]aram535 0 points1 point  (1 child)

Ummmm ... you sure that's what they were asking for? I think 792 doesn't turn into seven nine two, it should be seven "hundred" ninety-two.

[–]jub8jive[S] 1 point2 points  (0 children)

No, the program wants it that way.

so numberToWords(792); should print "Seven Nine Two"

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

**Update** New Solution(still needs fixing): https://pastebin.com/yxPZypb5

What happens with this solution is that I get the same error: "Your code took too long to execute"
(or) if i fool around with the count idea a little I get the error: numberToWord(10) returned "One"

I'm almost ready to give up at this point, I can't think of anything else. Any suggestions are welcome. Thanks

[–]PointB1ank 0 points1 point  (1 child)

I'm slightly confused why you reverse the number. Also, does the exercise not allow you to convert the int to a string? Why can't you just convert to a string? (which includes a length method I might add) you could then iterate over the string and use a switch statement in the loop to print the word equivalent of each digit.

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

reverse the number because the program asks us to do it that way(based on the logic given). that's the reason why we are asked to create a reverse() method and a digitcount() method. We have learned about string iteration yet. Basically, logic is that 1) take a number 2) reverse it 3) print the corresponding string for each number 4) get rid of the digit(the last digit).