problem solving with passing variables values

I need help with my problem solving. My code, a sentinel controlled while loop, should;
-get a max score possible
-take student score input and display score, percentage, letterGrade. For each input.
After sentinel input it should display a collective set of result.

My code works but my numbers come out wrong.

I cant: store the max and min input until loop terminates.
If someone can help with this part I will get the rest of the calculation going.

Thank you
go easy on the noob


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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  #include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
const int sentinel = -999;

const double aPlus = 97;
const double a = 90;
const double aMinus = 89;
const double bPlus = 87;
const double b = 80;
const double bMinus = 79;
const double cPlus = 77;
const double c = 70;
const double cMinus = 69;
const double dPlus = 67;
const double d = 60;
const double dMinus = 59;
const double f =58.9;

int main()
{
    // Variables initialized
    int score;
    //int count =0;
    int aCount =0;
    int bCount =0;
    int cCount =0;
    int dCount =0;
    int fCount =0;
    int avgPct =0;
    int avgScore =0;
    int totalScore =0;
    int hScore =0;
    int lScore =0;
    int Score =0;

    int pct =0;
    char ltGrade =0;
    int stuCount =0;
    int maxi =0;

    //Output and input
    cout << "Enter Maximum class score" << "\n";
    cin >> maxi;

    cout << "Enter score, enter -999 to display result: " << "\n";
    cin >> score;
    
    

    while (score != sentinel)
    {
        cout <<"\n" <<"score is: " << score <<endl;
        Score = score+0; //Score = score?
        cout <<"\n" <<"dbug Score is: " << Score <<endl;
        cout <<"\n" <<"dbug score is: " << score <<endl;
        // student Count
        stuCount++;
        
        {// total score calculation
            totalScore = totalScore + score;
            cout <<"\n" << "dbug this is total score: " << totalScore << endl;
        }
        // maximum and minimum calculation ***I CANT GET THIS PART TO WORK AS //AS I COULDN'T GET THE VALUE OF SCORE INPUT PASSED ON HERE FOR COPMARISON                       
        {
            hScore = score;
            cout <<"\n" <<" dbug hScore is:" << hScore <<endl;
            lScore = score;
            cout <<"\n" <<"dbug lScore is: " <<lScore <<endl;
            if (Score > hScore)
            {hScore = Score;}
            cout <<"\n" <<"dbug hScore is: " <<hScore <<endl;
            if (lScore < Score)
            {lScore = Score;}
            cout <<"\n" <<"dbug lScore is: " <<lScore <<'\n'<<'\n' ;
        }
        // percentage calculation
        pct = (score*100)/maxi;
        
        // letter grade determination
        {
        if(pct >= aPlus)
        {cout << "The student's grade is: A+ \n";}
        else if(pct >= a)
        {cout << "The student's grade is: A  \n";}
        else if(pct >= aMinus)
        {cout << "The student's grade is: A- \n";}
        
        else if(pct >= bPlus)
        {cout << "The student's grade is: B+ \n";}
        else if(pct >= b)
        {cout << "The student's grade is: B  \n";}
        else if(pct >= bMinus)
        {cout << "The student's grade is: B- \n";}
            
        else if(pct >= cPlus)
        {cout << "The student's grade is: C+ \n";}
        else if(pct >= c)
        {cout << "The student's grade is: C  \n";}
        else if(pct >= cMinus)
        {cout << "The student's grade is: C- \n";}
            
        else if(pct >= dPlus)
        {cout << "The student's grade is: D+ \n";}
        else if(pct >= d)
        {cout << "The student's grade is: D  \n";}
        else if(pct >= dMinus)
        {cout << "The student's grade is: D- \n";}
        else {cout << "The student's grade is: F \n";}
        }
        
        // letter grade count code block
        {
            if (pct >= aMinus)
            {aCount++;}
            else if (pct <= c)
            {fCount++;}
            else if (pct < aMinus && pct > cPlus)
            {bCount++;}
            else if (pct < bMinus && pct > dPlus)
            {cCount++;}
            else if (pct < cMinus && pct> f)
            {dCount++;}
        }
        
        
        //output for each individual student
        cout <<"\n" << "Student's score is: " <<score << endl;
        cout <<"\n" << "The percentge is: " << pct << endl;
        cout <<"\n" << "Enter another score or -999 for result: ";
        cin >> score;
        
    }
    
    cout << "Hello lol" << endl;


    cout << "Maximum Score = " << maxi << endl;
    cout << "Total number of student is: " << stuCount << endl;
    cout << "The highest score is: " << hScore << endl;
    cout << "The lowest score is: " << lScore << endl;
    cout << "The average score is: " << avgScore << endl;
    cout << "The average percentage is: " << avgPct << endl;
    cout << "There are " << aCount << " students with A" << endl;
    cout << "There are " << bCount << " students with B" << endl;
    cout << "There are " << cCount << " students with C" << endl;
    cout << "There are " << dCount << " students with D" << endl;
    cout << "There are " << fCount << " students with F" << endl;

    return 0;
}
closed account (D80DSL3A)
I see a couple of things wrong.
Lines 68 and 70
1
2
3
 hScore = score;

 lScore = score;

These appear inside the while loop, where they will be assigned every time a new score is entered.
These are the initial values you want for them only. These assignments belong outside of (and before) the while loop, right after the very 1st score is entered. Move these two lines to lines 50, 51.

Line 75 logic is backwards. You have:
if (lScore < Score)
It should be:
if (lScore > Score)// then Score is lower, becoming the new low score

I don't see why you have 2 variables for score (score and Score), but at least you equate them Score = score+0; //Score = score? so hopefully it's ok.

Does this fix the problem?
fun2code
Thanks for the assist.
I followed your suggestion. It didn't fix it be it helped me get to right place.
There was also a problem with my 'if' statement. I had the statement and condition reversed. here is the fix below.
The high and low updates now.

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

  cout << "Enter score, enter -999 to display result: " << "\n";
    cin >> score;
    hScore = score;
    lScore = score;
    //score = Score;



    while (score != sentinel)
    {
        cout <<"\n" <<"score is: " << score <<endl;
        // student Count
        stuCount++;

        {// total score calculation
            totalScore = totalScore + score;
                    }
        // maximum and minimum calculation 
        {

            if (score > hScore)
            {hScore = score;}
            else if (score < lScore)
            {lScore = score;}
            cout <<"\n" <<"dbug hScore is: " <<hScore <<endl;
            cout <<"\n" <<"dbug lScore is: " <<lScore <<'\n'<<'\n' ;
        }
Topic archived. No new replies allowed.