Homework assignment [Help]

Was wondering how
-I can add function to get the student name with the a string
-add a function to assign a letter grade called from Main then the letter grade passing to displayResults function
-and making a loop, so asking the user if they want to use the program again for the next student.

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
#include <iostream>
#include <string>
using namespace std;

// 2. Function prototypes
void displayResults(string name, float avg); //satisfies the complier
int getTestScore();
float calcAvg(int t1, int t2, int t3);
bool isValid(int num, int min, int max);

int main()
{
    string fullName;
    int test1, test2, test3;
    float average;
    
    // get student full name
    cout << "Student Name:  ";
    getline(cin, fullName);
    
    // get 3 test scores
    test1 = getTestScore(); // function call to value returning function
    test2 = getTestScore();
    test3 = getTestScore();
    
    // average calculation
    average = calcAvg( test1, test2, test3);
    
    //assign a letter grade, using a char
    
    
    //diplay heading, name and average
    displayResults(fullName, average); // function call for a void function
    
    system("pause");
    return 0;
}

    // 1. Function definiton
    // ======================================================================
    // displayResults outputs the heading and the results of the calculations
    // ======================================================================
    void displayResults(string name, float avg) // function header, scope
    {
        cout << "------------------------------------------------\n";
        cout << "Norco College \n";
        cout << " CIS-5: Intro to Programming with C++  \n";
        cout << "Student Averages \n";
        cout << "-------------------------------------------------\n";
  
        cout << name << ": " << avg << endl;
    }
        //getTestScore prompts user and gets a single test score
        int getTestScore()
        {
            int score; // scope of the operator
            int min = 0, max = 100;
            
            cout << "Enter a test score:       ";
            cin >> score;
            
            while(isValid(score, min, max)) // While score is not valid
            {
                cout << "Try again. Enter a valid test score between 0-100:   ";
            }
                
            return score; //returns a valid test score
        }

            //calcAvg totals scores and calculate the average
            float calcAvg(int t1, int t2, int t3)
            {
                float avg;
        
                avg = (t1 + t2 + t3) / 3.0;
        
                return avg;
            }
                // isValid test for valid input and returns true or false
                bool isValid(int num, int min, int max)
                {
                    if (num < min || num > max)
                        return false;
                    else
                        return true;
                }
            

-I can add function to get the student name with the a string
Seems rather silly, since it's just a line of code, but:
1
2
3
4
cout << "Student Name:  ";
string fullName = getStudentName();

// ... 


1
2
3
4
5
6
7
string getStudentName()
{
    // get student full name
    string fullName;
    getline(cin, fullName);
    return fullName;
}


Also,
1
2
3
4
            while(isValid(score, min, max)) // While score is not valid
            {
               // ...
            }

Presumably, you meant
while(!isValid(score, min, max)) // While score is not valid


I suggest reading: http://www.cplusplus.com/doc/tutorial/functions/

As it says in your prompt, you need to modify displayResults to take in a letter grade as a parameter (i.e. a char parameter). Show us an attempt in doing this.

and making a loop, so asking the user if they want to use the program again for the next student.
Basically, move all the logic you have in main inside a while loop. But add a little if statement at the end depending on what the user's response asking for the next student is.

e.g.
1
2
3
4
5
6
7
8
9
10
while (true)
{
    // ... existing main code ...

    cout << "Enter another student (y/n)? ";
    char yesno;
    cin >> yesno;
    if (yesno != 'y' && yesno != 'Y')
        break; // stop looping
}
Last edited on
Topic archived. No new replies allowed.