I've been trying to get this code to give me an output of "20 9.50" when I input "15 20 0 3 -1" but it keeps giving me the output "20 0.55". This is the code I've made:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int largest = 0;
int number = 0;
int count = 0;
double avg = 0;
while (number >= 0) {
cin >> number;
if (number >= 0) {
if (number > largest) {
largest = number;
}
avg += number; ++count;
}
if (count > 0) {
avg /= count;
}
}
cout << largest << " " << fixed << setprecision(2) << avg << endl;
return 0;
}
Can anyone point out how I'm getting the wrong output? I'm in a class for C++ but I'm still new to it. Hopefully I'm not treated too harshly, any help is appreciated.