Skip to content

Instantly share code, notes, and snippets.

@GermanHoyos
Created January 13, 2022 01:22
Show Gist options
  • Save GermanHoyos/2971c23afdb140682d87740c16d2c123 to your computer and use it in GitHub Desktop.
Save GermanHoyos/2971c23afdb140682d87740c16d2c123 to your computer and use it in GitHub Desktop.
Program to grade scores from a file C++
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
double score(string transferAns, string transferKey);
//MAIN FUNCTION
int main() {
string lineScan;
string transferKey;
char grade = 'A';
double cScore;
double percent;
int lineCounter = 0;
ifstream readFl("Ch8_Ex6Data.txt");
while (getline(readFl, lineScan)) {
if (lineCounter < 1) {
transferKey = lineScan.substr(0, 20);
}
lineCounter++;
if (lineCounter > 1) {
string transferID = lineScan.substr(0, 8);
cout << transferID << " ";
string transferAns = lineScan.substr(9, 29);
cScore = score(transferAns, transferKey);
//calculate grade via score
percent = (cScore / 40) * 100;
if (percent >= 90) {
grade = 'A';
} else if (percent >= 80 && percent <= 89.99) {
grade = 'B';
} else if (percent >= 70 && percent <= 70.99) {
grade = 'C';
} else if (percent >= 60 && percent <= 69.99) {
grade = 'D';
} else {
grade = 'F';
}
cout << cScore << " " << grade << endl;
}
}
lineCounter = 0;
return 0;
}
double score(string transferAns, string transferKey) {
int score = 0;
int x = 0;
cout << transferAns <<
" ";
for (char c: transferAns) {
if (c == transferKey[x]) {
score = score + 2;
}
if (c != transferKey[x] && c != ' ') {
score = score - 1;
}
x++; // verified correct tabulations
}
return score;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment