read two input files at the same time

Here is my homework.(Write a program to read 10 name from and input file, each name has 5 grades (from another input file)
now display and print them to an output file. Send input files, source code, and output file for grading

i got a problem to read names, grades from two different input files and store them in the same file.

anyone could help me?


[code]
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;

int main()
{
const int Num=10;
const int iScore=50;
string names[Num];
string score[iScore];
int count;
ifstream inFile;
ofstream outFile;

inFile.open("D:\\names.txt");
outFile.open("nameslin.txt");

for (count=0; count<Num;count++)
{
inFile>>names[count];
}
inFile.close();

inFile.open("D:\\grades.txt");
outFile.open("nameslin.txt");

for(count=0; count<iScore; count++)
{
inFile>>score[count];
}
inFile.close();

for (count=0; count<Num;count++)
{
int index=count*5;

outFile<<names[count]<<" : "<<score[index]<<" "<<score[index+1]<<" "<<score[index+2]<<" "<<score[index+3]<<" "<<score[index+4]<<"\n";

outFile<<setw(2)<<right<<count+1<<" : "<<names[count]<<endl;
}

outFile.close();

cout<<"These names are:\n";

for (count=0; count<Num;count++)
{
cout<<" "<<count+1<<": ";
cout<<names[count]<<endl;
}


return 0;
}
Think about this in logical steps. This is what your program needs to do:

Open first file.
Read in names and scores and store them in variables.
Close first file.
Open second file.
Read in names and score and store them in variables.
Close second file.
Create third file.
Write in the information you stored from the other two files.
Close third file.
Done!
thx! :)
What @Mats said is probably the most logical way to do things, and less chance for you to get muddled up but if you REALLY want to open 2 files at the same time then all you need to do is create 2 instances of the ifstream and use one for each file.
Topic archived. No new replies allowed.