Help with Loop & string display

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>  //For student names
using namespace std;

//Define Arrays and Variables
const int STUDENT = 5;	//Number of students
const int 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;
		}
		else if(avg <= 89 && avg >= 80)
		{
			cout << "\n\n" << name[count] <<"'s average score is " << avg
			<< " which is a B for the class." << endl;
		}
		else if(avg <= 79 && avg >= 70)
		{
			cout << "\n\n" << name[count] <<"'s average score is " << avg
			<< " which is a c for the class." << endl;
		}	
		else if(avg <= 69 && avg >= 60)
		{
			cout << "\n\n" << name[count] <<"'s average score is " << avg
			<< " which is a D for the class." << endl;
		}
		else if(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;
}
You're using the variable
count
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.
Well this is what I found...

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.
Thanks for the reply!

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...
It would be better if you put the code again so we don't have to guess what you did exactly...
Will do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>  //For student names
using namespace std;

//Define Arrays and Variables
const int STUDENT = 5;	//Number of students
const int 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;
		}
		else if(avg <= 89 && avg >= 80)
		{
			cout << "\n" << name[count] <<"'s average score is " << avg
			<< " which is a B for the class." << endl;
		}
		else if(avg <= 79 && avg >= 70)
		{
			cout << "\n" << name[count] <<"'s average score is " << avg
			<< " which is a C for the class." << endl;
		}	
		else if(avg <= 69 && avg >= 60)
		{
			cout << "\n" << name[count] <<"'s average score is " << avg
			<< " which is a D for the class." << endl;
		}
		else if(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;
}
Topic archived. No new replies allowed.