C++ while loop please help! I can't run it properly. I'm also new to programming. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C++ while loop please help! I can't run it properly. I'm also new to programming.

The task is to write a program that inputs 5 integer and print the smallest value using while loop. https://code.sololearn.com/cqS5zuuP8oMx/?ref=app https://code.sololearn.com/cqS5zuuP8oMx/?ref=app

18th Mar 2021, 8:27 PM
Jessica Zarate
Jessica Zarate - avatar
8 Answers
+ 2
Jayakrishna🇮🇳 :D not as fast as you could think... I've just see this thread while refreshing most recent posts list. I've first passed as Jessica Zarate doesn't post its code, then new refresh, coming to see what was posted and see code... I've hesitate a few, then studying code and finally posted only 5 minutes before you ;)
18th Mar 2021, 9:05 PM
visph
visph - avatar
+ 1
you use num for input, and you increment it as it was a counter (but you reset value at each iteration)... you must at least use a separate counter variable initialized to zero, and check for it in your while loop condition instead of num (and obviously increment it rather than num).
18th Mar 2021, 8:37 PM
visph
visph - avatar
+ 1
you must also test for counter<max instead of <= or initialize counter at one instead if zero...
18th Mar 2021, 8:41 PM
visph
visph - avatar
+ 1
Thank you for the help visph ! I'll make them separate.
18th Mar 2021, 8:42 PM
Jessica Zarate
Jessica Zarate - avatar
+ 1
num= stoi(input); num++; Here, if i enter 5 or greater number then by num++, loop stops here because num<=5 becomes false. You shloud use other variable for counter here. For ex: count++;
18th Mar 2021, 8:43 PM
Jayakrishna 🇮🇳
+ 1
Thank you too for pointing that out sir Jayakrishna🇮🇳
18th Mar 2021, 8:46 PM
Jessica Zarate
Jessica Zarate - avatar
+ 1
@visph is fast. its already answered. I didn't see. Jessica Zarate You're welcome..
18th Mar 2021, 8:51 PM
Jayakrishna 🇮🇳
+ 1
visph still you are 1st.. I mean only ,it is answered already before me. Upon refresh after my post i seen it. If i see it before, i didn't post. It seems duplicate now..
18th Mar 2021, 9:25 PM
Jayakrishna 🇮🇳