I want to store the names in a one dimensional array and the numbers (test scores) in a two dimenstion array. I noticed that I will have to iterate through the test scores faster than the names, which I cant quite figure out how to do. Hers what I have:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
int main(){
constint numStudents = 5;
constint numTests = 5;
string names[numStudents];
//char grades[numStudents];
int scores[numStudents][numTests];
ifstream inFile;
inFile.open("/Users/kylemiller/Desktop/Ch9Ex11.txt");
for (int i = 0; i < numStudents; i++)
inFile >> names[i] >> scores[i][0];
for (int i = 0; i < numStudents; i++)
cout << names[i] << endl;
for (int i = 0; i < numStudents; i++)
for (int j = 0; j < numTests; j++)
cout << setw(5) << scores[i][j];
return 0;
}
The problem I am having is with the inFile. I cant set up separate loops because it will iterate completely through the name loop before getting to the score loop, and I don't think its possible to go cout << names[i] << for (blah blah blah), since you cant put a for loop there.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
string students[5];
int tests[5];
ifstream infile;
int n=0;
infile.open("names");
infile >> students[n];
while (infile)
{
cout << "Location is " << n << endl;
cout << "Student is " << students[n] << endl;
for (int i =0; i < 5; i++)
{
infile >> tests[n];
cout << "Test is " << tests[n] << endl;
}
n++;
infile >> students[n];
}
infile.close();
for (int i=0; i < n; i++)
cout << students[i] << " " << tests[i] << endl;
return 0;
}
Sample Input:
Johnson 5 6 7 1 9
Abby 5 3 9 7 1
Joe 99 5 7 8 1
Dave 1 2 3 4 5
Sample Output:
Location is 0
Student is Johnson
Test is 5
Test is 6
Test is 7
Test is 1
Test is 9
Location is 1
Student is Abby
Test is 5
Test is 3
Test is 9
Test is 7
Test is 1
Location is 2
Student is Joe
Test is 99
Test is 5
Test is 7
Test is 8
Test is 1
Location is 3
Student is Dave
Test is 1
Test is 2
Test is 3
Test is 4
Test is 5
Johnson 9
Abby 1
Joe 1
Dave 5
Sample Output Without Cout Statements:
Johnson 9
Abby 1
Joe 1
Dave 5
It seems that my test array only holds the last variable.
Not sure why.