tocanna
asked on
Trouble using for loop with an array as well as reading in values.
For some reason I can't get this program to work. It has something to do with how I have setup the for loop nested inside that is designed to read in 5 integer values. After it gets done reading in the 5 values I get a logic error that prints out like this:
Enter Name: Enter Class:
Instead of just having it like it was the first time it runs through:
Enter Name: (Name entered)
Enter Class: (Class entered)
and so on...
Here is the code for the program so far:
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
char Name[5][20];
char Class[5][20];
char Teacher[5][20];
int Grades[5];
void main()
{
system("cls");
cout << "This program creates a sheet of test results for 5 students." << endl << endl;
for( int i = 0; i < 5; i++ )
{
cout << "Enter Name: ";
cin.getline( Name[i],20 );
cout << "Enter Class: ";
cin.getline( Class[i],20 );
cout << "Enter Teacher: ";
cin.getline( Teacher[i],20 );
for( int j = 0; j < 5; j++ )
{
cout << "Enter Grade: ";
cin >> Grades[j];
}
}
system("pause");
} // end of main()
Enter Name: Enter Class:
Instead of just having it like it was the first time it runs through:
Enter Name: (Name entered)
Enter Class: (Class entered)
and so on...
Here is the code for the program so far:
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
char Name[5][20];
char Class[5][20];
char Teacher[5][20];
int Grades[5];
void main()
{
system("cls");
cout << "This program creates a sheet of test results for 5 students." << endl << endl;
for( int i = 0; i < 5; i++ )
{
cout << "Enter Name: ";
cin.getline( Name[i],20 );
cout << "Enter Class: ";
cin.getline( Class[i],20 );
cout << "Enter Teacher: ";
cin.getline( Teacher[i],20 );
for( int j = 0; j < 5; j++ )
{
cout << "Enter Grade: ";
cin >> Grades[j];
}
}
system("pause");
} // end of main()
ASKER
Thanks but we are not storing American style grades (IE: A-F), we are storing 5 integer values (IE: 90, 100), 75, etc. These are the headers our teacher has had us use and we also have not been using the 'using namespace std;' Any more help would be greatly appreciated.
Then you should be fine entering integer values with your code... *puzzled*
try using flushall() before getting input
in each loop of "i" you overwrite the grades of the previous loop (student)
Did you mean to have :
int Grades[5][5];
ie. 5 grades per student ?
If so, also change this line :
cin >> Grades[j];
to :
cin >> Grades[i][j];
to read the j-th grade of the i-th student.
int Grades[5][5];
ie. 5 grades per student ?
If so, also change this line :
cin >> Grades[j];
to :
cin >> Grades[i][j];
to read the j-th grade of the i-th student.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
tried changing int Grades[5] to in Grades[5][5];
and cin >> Grades[i][j];
also did the fix alex posted, still getting the same problem. It runs through once and soon as I enter the fifth grade for the first student I see this:
Enter Name: Enter Class:
Then it seems to work normally (allows me to enter the class, teacher and 5 grades) but just skips the input name.
and cin >> Grades[i][j];
also did the fix alex posted, still getting the same problem. It runs through once and soon as I enter the fifth grade for the first student I see this:
Enter Name: Enter Class:
Then it seems to work normally (allows me to enter the class, teacher and 5 grades) but just skips the input name.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks for putting it in code, Alex ;)
ASKER
Thanks again guys, you helped me out a ton. Thanks for putting up with my lack of knowledge, I am just starting out.
>>>> Thanks for putting it in code, Alex ;)
You may not believe me, but I made the code after I read tocanna's comment that it still doesn't work and posted it *before* I read your comment. It is one more time where you and me have identical solutions ... ;-)
You may not believe me, but I made the code after I read tocanna's comment that it still doesn't work and posted it *before* I read your comment. It is one more time where you and me have identical solutions ... ;-)
>> You may not believe me
That's not what I meant ... I just didn't have the time to adapt and test the code ...
>> It is one more time where you and me have identical solutions ... ;-)
Just shows that the solution is good eh :)
That's not what I meant ... I just didn't have the time to adapt and test the code ...
>> It is one more time where you and me have identical solutions ... ;-)
Just shows that the solution is good eh :)
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string>
#include <math.h>
using namespace std;
char Name[5][20];
char Class[5][20];
char Teacher[5][20];
string Grades[5];
void main()
{
system("cls");
cout << "This program creates a sheet of test results for 5 students." << endl << endl;
for( int i = 0; i < 5; i++ )
{
cout << "Enter Name: ";
cin.getline( Name[i],20 );
cout << "Enter Class: ";
cin.getline( Class[i],20 );
cout << "Enter Teacher: ";
cin.getline( Teacher[i],20 );
for( int j = 0; j < 5; j++ )
{
cout << "Enter Grade: ";
cin >> Grades[j];
}
}
system("pause");
}
BTW, you were using deprecated header files also.