+ 1

Why does this not work?

I know, this is a bad question, but I have no idea what I have done wrong. I tried to make a C code to that Collatz Conjecture-thing, but it doesn't work and I have no idea why. Here's the code: https://code.sololearn.com/cfDKYhrjhb9X/?ref=app It's btw my first C code, if we don't count some testing with input / output...

27th Mar 2019, 6:56 AM
Joel Kronqvist
Joel Kronqvist - avatar
4 Answers
+ 5
Hey, that's not how scanf works. What you're doing is, you're applying atoi on what is returned by scanf. Scanf returns the number of inputs read and assigned successfully. And in this case, you're not giving reference to any variable inside the scanf in which the input value would be stored and hence scanf will return 0. And thus, your i will have a value 0 and hence won't go inside while loop. You should do it like: int i; scanf("%d", &i);
27th Mar 2019, 7:27 AM
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½ - avatar
+ 3
Coincidentally, your in-/output was the issue. The logic was correct. Here is how it should look: https://code.sololearn.com/cpif2RfhHkL4/?ref=app You might want to look at printf and scanf again, then you'll see the mistakes by yourself.
27th Mar 2019, 7:19 AM
Shadow
Shadow - avatar
+ 2
Thank you, Shadow and Shashi Ranjan As I said, this was practically my first C code, so thanks for the help.
27th Mar 2019, 7:56 AM
Joel Kronqvist
Joel Kronqvist - avatar
28th Mar 2019, 7:00 AM
Joel Kronqvist
Joel Kronqvist - avatar