1
/* 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

Oh God
  • 47
  • 4
  • 1
    The comment says _"i tried but hv error,idk why"_ - what did you try and what error did you get? Do you really want to stop the calculation on`-1`, or do you want stop reading input when `-1` is entered? What didn't work with the obvious `if(v[i] == -1) break;`? – Lukas-T Oct 08 '22 at 11:51
  • I want to stop reading input when -1 is entered, --> Reading Input Until -1 – Oh God Oct 08 '22 at 11:54
  • Why do you want that? You already have a much better way to stop. – n. m. could be an AI Oct 08 '22 at 11:56
  • See [this answer](https://stackoverflow.com/a/73891662/12002570). Also refer to a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Oct 08 '22 at 13:29

2 Answers2

4

Preferred method

To read input until -1, you should use different approach then getline. Because getline reads characters in string until delimeter or new line character is found. You can use something like this:

int x = 0;
do {
  cin >> x;
} while (x != -1);

Alternative

You can check it in your while loop at split function. If you see a number -1 you will break your loop.

I can't check if it is working but seems fine.

vector<float> split(const string line, char delimiter) {
  stringstream ss(line);
  string item;
  vector<float> numbers;
  while (getline(ss, item, delimiter)) {
    if (stoi(item) == -1) break; 
    numbers.push_back(stoi(item));
  }
  return numbers;
}
erenalyoruk
  • 121
  • 4
0

You can do it in avgNumbers function. Just add break inside for loop

if (v[i] == -1)
    break;

you also must count no of numbers before -1 to calculate avg, so put declaration of for loop iterator outside. BTW the iterator is always integer so don't use float

int nOfNums;
    for (nOfNums = 0; nOfNums < v.size(); nOfNums++)

Whole function can look like:

float avgNumbers(vector<float> v) {
    float sum = 0;
    int nOfNums;
    for (nOfNums = 0; nOfNums < v.size(); nOfNums++) {
        if (v[nOfNums] == -1)
            break;
        sum += v[nOfNums];
    }
    return sum / nOfNums;
}
Vazun
  • 1
  • 1