0
bug in while loop
https://code.sololearn.com/cwTYOeaXEdP7/?ref=app The code above has a printNumberOfDigits function that takes a number, hard coded as 1234 as an argument and outputs how many 1s, 2s, 3s and 4s there are in the number 1234. It then prints the result which should be '1 1 1 1', but this is not the case. Instead what is being printed is '1 1 1 13' which is wrong. Can anyone please take a look at the code and tell me what I'm doing wrong?
6 odpowiedzi
+ 1
I don't see the seg fault however the 1 1 1 13 is the output of 1 as one 1 as two 1 as three 1 as four, and the 3 is due to the fact that number was divided by 10 prior to the while statement so three is your count variable.
+ 2
I think you going at this in a difficult way. Take a look at this code. I think it does what you want. then make it your own.
https://code.sololearn.com/c3WDZaisfDo9/?ref=app
+ 1
Add a New Line "\n"
printf("%d %d %d %d \n", one, two, three, four);
+ 1
William Owens I see it now. Thank you very much.
+ 1
Welcome enjoy coding in C. It's awesome.
0
William Owens sigh... I know but the reason is because I have no knowledge of how arrays in C work so I chose the hard way. But still I would like to know why the function prints out an incorrect result.