Roman to integer | Sololearn: Learn to code for FREE!

0

Roman to integer

https://code.sololearn.com/c9BqjLY8ui5I/?ref=app I write my code on my way, but it doesn't go correctly. Does anybody knows how to solve it? Sometimes it goes right, something not. It doesn't considerate the last one or two letters.

c++

12/27/2021 7:03:46 AM

TeaserCode

10 Answers

New Answer

+3

This I think should work šŸ™‚ https://code.sololearn.com/c7jJfPI2nuWb

+1

You can salvage most of your code the following way: 1) Make dig1 one element larger and set dig1[count]=0. The reason is that your loop either includes one element too many or drops the last one. By adding one element more and setting it to 0, it is involved in the comparison, and will always be smaller than its predecessor. 2) Have the loop run while "i<count", to process all digits 3) Lose the "visited" flag and its "if". Instead, replace "continue" with "x++" to increase x to two digits having been processed. 4) fix the brackets after removing the "if(visited)" Now, it should work šŸ™‚

+1

I use bool statement because I noticed during interation if next letter is bigger than current then bigger is used twice. First at the substraction with current and second when it is itereted normally. So bool skip the second way.

+1

Agnes, I have done as you said. But it is not possible to initialize variable-sized object. Nothing happens, last letter is still not in account. Can you make corrections?

+1

But it write down the right size of array when I print count. I consider the size when I am copying the numbers into array in switch statement. Seems strange.

0

I 'm going to test it if it is really possible. Thanks.

0

It will be, I'm sure šŸ™‚šŸ€ Incidentally, the "visited" flag is not necessary, because there are only two logical paths. And "count[x] < count[x+1]" is the same as "visited". So, no need for the boolean flag. As a matter of fact, it is one of the problems you have now, because you forgot to reset the flag to "false". And the "continue" statement has you skip the output - so, that needs to go, too.

0

Yes, but you can skip already when checking the digits for their numerical value šŸ™‚

0

Just noticed, you did not specify the correct size for count. I have added that now.

0

The array does not adjust its size later. It is sized the moment it is created, and at that time, count is zero.