/* this is a c++ program that cal the avg */
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<float> split(const string line, char delimiter) {
stringstream ss(line);
string item;
vector<float> numbers;
while (getline(ss, item, delimiter)) {
numbers.push_back(stoi(item));
}
return numbers;
}
**/*what can i do if i want to break the user calculation if the user input is -1?*/**
float avgNumbers(vector<float> v) {
float sum = 0;
for (float i = 0; i < v.size(); i++) {
*/*revise the 'for' statment to if ==-1,break, i tried but hv error,idk why? */*
sum += v[i];
}
float idk=sum;
idk=sum/v.size();
return idk;
}
int main() {
string s_numbers;
getline(cin, s_numbers);
vector<float> numbers = split(s_numbers, ' ');
cout << "Avg is: " << avgNumbers(numbers) << "\n";
}
/*i have tried the while/do loop many times, but it does not work, idk if i make some errors, so i decided to remove that statement to a for loop since this one has no bugs*/
i expect:
input: 5,5,5,-1
output: 5
Now:
input: 5,5,5, -1
output: 3.5
I want to find a way for my code to detect -1 and to break/stop calculating when the user input is -1