I am having trouble with the first for Loop. It only executes once. I can't figure out what I did wrong :( I also can't get the student names to display either. I really appreciate the help.
#include <iostream>
#include <string> //For student names
usingnamespace std;
//Define Arrays and Variables
constint STUDENT = 5; //Number of students
constint TEST = 4; //Number of tests per student
string name[STUDENT]; //Name of students
char grade[STUDENT]; //Student grades
double score[TEST]; //Student test scores
int count; //Counter variable
double sum; //Sum of all test scores
double avg; //Average test score
int main( )
{
//Prompt for information input
for(count = 0; count < STUDENT; count++)
{
cout << "Enter student name: ";
cin >> name[count];
cout << endl;
for(count = 0; count < TEST; count ++)
{
cout << "Enter score for test #" << (count+1) << " :";
cin >> score[count];
cout << endl;
if(score[count] < 0 || score[count] > 100)
{
cout << "Invalid score. Please enter a score between 0-100: ";
cin >> score[count];
cout << endl;
}
//Find the lowest test score of the student
double lowest = 0; //Lowest score
if(score[count] < lowest)
score[count] = lowest;
//Drop the lowest test score and find the average score
sum += score[count];
avg = (sum - lowest) / 3; // Calculate average score
}
//Display student grades
if(avg <= 100 && avg >= 90)
{
cout << "\n\n" << name[count] <<"'s average score is " << avg
<< " which is an A for the class." << endl;
}
elseif(avg <= 89 && avg >= 80)
{
cout << "\n\n" << name[count] <<"'s average score is " << avg
<< " which is a B for the class." << endl;
}
elseif(avg <= 79 && avg >= 70)
{
cout << "\n\n" << name[count] <<"'s average score is " << avg
<< " which is a c for the class." << endl;
}
elseif(avg <= 69 && avg >= 60)
{
cout << "\n\n" << name[count] <<"'s average score is " << avg
<< " which is a D for the class." << endl;
}
elseif(avg <= 59 && avg >= 0)
{
cout << "\n\n" << name[count] <<"'s average score is " << avg
<< " which is an F for the class." << endl;
}
}
//Hold result on screen
cin.ignore( );
cout << "\n\nPress Enter to exit the program.";
cin.get( );
return 0;
}
in two places for the same loop. So when count is updated for the TEST part, the student name part is already fulfilled, so the loops end. Use a different variable name for one of the counts.
1. DON'T use the same variable for the 2 for loops because when the second finishes count = 4 then the first finish count = 5, so it just go 1 time.
2.if(score[count] < lowest) is missing {} after it and even if it had it wouldn't work because you set lowest = 0 and since you don't accept negative grades NOTHING will be less than it.
I've made 2 adjustments accordingly by changing one of the variables and setting lowest = score[0]. It is looping 5 times now but the display grades part at the end only shows up during the first execution. I don't understand why it is doing that...
#include <iostream>
#include <string> //For student names
usingnamespace std;
//Define Arrays and Variables
constint STUDENT = 5; //Number of students
constint TEST = 4; //Number of tests per student
string name[STUDENT]; //Name of students
char grade[STUDENT]; //Student grades
double score[TEST]; //Student test scores
int count, index; //Counter variables
double lowest = score[0]; //Lowest score
double sum; //Sum of all test scores
double avg; //Average test score
int main( )
{
//Prompt for information input
for(count = 0; count < STUDENT; count++)
{
cout << "Enter student name: ";
cin >> name[count];
cout << endl;
for(int index = 0; index < TEST; index++)
{
cout << "Enter score for test #" << (index+1) << " :";
cin >> score[index];
cout << endl;
if(score[index] < 0 || score[index] > 100)
{
cout << "Invalid score. Please enter a score between 0-100: ";
cin >> score[index];
cout << endl;
}
//Find the lowest test score of the student
if(score[index] < lowest)
lowest = score[index];
sum += score[index];
//Drop the lowest test score and find the average score
avg = (sum - lowest) / 3; // Calculate average score
}
//Display student grades
if(avg <= 100 && avg >= 90)
{
cout << "\n" << name[count] <<"'s average score is " << avg
<< " which is an A for the class." << endl;
}
elseif(avg <= 89 && avg >= 80)
{
cout << "\n" << name[count] <<"'s average score is " << avg
<< " which is a B for the class." << endl;
}
elseif(avg <= 79 && avg >= 70)
{
cout << "\n" << name[count] <<"'s average score is " << avg
<< " which is a C for the class." << endl;
}
elseif(avg <= 69 && avg >= 60)
{
cout << "\n" << name[count] <<"'s average score is " << avg
<< " which is a D for the class." << endl;
}
elseif(avg <= 59 && avg >= 0)
{
cout << "\n" << name[count] <<"'s average score is " << avg
<< " which is an F for the class." << endl;
}
cout << endl << endl;
}
//Hold result on screen
cin.ignore( );
cout << "\n\nPress Enter to exit the program.";
cin.get( );
return 0;
}