a while loop for counting in the average of the total numbers.

I have to keep track of the numbers whose total is less than 100. And I don't want to count the last number that makes the total over 100. Here is my code. Thanks in advance.


#include <iostream>

using namespace std;

main() {
int num;
int total = 0;
int average;
int nums;
cout << "Enter a number: ";
cin >> num;
while (total + num <= 100) {
total += num;

cout << "Enter a number: ";
cin >> num;
}

while (total < 100){
average = total/nums;
}
cout << "The average of the " << num << "is" << average << endl;
}
total + num <= 100

replace by

true

and inside the loop

1
2
3
4
5
6
if(total+num<=100)
{
     total+=num;
} else {
     break;
}


Not very beautiful, but the other ways to do it (e.g. substracting the last num after the loop) aren't either.
thanks!
Topic archived. No new replies allowed.