Skip to content

Instantly share code, notes, and snippets.

@austenstrine
Last active February 10, 2017 21:05
Show Gist options
  • Save austenstrine/3c5e2675d9ce395ade430168bc37ce3b to your computer and use it in GitHub Desktop.
Save austenstrine/3c5e2675d9ce395ade430168bc37ce3b to your computer and use it in GitHub Desktop.
A program to help teachers average grades
//The purpose of the original assignment was to create a program that would
//allow teachers to input three test scores, and get the average of
//those three. I've altered it quite a bit, and the current version includes
//my first attempt at vectors (just taught myself how to use them today.)
//Planning on eventually making the averages be stored/retreivable.
//Also note that I follow the whitespace convention of indenting
//loops and if statements in entirety, and again for the body.
#include <iostream>
#include <iomanip>
#include <vector>
//#include <typeinfo>
//#include <cmath>
//#include <string>
using namespace std;
double getTestScore (int tck);
void getSubScript (int &sub);
void numOfStudentsQry (int &sentinel, int &pass);
void averagesYesOrNo(char &ynb);
void noFail ();
int main()
{
int sent = 0;
int firstPass = 0;
numOfStudentsQry (sent,firstPass); //asks how many students teacher'll be grading - same var as where sentValue can be declared.
vector<double>averages(firstPass, 0.0);
vector<string>names(firstPass, "");
while (sent > 0)//primary loop
{
string stuName = " ";
cout << endl << "Enter student name/ID: ";
getline (cin,stuName); //prompt for student name to tag average score/w
names.erase(names.begin()+(sent-1));
names.insert(names.begin()+(sent-1), stuName);
int subScr = 0;
getSubScript (subScr);//asks user for cin >> of subscript variable
if (subScr < 0)//if sentValue has been called, end program
{
sent = -1;
break;
}
vector<double> score (subScr, 0.0);
//score.reserve(subScr);
//score.resize(subScr, 0.0);
for (int tick = subScr-1; tick > -1; tick -= 1)//define values for vector
{
score.erase(score.begin()+tick);
score.insert(score.begin()+tick, getTestScore(tick)) ;//asks for a cinput for each vector data location
if (score.at(tick) < 0)//if sentValue has been called, end program
{
sent = -1;
break;
}//end if
}//end for
double sum = 0.0;
int tack = subScr;
for (tack = subScr-1; tack > -1; tack -= 1)//gets sum of all data locations in the vector
{
sum += score.at(tack);
}
double avg = sum/subScr;//calculates average, assigns to last spot in the array
cout << " Average score for " << names.at(sent-1) << ", ID #" << sent << ": " << fixed << setprecision(1) << avg << endl;
averages.erase(averages.begin()+(sent-1));
averages.insert(averages.begin()+(sent-1), avg);
sent -= 1;
if (sent == 0)//checks for normal end of loop, lets user view averages
{
char yN = ' ';
averagesYesOrNo(yN);//asks if they would like to view the averages they just calc'ed
if (yN == 'Y')
{ int eenie = firstPass;
for (; eenie > 0; eenie -= 1)
{
cout << names.at(eenie-1) << ", ID #" << eenie << ": " << averages.at(eenie-1) << endl;
}//end for
}//end inner if
}//end outer if
int meenie = firstPass;
numOfStudentsQry(sent, firstPass);//asks if they would like to use the program more if program would end normally
if (meenie != firstPass && firstPass > 0)//checks if new session has begun, so resets/sized vectors
{
averages.resize(firstPass, 0.0);
names.resize(firstPass, "");
}
} //end while
return 0;
}
double getTestScore (int tck)
{
double score = 0.0001;
while(score > 0.00001 && score < 0.0002)
{
cout << endl << "Enter score #" << tck+1 << ": ";
cin >> score; noFail();
}//end while
return score;
}
void getSubScript (int &sub)
{
while (sub == 0)//while loop ensures correct input, allows for sValue
{
cout << endl << "Enter the # of test scores to average: ";
cin >> sub; noFail();
}//end while
}
void numOfStudentsQry (int &sentinel, int &pass)
{
while (sentinel == 0)//chance to edit sentine value
{
cout << endl << "Enter # of students you'll be grading (any -integer ends): ";
cin >> sentinel; noFail();//define sentinel value for while
pass = sentinel;
}//end while
}
void averagesYesOrNo(char &ynb)
{
while (ynb != 'Y' && ynb != 'N')
{
cout << endl << "Would you like to view the averages for this session? (y/n)";
cin >> ynb; noFail();
ynb = toupper(ynb);
cout << endl;
}
}
void noFail ()
{
std::cin.clear();
std::cin.ignore(65536,'\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment