Creating a function with For loop for highest and lowest number

Hello, I'm new to C++ and programming and I must create the following program:
it must be able to tell which number is the lowest, which is the highest (best and worst grade) and what's the average value from an undefined number of values that the user enters. I must do this as a function with a For loop though and not inside the int main (). Here's what I've tried to do so far. What do I need to change to make it work? Any help is 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
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;

string subjectAndGradeEntry ()
{
    bool forward = true;
    string name;
    char answer = ' ';
    float grade = 0;
    vector<string> names;
    vector<float> grades;
    while (forward){
    cout << "Please enter the name of the subject." << endl;
    cin >> name;
    cout << "Please enter your grade." << endl;
        names.push_back(name);
        cin >> grade;
        grades.push_back(grade);
        cout << "One more subject? [y/n]" << endl;
        cin >> answer;
        if (answer == 'n')
        forward = false;
        else if (answer == 'y')
        forward = true;
        }
    return name;
}

float calculateWorstGrade ()
{
    int count = 0;
    float grade = 0;
    float worst_grade=0;
    for (int i = 0; i < count; ++i)
        {

             if (grade > worst_grade)
                 worst_grade = grade;
             cout <<worst_grade<< " is your worst grade." << endl;
        }
    return worst_grade;

}

int main ()
{

    int count;
    float grade;
    float worst_grade;

    count = calculateWorstGrade ();
    grade = calculateWorstGrade ();
    worst_grade = calculateWorstGrade ();

    subjectAndGradeEntry ();

    return 0;
}
Last edited on
As a starter it might be better if you entered the student information before you try to calculate anything.

What do you mean?
I mean where are you asking the user to enter the data (calling subjectAndGradeEntry())?

How can you compute something if you have no valid data available?

Yes, the user has to enter his data there and the calculation will be based on the entered data.
On line 59 you seek to calculate worst grade with the data you have previously input on line ... err ...
On line 60 you again seek to calculate worst grade with the data you previously input on ... oh hang it.
On line 61 you yet again seek to calculate worst grade with ... oh forget it.

And then on line 63 you decide to enter some data.

And when you have fixed the triplication of finding the worst grade with non-existent data, consider how the actual data gets transferred (or, at present, not transferred) between functions.
Topic archived. No new replies allowed.