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?

3rd Dec 2021, 11:13 PM
Segmentation Fault
Segmentation Fault - avatar
6 Answers
+ 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.
4th Dec 2021, 12:04 AM
William Owens
William Owens - avatar
+ 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
3rd Dec 2021, 11:41 PM
William Owens
William Owens - avatar
+ 1
Add a New Line "\n" printf("%d %d %d %d \n", one, two, three, four);
4th Dec 2021, 12:01 AM
SoloProg
SoloProg - avatar
+ 1
William Owens I see it now. Thank you very much.
4th Dec 2021, 12:13 AM
Segmentation Fault
Segmentation Fault - avatar
+ 1
Welcome enjoy coding in C. It's awesome.
4th Dec 2021, 12:26 AM
William Owens
William Owens - avatar
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.
3rd Dec 2021, 11:44 PM
Segmentation Fault
Segmentation Fault - avatar