Breaking out of a loop

So I'm learning about arrays and I'm having trouble breaking out of a loop that adds user input directly into the array without adding the last value to the sum. Putting a value (anything below 0)that is supposed to break out of the loop does break it from the loop but it also adds it to my sum, thus affecting my average. This is what I have. Any help you guys can provide would be greatly appreciated.


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>

using namespace std;

/*
 * 
 */
int main() {

    int sum = 0;
    int count = 0;
    int scores[100];
    int average = 0;

    
        cout << "Enter a score: ";
        cin >> scores[count];
        sum+=scores[count];
        count++;
        
    while (scores[count-1]>=0)
    {
     
        cout << "Enter a score: ";
        cin >> scores[count];
        sum+=scores[count];
        count++;
                
    }
    average = sum/count;
    cout << average;
    
   
       










    return 0;







}
Last edited on
1
2
3
4
5
6
7
8
int score = 0;
while ( count < 100 && cin >> score )
{
  if ( score < 0 ) break;
  sum += score;
  scores[count] = score;
  ++count;
}
oh wow I feel really dumb now, that was such an easy solution. Thank you so much.
Topic archived. No new replies allowed.