What if the user entered values less than or more than 5 times ?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What if the user entered values less than or more than 5 times ??

In the code below the user is supposed to enter values 5 times so that the code runs, but if ( the case above ) ^^ is True, What happens ? int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << total << endl; }

3rd Mar 2018, 10:50 AM
Nacer Benyoub
Nacer Benyoub - avatar
6 Answers
+ 4
If the user enters 5 values(smaller or bigger doesn't matters), the following code will return their sum. If the user entered only one value. Then because it is a loop statement it will take that value again and again. For Example:- If the input is 5. It will print 25.
3rd Mar 2018, 10:59 AM
Akash Pal
Akash Pal - avatar
+ 4
If users enters a values less than 5. For example:- 5 and 10. It will go for a loop and first will add 5 to total and then 10. After that it will add the last entered number again till the loop ends. Therefore the following input will print 45.
3rd Mar 2018, 11:04 AM
Akash Pal
Akash Pal - avatar
+ 4
Did you understand @Nacer?
3rd Mar 2018, 11:06 AM
Akash Pal
Akash Pal - avatar
+ 1
So it will take it as the user entered the value 5 five times and gives 25 as the sum ...
3rd Mar 2018, 11:04 AM
Nacer Benyoub
Nacer Benyoub - avatar
+ 1
aaah !! Got it ! Thanks a lot Akash Pal !! really helpful !
3rd Mar 2018, 11:09 AM
Nacer Benyoub
Nacer Benyoub - avatar
+ 1
The user can't put more then 5 numbers or less then 5 because that's how the code is written. Don't use the SoloLearn compiler it creates confusion get a desktop one
3rd Mar 2018, 12:29 PM
Marco
Marco - avatar