A big tangled mess

ok I need some help. I'm doing this programming project where I'm supposed to get 5 students names, then the test scores they made on 4 tests, average them together and pop out a letter grade for each student.

I get through the first for loop, assigning the names for everyone just fine.

But when I get to the second for loop where I have to assign the grades to each student... it gives me the first student's name and lets me input his scores... and then it gives me the second student name and lets me input his scores....

but then it just keeps giving me the second students name and keeps making me re-put in his scores... it doesn't progress past that!

heres my code:


// Andrew Butler
// Programming Assignment 2
// Grade book
// averages 4 test grades for 5 students

#include "stdafx.h"

#include <iostream>

using namespace std;

char letterGrade (double avg);

int _tmain(int argc, _TCHAR* argv[])
{

char studentName[5][50];
char lGrade[5];
int student1[4], student2[4], student3[4], student4[4], student5[4];
double average1 = 0.00, average2 = 0.00, average3 = 0.00, average4 = 0.00, average5 = 0.00; // holding places for the student averages
double average;
int i;

for (i = 1; i <= 5; i++)
{
cout << "Please enter student " << i << "'s name: ";
cin.getline (studentName[i], 50);

}

for (i = 1; i <= 5; i++)
{
cout << "Please enter the 4 grades " << studentName[i] << " made: ";
for (int x = 0; x <=3; x++)
{
if (i = 1) // determines which array to use
{
cin >> student1[x]; // allows input into the first of the 5 arrays
while (student1[x] < 0 || student1[x] > 100) // checks to see if input is correct
{
cout << "Error, must be between 0 and 100: ";
cin >> student1[x];
}
average1 += student1[x]; // adds up all the test scores and stores them for later
}
else if (i = 2)
{
cin >> student2[x];
while (student2[x] < 0 || student2[x] > 100)
{
cout << "Error, must be between 0 and 100: ";
cin >> student2[x];
}
average2 += student2[x];
}
else if (i = 3)
{
cin >> student3[x];
while (student3[x] < 0 || student3[x] > 100)
{
cout << "Error, must be between 0 and 100: ";
cin >> student3[x];
}
average3 += student3[x];
}
else if (i = 4)
{
cin >> student4[x];
while (student4[x] < 0 || student4[x] > 100)
{
cout << "Error, must be between 0 and 100: ";
cin >> student4[x];
}
average4 += student4[x];
}
else
{
cin >> student5[x];
while (student5[x] < 0 || student5[x] > 100)
{
cout << "Error, must be between 0 and 100: ";
cin >> student5[x];
}
average5 += student5[x];
}
}
}


for (i = 0; i <=4; i++)
{
if (i = 0)
{
average = average1 / 4;
lGrade[i] = letterGrade (average);
}
else if (i = 1)
{
average = average2 / 4;
lGrade[i] = letterGrade (average);
}
else if (i = 2)
{
average = average3 / 4;
lGrade[i] = letterGrade (average);
}
else if (i = 3)
{
average = average4 / 4;
lGrade[i] = letterGrade (average);
}
else
{
average = average5 / 4;
lGrade[i] = letterGrade (average);
}
}




for (i = 1; i <= 5; i++)
{
cout << studentName[i] << " has a " << lGrade[i] << endl;
}
return 0;
}

char letterGrade (double avg)
{
if (avg >= 90)
return 'A';
else if (avg >= 80)
return 'B';
else if (avg >= 70)
return 'C';
else if (avg >= 60)
return 'D';
else
return 'F';
}


Let me explain a few things....

I know using a 2 dimensional array for the names is kinda stupid but thats what the teacher wants.

Also using a 2 dimensional array to organize the students grades and who's grades they are would also be a pretty smart idea.... but the teacher specifically said to use 1 dimensional arrays there..

Hopefully someone can read through my mess of a code and help me bug the problem.

thanks a million.
Saw this appear quite a few times:
if (i = 1) // determines which array to use
This assigns 1 to i, as '=' is the assignment operator. You want the comparison operator '=='. That's what causes your inifinite loop, because it keeps resetting to i = 1!
By the way, the entire code would be a LOT cleaner if you made the averageX's an array as well:

1
2
3
4
for (int i = 0; i <= 4; ++i) {
    average = averages[i]/4;
    lGrade[i] = letterGrade(average);
}
god... I knew it was something stupidly simple.... thanks mate
Mark the asnwer as solved. Button located at top under " Tangled Mess"! Pls. as it takes us to quest. that are already solved but still present on the board.
Topic archived. No new replies allowed.