I got it up and running. With EXTREMELY sloppy code. But it works so i don't really care lol. My teacher did assign an extra credit part to this assignment which is in the CreateDatabase() function. Basically he wants us to print an error if the user tries to enter the same name twice. Ex) Someone enters Rob 100 as names grades then tries entering Rob 84 in names grades. Now keep in mind this is case sensitive so Rob and rob are unique. However I know this involves more str(etc.) usage and I suck with that! Cryston halp me! Other than that extra credit this works.
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX_NUM_STUDENTS = 5;
const int MAX_NAME_LENGTH = 32;
void createDatabase();
void viewDatabase();
void modifyGrade();
void displayMenu();
bool loadDatabase(char names,double grades, int& size);
void writeDatabase(char names,double grades,int size);
int main()
{
bool continueProcessing = true;
char userSelection=' ';
while (continueProcessing)
{
displayMenu();
cin >> userSelection;
cin.ignore();
switch (userSelection)
{
case 'c':
case 'C':
createDatabase();
break;
case 'v':
case 'V':
viewDatabase();
break;
case 'm':
case 'M':
modifyGrade();
break;
case 'q':
case 'Q':
continueProcessing = false;
break;
default:
cout << "\nPlease enter a valid response.\n";
}
}
cout << "\n\nGoodbye";
}
void displayMenu()
{
cout << "###################################################\n";
cout << "# #\n";
cout << "# COURSE DB v1.0 #\n";
cout << "# #\n";
cout << "###################################################\n\n";
cout << "Please select from one of the following menu options:\n\n";
cout << "(C)reate a new database\n";
cout << "(V)iew the current database\n";
cout << "(M)odify a student's grade\n";
cout << "(Q)uit\n";
cout << "> ";
}
void createDatabase()
{
char names;
double grades;
int i = 0;
cout << endl;
cout << "Enter DONE 0 when finished" << endl;
while(i < MAX_NUM_STUDENTS)
{
cout << "Enter student " << (i+1) <<"'s name and grade: ";
cin >> names;
cin >> grades;
if(grades < 0 || grades > 100)
{
cout << "You entered an invalid grade. Please try again." << endl;
}
else if(strcmp(names, "DONE") == 0 || strcmp(names, "done") == 0 || strcmp(names, "Done") == 0)
{
break;
}
else
{
i++;
}
}
writeDatabase(names,grades, i);
}
void writeDatabase(char names,double grades,int size)
{
int count = 0;
ofstream outputFile;
outputFile.open("config.txt");
while(count < size)
{
outputFile << names << " " << fixed << setprecision(2) << grades << endl;
count++;
}
outputFile.close();
cout << endl;
}
void viewDatabase()
{
char names;
double grades;
int size = 0;
bool result = loadDatabase(names, grades, size);
int count = 0;
double sum = 0;
double average = 0;
if(!result)
{
cout << endl;
cout << "Error reading database file..." << endl;
cout << endl;
cin.get();
}
else
{
while(count < size)
{
cout << names << " " << fixed << setprecision(2) << grades << endl;
sum += grades;
count++;
}
average = sum / size;
cout << endl;
cout << " The average grade in the database is: " << fixed << setprecision(2) << average << endl;
cout << endl;
cin.get();
}
}
void modifyGrade()
{
char names;
double grades;
int size = 0;
bool result = loadDatabase(names, grades, size);
int count = 0;
char tempName;
double tempGrade = 0;
bool found = false;
int i = 0;
bool validGrade = true;
bool rewrite = false;
if(!result)
{
cout << endl;
cout << "Error reading database file..." << endl;
cout << endl;
cin.get();
}
else
{
while(count < size)
{
cout << names << " " << fixed << setprecision(2) << grades << endl;
count++;
}
while(validGrade)
{
cout << endl;
cout << "Enter the name of the student to modify and their new grade: ";
cin >> tempName;
cin >> tempGrade;
if(tempGrade < 0 || tempGrade > 100)
{
cout << "You entered an invalid grade." << endl;
cout << endl;
}
else
{
validGrade = false;
}
}
while(i < size && !found)
{
if(strcmp(names, tempName) == 0)
{
found = true;
grades = tempGrade;
rewrite = true;
}
else
{
i++;
}
}
if(!found)
{
cout << endl;
cout << endl;
cout << "The student's name was not found!" << endl;
cout << endl;
cin.get();
cin.get();
}
}
if(rewrite)
{
writeDatabase(names,grades, size);
cout << "Grade successfully updated!" << endl;
cin.get();
cin.get();
}
}
bool loadDatabase(char names,double grades, int& size)
{
bool result = true;
int counter = 0;
ifstream dbFile;
dbFile.open("config.txt");
if (!dbFile)
{
result = false;
}
else
{
while ( !dbFile.eof() && counter < MAX_NUM_STUDENTS)
{
dbFile >> names >> grades;
counter++;
}
if (dbFile.eof())
{
counter--;
}
}
size = counter;
dbFile.close();
return result;
}