Dec 4, 2018 at 10:04pm UTC
How can I exit the lopp using -1, before I had the second while loop I was able to exit the loop using -1 but now it doesn't exit it just keep asking for another number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
using namespace std;
int main()
{
int number,
total = 0,
counter = 1,
even = 0;
while (true ) {
cout << "Enter number " << counter << ": " ;
cin >> number;
int remainder = number % 2;
if (remainder == 0)
even++;
while (number != -1)
{
total = total + number;
counter++;
cout << "Enter number " << counter << ": " ;
cin >> number;
}
}
Last edited on Dec 4, 2018 at 10:08pm UTC
Dec 4, 2018 at 11:10pm UTC
Hello Rocketboy,
the outer while loop at line 12 is an endless loop whereas the while loop at line 26 checks for "-1".
At line 15 use an if statement to check for "-1". If true then (break;) out of the loop.
Hope that helps,
Andy