Simple while QA | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Simple while QA

How can I modify this code to make it accept 4 entries <= 5? int num = 1; int number; while (num <= 5) { cin >> num; num++; }

10th Nov 2018, 7:54 AM
Mohanad Methkal
7 Answers
+ 3
Inside the body of the loop, after getting the user's input, add this block if (number == 5) break; Happy ending all around! 8D
10th Nov 2018, 8:44 AM
Babak
Babak - avatar
+ 6
Remove = in the while.. It should help.. 😅 while(num<5)
10th Nov 2018, 8:03 AM
$hardul B
$hardul B - avatar
+ 3
Any chance you wrongly put cin >> num; instead of cin >> number; in the body of the loop?
10th Nov 2018, 8:21 AM
Babak
Babak - avatar
+ 1
No, maybe you do not understood me. if I run this code or even I remove = mark , the user stills can enter many numbers less than 5. But what I need is make this code allows to user to input just 5 entries ( if it was < 5 )
10th Nov 2018, 8:06 AM
Mohanad Methkal
+ 1
yes, thank you soldier :)
10th Nov 2018, 8:49 AM
Mohanad Methkal
+ 1
int count = 0; int number; for (int i = 0; i < 4; i++) { cin >> number; if (number == 5) { break; } if (number <= 5) { count++; }
6th Jul 2023, 7:31 AM
Penyo Penev
Penyo Penev - avatar
0
Then, how to make this code end when the user input (5) at the first time?
10th Nov 2018, 8:37 AM
Mohanad Methkal