In: Computer Science
Create a C++ program which will accept an unlimited number of
scores
and calculates the average score. You will also need to prompt for
the
total points possible. Assume each test is worth 100 points.
Requirements.
• Enter the Student ID first, then prompt for grades. Keep
prompt-
ing for grades until the client enters ’calc’. That triggers final
pro-
cessing.
• Make your code as reliable as possible.
• Make your program output easy to read.
• You cannot use anything from the standard template library.
• Use functions whenever possible to modularize your code.
Use
function prototypes and code the functions under the main().
• Style guide elements apply comments, layout, Program
Greeting,
Source File Header, and variables, etc. etc.
1. // Specification A1 - OOP
Code this assignment using at least one class. Put this
comment
above the class declaration.
2. // Specification A2 - Sort Grades
Sort the grades before printing them under specification C2.
High
to low. Use any sort you wish, but code your own sort.
3. // Specification A3 - Logfile
Log the grades to a text file for archival purposes.
4. // Specification A3 - <Description>
Replace A3 with one feature of your own choosing. You can
code
A3 as it appears if you wish and skip this.
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
// Specification A1 - OOP
class Scores{
int max_score;
int *scores;
int tot_scores;
int sid;
public:
Scores(int max){
max_score = max;
scores = new int(max_score);
tot_scores = 0;
}
void getScores(){
string inp;
int x;
cout << "Enter student id: ";
cin >> sid;
for(tot_scores = 0; tot_scores < max_score; tot_scores++){
cout << "Enter score " << tot_scores + 1 << ": "
;
cin >> inp;
if(inp.compare("calc") == 0){
break;
}
char char_array[inp.length()];
strcpy(char_array, inp.c_str());
sscanf (char_array,"%d",&x);
scores[tot_scores] = x;
}
}
// Specification A2 - Sort Grades
void sortScores(){
int i,j,x;
for(i = 0; i < tot_scores - 1; i++){
for(j = i + 1; j < tot_scores; j++){
if(scores[i] < scores[j]){
x = scores[j];
scores[j] = scores[i];
scores[i] = x;
}
}
}
}
void displayScores(){
int sum = 0;
cout << "Student ID: " << sid << endl;
for(int i = 0; i < tot_scores; i++){
cout << "score " << i + 1 << ": " <<
scores[i] << endl;
sum += scores[i];
}
cout << "Average Score : " << sum / tot_scores <<
endl;
}
// Specification A3 - Logfile
void archiveScores(){
ofstream myfile;
myfile.open ("archive.txt");
myfile << "Student ID: " << sid << endl;
for(int i = 0; i < tot_scores; i++){
myfile << "score " << i + 1 << ": " <<
scores[i] << endl;
}
myfile.close();
}
};
int main()
{
int max;
cout << "Enter possible total scores:";
cin >> max;
Scores sc(max);
sc.getScores();
sc.sortScores();
sc.displayScores();
sc.archiveScores();
return 0;
}