getting min number

okay so with my code shown below everything seems to be working fine exept for my minimum number, i have to find what the minimum number is depending on the input , but i have to use -1 to end the program, therefore my min is being set to -1 , how do i work around that ? thanks in advance
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 #include <iostream>
#include <cmath>
using namespace std;

int main() {

    cout << "Score Tally App\n\n";

    string a;
    string b;
    string c;
    string d;
    string f;

    double numscores = 0;
    double sum = 1; // taking into account the -1 that must be input to end program
    double high = 0;
    double low = 0;


    for (int i = 0; i >= 0; numscores++) {

        cout << "Enter a score(%) or -1 to end: " ;
        cin >> i;



        sum += i;

        if ( i >= 90) {

            a += "*";
        } else if ( i < 90 && i >= 80) {

            b += "*";
        } else if ( i < 80 && i >= 70) {

            c += "*";
        } else if ( i < 70 && i >= 60) {

            d += "*";
        } else if ( i < 60 && i != -1) {

            f += "*";
        }


        if ( high < i ) {
            high = i ;
        } else if ( low > i ) {
            low = i ;

        }

    }

    numscores -= 1; // taking into account the -1 that must be input to end program

    cout << "\n\nChart of Scores: \n";
    cout << "A's: " <<  a << "\n";
    cout << "B's: " <<  b << "\n";
    cout << "C's: " <<  c << "\n";
    cout << "D's: " <<  d << "\n";
    cout << "F's: " <<  f << "\n\n";

    cout << "Summary Statistics:\n";
    cout << "Number of scores: " << numscores << endl;
    cout << "Average of score: " << sum / numscores << endl;
    cout << "Highest score : " << high << endl;
    cout << "Lowest score : " << low;

    return 0;

}
I'd recommend storing the scores in a std::vector.
1
2
3
4
5
std::vector<int> scores;
int s{};
while( std::cin >> s && s != -1 ) {
    scores.push_back( s );
}

You can also write the loop like so
1
2
3
for( int s{}; std::cin >> s && s != -1; ) {
    scores.push_back( s );
}

The only difference is that the variable s is restricted to the scope of the for loop, instead of the main scope.
Last edited on
Topic archived. No new replies allowed.