Okay, I borrowed (stole?) code from another post on here and modified it slightly. What I'm trying to do is input a student's name until the user types "Quit". After each entry I want it to display the student's name, average, and letter grade. Obviously my code isn't there yet...I am getting pieces right at a time. My main question at the moment is that after I type in "Quit" the program thanks the word quit is a student and asks for test scores and assigns an average and letter grade to it.
I know this program has a long way to go...Like I said, I'm piecing it together here, and I'm trying to fix problems as I run in to them. Below is what I have.
#include <iostream>
#include <string>
usingnamespace std;
// Declare structs
struct GradeBook{
string name; // Name of student
int* tests; // Pointer to an array of test scores
double average; // Average of test scores
char grade; // Grade for the course
};
// Function Prototype(s)
int inputStudents(); // Asks user for number of students
int inputTests(); // Asks user for number of test scores
int inputName(); // Asks uer for each student's name
int *inputScores(int); // Asks user for the test scores for each student
double calcAverage(int*, int); // Calculates the average test score for each student
char calcGrade(double); // Calculates the letter grade for each student based on their average
void display(GradeBook*, int); // Displays each student's name, ID#, average test score, and course grade
int counter = 0; //counter for number of students
string name;
int main(){
// Create a dynamic array of the GradeBook structure. Array size is based upon user input given in
// the inputStudents function.
GradeBook *studentList;
int size = inputStudents();
studentList = new GradeBook[size];
// Check for possible memory allocation errors. If error is found, end program.
if (studentList == NULL){
cout << "Memory Allocation Error!";
system("Pause");
return 0;
}
// Create a variable array to hold the number of test scores for each student.
int numOfTests = 3; //inputTests();
for (int count = 0; count < size; count++){
//studentList[count].name = inputName();
studentList[count].tests = inputScores(numOfTests);
studentList[count].average = calcAverage(studentList[count].tests, numOfTests);
studentList[count].grade = calcGrade(studentList[count].average);
}
display(studentList, size);
delete [] studentList;
system ("Pause");
return 0;
}
int inputStudents(){
int students;
//cout << "How many students are there?" << endl;
//cin >> students;
counter = inputName();
students = counter;
return students;
}
/*int inputTests(){
int tests;
cout << "How many tests will there be?" << endl;
cin >> tests;
return tests;
}*/
int inputName(){
//string name;
//counter = 0;
if (name == "Quit")
return counter;
elsewhile (name != "Quit")
{
cout << "Enter the student's name: "; //sldkjf;sklafjskl;jfjkl
cin >> name;
++counter;
}
return counter;
}
int *inputScores(int numOfTests){
int *scores;
scores = newint[numOfTests];
cout << "Enter the test scores for the student. Press ENTER after each score." << endl;
for (int count = 0; count < numOfTests; count++){
cout << "Score " << (count + 1) << ": ";
cin >> scores[count];
}
return scores;
}
double calcAverage(int *testScores, int numOfTests){
int total = 0;
double average;
for (int count = 0; count < numOfTests; count++){
total += testScores[count];
}
average = total/numOfTests;
return average;
}
char calcGrade(double average){
char letterGrade;
if (average > 90 && average <= 100)
letterGrade = 'A';
elseif (average > 80 && average <= 90)
letterGrade = 'B';
elseif (average > 70 && average <= 80)
letterGrade = 'C';
elseif (average > 60 && average <= 70)
letterGrade = 'D';
elseif (average >= 0 && average <= 60)
letterGrade = 'F';
else{
cout << "Logic error." << endl;
// system("pause");
exit(1);
}
return letterGrade;
}
void display(GradeBook *studentList, int size){
for (int count = 0; count < size; count++)
cout << studentList[count].name << " " << " "
<< studentList[count].average << " " << studentList[count].grade << endl;
return;
}
Just for starters the name can't be an int. it should be a string. Also, you need it to be quit or Quit. You need to compare the string.. quit or Quit. The name must be a string.
1 2 3 4 5 6 7 8
if (name == "Quit || quit)
return counter;
else while (name != Quit || quit)
cout<< Enter name: << THE variable <<"n\";
cin>> THE variable;
{