Functions lab

Need help to see what I did wrong and/or what am I missing

Object:
Write a program to compute the final score and the letter grade of students in a class of 5 or more students.
Each student has the following:
- An ID number(positive integer value); a dummy ID number of -99 is used to indicate the end of the input data.
- The exam score and 7 test scores. The exam score and the test scores are real values in the range 0.00 to 100.00. The final test score is computed as: 4/10(average test score)+6/10(exam score).

Input: For each student, its ID number, its exam score, and 7 test scores. The last ID number must be the dummy student ID number -99 with no exam or test scores.

1) Write the function void readTestScores (double &exam, double &tavge) that does the following:
- print the prompt: "enter exam score:" than read the exam score
- print the prmpt: "enter all test scores:" then read 7 test scores in a loop, and than computer their average
- the exam score and the average of the test scores are returned to the calling function using the reference parameters exam and tavge respectively

2) Write the function double computeFinalScore(double exam, double tavge) that gets a student's exam score and its test scores average using the value parameters exam and tavge respectively, computes the final score as follows, 4.0/10(test scores average)+6.0/10(exam score) and returns it to the calling function.

3) Write the function char getLetterGrade(double score) that gets a student's final score using the value parameter score, determines the corresponding letter grades, and returns it to the calling function. The letter grade is determined as follows:
of:
score >=90 A
80 <= score<90 B
70 <= score<80 C
60 <= score<70 D
score<60 F

4) Write the function void printComment(char grade) that gets a student's letter grade and prints the corresponding comment as follows: COMMENT: <comment>
the <comment> is determined as follows:

A very good
B good
C satisfactory
D need improvement
F poor

This is what I did so far:
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
#include <iostream>
using namespace std;

int main()
{
    
    void readTestScores(double&exam, double&tavge);
    double computeFinalScore(double exam, double tavge);
    char computeGrade(double score);
    void printComment(char grade);

    double exam; // a student's average exam score
    double tavge; // a student's average test score
    
    /*read the student's exam scores and compute their average*/
    cout << endl << "Enter Student ID:\t";
    cout << endl << "Enter exam score:\t";
    
    /*read the student's test score and compute their average*/
    cout << endl << "Enter all test scores:\t";
    
    /*compute and print the student's average score*/
    cout << endl << "The average score is:\t" << (.4 * tavge + .6 * exam);
    int score;
   
    
         cout << "\n Enter your marks = ";
         cin  >> score;
        
         char computeGrade(int val);
         {
              char symb;
              if(val>=90)
                   symb='A';
         
              else if(val>=80)
                   symb='B';
         
              else if(val>=70)
                   symb='C';
         
              else if(val>=60)
                   symb='D';
         
              else
                  symb='F';
                  return(symb);
                  }
         
         cout<<"\n Your Grade is = ";
         
         system("pause");
         return 0;
}
Last edited on
Try this:

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
#include <iostream>

#define NO_OF_TESTS 7

void readTestScores (double &exam, double &tavge)
{  
  double test_score[NO_OF_TESTS], total = 0;
  
  /* read the student's exam scores and compute their average */
  std::cout << std::endl << "Enter exam score: ";
  std::cin >> exam;

  /* read the student's test score and compute their average */
  unsigned int i = 0;
  while(i < NO_OF_TESTS)
  {
    std::cout << "Enter test " << i+1 << " score: ";
    std::cin >> test_score[i];
    total += test_score[i];
    i++;
  }

  tavge = total / NO_OF_TESTS;
}

double computeFinalScore (double exam, double tavge)
{
  double final_score;
  final_score = (.4 * tavge) + (.6 * exam);
  return final_score;
}

char computeGrade(double score)
{
  char grade;
  if (score >= 90) grade = 'A';
  else if (score >= 80) grade = 'B';
  else if (score >= 70) grade = 'C';
  else if (score >= 60) grade = 'D';
  else grade = 'F';
  return(grade);
}

void printComment(char grade)
{
  switch(grade)
  {
    case 'A':
      std::cout << std::endl << "Very Good";
      break;
    case 'B':
      std::cout << std::endl << "Good";
      break;
    case 'C':
      std::cout << std::endl << "Satisfactory";
      break;
    case 'D':
      std::cout << std::endl << "Need improvement";
      break;
    case 'F':
      std::cout << std::endl << "Poor";
      break;
  };
}

