I have a little bit of an update, I got the course grade * thing working, but now have a bug in the regular part. If CS240 is entered, it doesn't output the classname or classnumber. I see the problem, count is way to high, but have to go to work, no time to fix it.
Code:
//create a menu based system that reads in 3 files; student, grades, and classes
//and allows the user to add/modify a grade, remove a grade, print a student grade report
//print a course grade report, list all students, and list all classes.
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <iomanip>
#include <ctype.h>
using namespace std;
struct student
{
string firstname;
string lastname;
int ID;
string phone;
};
struct grade
{
int ID;
string classnum;
char lettergrade;
};
struct course
{
string num;
string name;
};
//subroutine menu
//used to print out the menu choices
// returns the number that they choose
char menu()
{
char menuchoice =0;
cout << endl << endl << endl << endl
<< "1. Add/Modify Grade" << endl
<< "2. Remove Grade" << endl
<< "3. Student Grade Report" << endl
<< "4. Course Grade Report" << endl
<< "5. List Students" <<endl
<< "6. List Courses" << endl
<< "7. Exit" << endl;
cout << endl << endl << "Enter the number of what you would like to do -> ";
cin >> menuchoice;
return menuchoice;
}
//function to add/modify a grade
void add(grade grades[], course courses[], student students[])
{
int ID,counter;
string num,classnum,name;
char letter;
grade tempgrade;
bool inlist=false;
cout << "Please enter a student ID ->";
cin >> ID;
cout << endl << "Please enter a course number ->";
cin >> num;
cout << endl << "Please enter a grade ->";
cin >> letter;
toupper(letter);
//toupper(num.c_str());
if (letter==char("Q"))
{
cout << "Quitting"<< endl;
return;
}
if( (letter==char('A')) || (letter==char('B')) || (letter==char('C'))
|| (letter==char('D')) || (letter==char('E')) || (letter==char('F'))
|| (letter==char('I')) ) //valid letters
{
counter=0;
classnum=courses[counter].num;
while (courses[counter].num!="")
{
if (courses[counter].num==num)
{
inlist=true;
cout << "Course number OK" << endl;
}
counter++;
classnum=courses[counter].num;
}//end while
if(!inlist)
{
cout << "Course number not in database" << endl;
return;
}
//check if ID is valid
counter=0;
name=students[counter].firstname;
inlist=false;
while (name!="")
{
if(students[counter].ID==ID)
{
inlist=true;
cout << "Student ID OK"<< endl;
}
counter++;
name=students[counter].firstname;
}
if(!inlist)
{
cout << "Student ID not in database" << endl;
return;
}
//check if student already has a grade
tempgrade = grades[0];
counter =0;
inlist=false;
while (tempgrade.classnum!="")
{
if (grades[counter].ID == ID)
{
if(grades[counter].classnum==num) //its a match, just modify
{
grades[counter].lettergrade=letter;
cout << "Student " << ID << " in " << num
<< " has been changed to " << letter << endl;
inlist=true;
}
}
counter++;
tempgrade=grades[counter];
}
if(!inlist)//need to add to database
{
cout << "Adding Student " << ID << " to database" << endl;
grades[counter].ID = ID;
grades[counter].classnum=num;
grades[counter].lettergrade=letter;
}
}
else
{
cout << "Invalid Letter Grade" << endl;
return;
}
}
// function to remove a grade
void remove(grade grades[], course courses[], student students[])
{
int ID,counter;
string num,name,classnum;
bool inlist;
cout << "Please enter a Student ID ->";
cin >>ID;
cout << "Please enter a course number ->";
cin >> num;
//check if ID is in database
inlist=false;
counter=0;
name=students[counter].firstname;
while(name!="")
{
if (students[counter].ID==ID)
{
inlist=true;
cout <<"Student ID OK" << endl;
}
counter++;
name=students[counter].firstname;
}// end while
if(!inlist)
{
cout << "Student ID not found" << endl;
return;
}
//check course number
counter=0;
inlist=false;
classnum=courses[counter].num;
while(classnum!="")
{
if(courses[counter].num==num)
{
cout << "Course Number OK" << endl;
inlist=true;
}
counter++;
classnum=courses[counter].num;
}//end while
if(!inlist)
{
cout <<"Course Number not in database"<< endl;
return;
}
counter=0;
while(grades[counter].ID!=ID)
{
counter++;
}
while(grades[counter].classnum!="")
{
grades[counter].classnum=grades[counter+1].classnum;
grades[counter].ID=grades[counter+1].ID;
grades[counter].lettergrade=grades[counter+1].lettergrade;
counter++;
}
grades[counter-1].classnum="";//reset so loops stop right
cout << "Student " << ID << " was removed from " << num << endl;
}
// function to vew a student grade report
void studgrad(grade grades[], course courses[], student students[])
{
int ID,counter,count, counting;
string first,num,classnum;
char grade;
string input;
bool newstudent=true;
cout << "Please enter a Student ID ->";
cin >> input;
cout << setw(12) << left << "First Name";
cout << setw(12) << left << "Last Name";
cout << setw(8) << left << "ID";
cout << setw(31) << left << "Course Name";
cout << setw(7) << left << "Num";
cout << setw(1) << "Grade" << endl;
counter=0;
if (input =="*")
{//do all students
ID=students[counter].ID;
first=students[counter].firstname;
count=0;
while(first!="")
{
cout << setw(12) << left << students[counter].firstname;
cout << setw(12) << left << students[counter].lastname;
cout << setw(6) << left << students[counter].ID;
count=0;
ID=students[counter].ID;
num=grades[count].classnum;
while(num!="")
{
if(ID==grades[count].ID)
{
grade=grades[count].lettergrade;
classnum=grades[count].classnum;
counting=0;
if(!newstudent)
{
cout << endl;
cout << " ";
}//end if
while(courses[counting].name!="")
{
if(courses[counting].num==classnum)
{
cout << setw(31) << left <<courses[counting].name;
cout << setw(7) << left << courses[counting].num;
cout << setw(1) << grade;
newstudent=false;
}//end if
counting++;
}//end while
}//end if
count++;
num=grades[count].classnum;
}//end while
counter++;
first=students[counter].firstname;
newstudent=true;
cout << endl;
}//end while
}
else //output for one student
{
//validate student ID
//StringToInt(input,ID);
}
}
//function to view a course grade report
void courgrad(grade grades[], course courses[], student students[])
{
string input, num,name;
int count,counter,ID,counting;
bool inlist=false;
char grade;
cout<<"Please enter a course number ->";
cin >> input;
cout << setw(12) << left << "First Name";
cout << setw(12) << left << "Last Name";
cout << setw(8) << left << "ID";
cout << setw(31) << left << "Course Name";
cout << setw(7) << left << "Num";
cout << setw(1) << "Grade" << endl;
if(input=="*") //do all courses and students
{
count=0;
input = courses[count].num;
num=grades[count].classnum;
while(num!="")
{
if(num==input)
{
ID=grades[count].ID;
grade=grades[count].lettergrade;
num=grades[count].classnum;
counter=0;
name=students[counter].firstname;
while(name!="")
{
if(students[counter].ID==ID)
{
cout << setw(12) << left << students[counter].firstname;
cout << setw(12) << left << students[counter].lastname;
cout << setw(6) << left << ID;
counting=0;
name=courses[counting].name;
while(name!="")
{
if(courses[counting].num==num)
{
cout << setw(31) << left << courses[counting].name;
cout << setw(7) << left << num;
cout << setw(1) << left << grade << endl;
}//end if
counting++;
name=courses[counting].name;
}//end while
}
counter++;
name=students[counter].firstname;
}//end while
}//endif
count++;
num=grades[count].classnum;
input=num;
}//end while
}//endif
else //just one course
{ //check if input is valid
count=0;
num=grades[count].classnum;
while(num!="")
{
if (grades[count].classnum==input)
{
inlist=true;
}//end if
count++;
num=grades[count].classnum;
}//end while
if(!inlist)
{
cout << "Course number not found." << endl;
return;
}//end if
//start output of data
count=0;
num=grades[count].classnum;
while(num!="")
{
if(grades[count].classnum==input)
{
ID=grades[count].ID;
grade=grades[count].lettergrade;
counter=0;
name= students[counter].firstname;
while(name!="")
{
if(students[counter].ID==ID)
{
cout << setw(12) << left << students[counter].firstname;
cout << setw(12) << left << students[counter].lastname;
cout << setw(6) << left << ID;
cout << setw(31) << left << courses[count].name;
cout << setw(7) << left << courses[count].num;
cout << setw(1) << left << grade << endl;
}
counter++;
name=students[counter].firstname;
}//end while
}
count++;
num=grades[count].classnum;
}//endwhile
}//end else
}
//function to view a list of all students in a class
void liststud(student students[])
{
cout << endl << endl;
cout<<setw(12) << left << "First Name" << setw(12) << left << "Last Name";
cout<<setw(6) << left << "ID";
cout<<setw(13) <<left << "Phone" << endl;
int counter=0;
string name=students[counter].firstname;
while(name!="")
{
cout << setw(12) << left << students[counter].firstname;
cout << setw(12) << left << students[counter].lastname;
cout << setw(6) << left << students[counter].ID;
cout << setw(13) << left << students[counter].phone;
cout << endl;
counter++;
name=students[counter].firstname;
}// end while
}
// function to display all courses listed
// simple loop til the end of course array
void listcourses(course courses[])
{
cout << endl << endl << endl;
cout << setw(7) << left << "Num";
cout << setw(31) << left << "Course Name" << endl;
int counter=0;
string num=courses[counter].num;
while (num!="")
{
cout << setw(7) << left << courses[counter].num;
cout << setw(31) << left << courses[counter].name;
cout << endl;
counter++;
num=courses[counter].num;
}//end while
}
// exit function
// must save data back to file before exiting
void myexit(grade grades[], course courses[], student students[])
{
int count;
string name;
ofstream outfile;
outfile.open("student.out");
count =0;
name = students[count].firstname;
while(name!="")
{
outfile << setw(12)<< left<< students[count].firstname
<< setw(12) << left << students[count].lastname
<< setw(6) << left << students[count].ID
<< setw(13) << left << students[count].phone << endl;
count++;
name=students[count].firstname;
}//end while
outfile.close();
outfile.open("course.out");
count =0;
name=courses[count].num;
while(name!="")
{
outfile << setw(7) << left << courses[count].num
<< setw(31) << left << courses[count].name << endl;
count++;
name=courses[count].num;
}// end while
outfile.close();
outfile.open("grade.out");
count =0;
name=grades[count].classnum;
while(name!="")
{
outfile << setw(6) << left << grades[count].ID
<< setw(7) << left << grades[count].classnum
<< setw(1) << left << grades[count].lettergrade << endl;
count++;
name=grades[count].classnum;
}//endwhile
outfile.close();
cout << "Files written." << endl;
}
void readdata(char* argv[],grade grades[], course courses[], student students[])
{
string first, last, phone;
int ID,counter;
char letter;
ifstream infile1 (argv[1]);
infile1 >> first >> first >>first >>first; // clear the first line
counter=0;
while (!infile1.eof())
{
infile1 >> first >> last >> ID >> phone;
students[counter].firstname=first;
students[counter].lastname = last;
students[counter].ID = ID;
students[counter].phone=phone;
counter++;
}//end while
infile1.close();
ifstream infile2 (argv[2]);
infile2 >> first >> first;
counter=0;
while (!infile2.eof())
{
infile2 >> first;
getline(infile2,last);
courses[counter].name=last;
courses[counter].num=first;
counter++;
};//end while
infile2.close();
ifstream infile3 (argv[3]);
getline(infile3,first);
//infile3 >>ID >> first >> first;
counter=0;
while(!infile3.eof())
{
infile3 >> ID >> first >> letter;
grades[counter].ID=ID;
grades[counter].classnum=first;
grades[counter].lettergrade=letter;
counter++;
};//end while
infile3.close();
}
int main(int argc,char* argv[])
{
char item;
string first,last,phone;
student students[1000];
grade grades[10000];
course courses[9999];
readdata(argv,grades,courses,students);
while(item!=7)
{
item = menu();
if(item==int('A'))
item=1;
else if(item==int('a'))
item=1;
else if(item==int('R'))
item=2;
else if(item==int('r'))
item=2;
else if(item==int('S'))
item=3;
else if(item==int('s'))
item=3;
else if(item==int('C'))
item=4;
else if(item==int('c'))
item=4;
else if(item==int('L'))
item=5;
else if(item==int('l'))
item=5;
else if(item==int('D'))
item=6;
else if(item==int('d'))
item=6;
else if(item ==int('E'))
item=7;
else if(item ==int('e'))
item=7;
if (item =='1')
add(grades, courses, students);
else if (item =='2')
remove(grades, courses, students);
else if (item =='3')
studgrad(grades, courses, students);
else if (item =='4')
courgrad(grades, courses, students);
else if (item =='5')
liststud(students);
else if (item =='6')
listcourses(courses);
};//end while
myexit(grades, courses, students);
return 0;
}//end main