int main()
{
  unsigned int id; // student's id.
  double exam; // a student's average exam score
  double tavge; // a student's average test score
  double final_score;
  char grade;

  while(1)
  {  
    std::cout << std::endl << "Enter student id. Enter -99 to exit: ";
    std::cin >> id;
    
    if(id == -99)
      return 0;

    readTestScores(exam, tavge);
    final_score = computeFinalScore(exam, tavge);
    grade = computeGrade(final_score);
    printComment(grade);
  }
  return 0;
}
We didn't learn some of these things so I changed it around.

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
/******************************************************************************/
/*                              LAB ASSIGNMENT #7                             */
/* Program to compute the final score and the letter grade of students in a
class of 5 or more students.                                                  */
/* Programmer: James Chapple                                                  */
/* Class: CS 2300-60                                                          */
/* Date: April 2, 2014                                                        */
/* Due Date: April 9, 2014                                                    */
/******************************************************************************/

#include <iostream>
#define MAXCOUNT 7
using namespace std;

void readTestScores(double &exam, double &tavge)
{
     double testscore(MAXCOUNT), total = 0;
     
     /* read the student's exam scores and compute their average */
     cout << endl << "Enter exam score: ";
     cin >> exam;
     
     /* read the student's test score and compute their average */
     cout << "Enter all test scores: ";
     cin >> testscore;
}

double computeFinalScore(double exam, double tavge)
{
       double finalscore;
       finalscore = (.4 * tavge) + (.6 * exam);
       return finalscore;
}

char getLetterGrade(double score)
{
     char grade;
     if (score >= 90) grade = 'A';
     else if (score >=80) grade = 'B';
     else if (score >= 70) grade = 'C';
     else if (score >= 60) grade = 'D';
     else grade = 'F';
     return(grade);
}

void printComment(char grade)
{
     switch(grade)
     {
                  case 'A':
                       cout << endl << "Very Good";
                       break;
                  case 'B':
                       cout << endl << "Good";
                       break;
                  case 'C':
                       cout << endl << "Satisfactory";
                       break;
                  case 'D':
                       cout << endl << "Need Improvement";
                       break;
                  case 'F':
                       cout << endl << "Poor";\
                       break;
     };
}

int main()
{
    int id; // student's id.
    double exam; // a student's average exam score
    double tavge; // a student's average test score
    double finalscore; 
    char grade;
    
    while(1)
    {
            cout << endl << "Enter Student ID: Enter -99 to exit: ";
            cin >> id;
            
            if(id == -99)
            return 0;
            
            readTestScores(exam, tavge);
            finalscore = computeFinalScore(exam, tavge);
            grade = getLetterGrade(finalscore);
            printComment(grade);
            
    }
    return 0;
}
    
                       
     


The only thing that came from the output was this:

Enter Student ID: Enter -99 to exit.

Why is that and how can I fix it so the output can look like this for example:

Enter Student ID:
Enter Exam Score:
Enter All Test Scores:
Test Average Is:
Final Score Is:
Letter Grade Is:B
Comment:

Enter Student ID: -99

@17 double testscore(MAXCOUNT)

This should be

double testscore[MAXCOUNT]

====

1
2
@24 cout << "Enter all test scores: ";
@25 cin >> testscore;

Here only one testscore is getting stored. Whereas the program logic is to get and store 7 testscores. A while loop suits it. Something like:

1
2
3
4
5
6
7
8
  unsigned int i = 0;
  while(i < NO_OF_TESTS)
  {
    std::cout << "Enter test " << i+1 << " score: ";
    std::cin >> test_score[i];
    total += test_score[i];
    i++;
  }

Incase array and while should not be used, make use of 7 variables. Something like this:

1
2
3
4
5
6
7
8
9
10
double ts1, ts2, ts3, ts4, ts5, ts6, ts7;
cout >> "Enter testscore 1: "; cin >> ts1;
cout >> "Enter testscore 2: "; cin >> ts2;
cout >> "Enter testscore 3: "; cin >> ts3;
cout >> "Enter testscore 4: "; cin >> ts4;
cout >> "Enter testscore 5: "; cin >> ts5;
cout >> "Enter testscore 6: "; cin >> ts6;
cout >> "Enter testscore 7: "; cin >> ts7;
total = ts1 + ts2 + ts3 + ts4 + ts5 + ts6 + ts7;
tavge = total / NO_OF_TESTS;


====

1
2
@85 finalscore = computeFinalScore(exam, tavge);
@86 grade = getLetterGrade(finalscore);

Between line 85 and 86 insert the output statement to print out the average test score that was computed and returned by (reference) computeFinalScore function.

cout << "Test Average Is: " << tavge;

After that insert another cout statement to output final score that was computed and returned by computeFinalScore function.

cout << "Final Score Is: " << finalscore;

====

1
2
@86 grade = getLetterGrade(finalscore);
@87 printComment(grade);

To print the letter grade insert a cout statement between lines 86 and 87. Letter grade is returned by getLetterGrade function call at line 86.

cout << "Letter Grade Is: " << grade;

====
Topic archived. No new replies allowed.