how to format outfiles with multiple input files

hi, I have a code here that is reading two input files and creating one output file. What i am having trouble with is I don't know how to format the output file.

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;


int main()
{
ifstream in_stream;
ifstream in_stream1;
ofstream out_stream;
string name;
string score;

/* get input file */
in_stream.open("csci_123_student_names.txt");
if (in_stream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}

in_stream1.open("csci_123_student_exam_score.txt");
if (in_stream1.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}

/* get output file */
out_stream.open("outfile.txt");
if (out_stream.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}

/* Read line by line and save to output file */
while(!in_stream.eof()){
getline(in_stream, name);
out_stream << name << endl;
}

while(!in_stream1.eof()){
getline(in_stream1, score);
out_stream << score <<endl;
}

in_stream.close();
out_stream.close();
return 0;
}


Right now my output file looks like this

Bade Shrestha, Riyaj
Baker, Christopher
Castro, Bryan
Chou, Yu Hsiu
Cone, Philip
Diaz, Josiah
Farraj, Mustapha
Graves, Kyle
Hoehle, Graham
Huynh, Khoa
Hwang, Joey
Irie, Susumu
Joya Ruiz, Diana
Khai, Brian
Kim, Jasmine
Kwak, Junyoung
Lee, Christopher
Lee, Tae Hun
Lema, Nathaniel
Liu, Heng
Lo, Jack
Matsuo, Kenta
Nam, Ryan
Nguyen, Simon
Wang, Yijia
Williams, Jordan
Young, Brian
Zew, Daniel
80
85
89
100
79
90
66
77
80
97.5
78
69
99.5
87.7
95.5
89
86.5
97
69
100
95
85
81
0
89.5
75.5
99
91

even though is it suppose to look like this

Index Student Name Score
1 Bade Shrestha, Riyaj 80
2 Baker, Christopher 85
3 Castro, Bryan 89
4 Chou, Yu Hsiu 100
5 Cone, Philip 79
6 Diaz, Josiah 90
7 Farraj, Mustapha 66
8 Graves, Kyle 77
9 Hoehle, Graham 80
10 Huynh, Khoa 97.5
11 Hwang, Joey 78
12 Irie, Susumu 69
13 Joya Ruiz, Diana 99.5
14 Khai, Brian 87.7
15 Kim, Jasmine 95.5
16 Kwak, Junyoung 89
17 Lee, Christopher 86.5
18 Lee, Tae Hun 97
19 Lema, Nathaniel 69
20 Liu, Heng 100
21 Lo, Jack 95
22 Matsuo, Kenta 85
23 Nam, Ryan 81
24 Nguyen, Simon 0
25 Wang, Yijia 89.5
26 Williams, Jordan 75.5
27 Young, Brian 99
28 Zew, Daniel 91

I would just like to know how to format the output file. Any help would be greatly appreciated
Last edited on
1
2
3
4
5
6
7
8
9
/* Read line by line and save to output file */
while((!in_stream.eof() && (!in_stream1.eof() )
{
     getline(in_stream, name);
     getline(in_stream1, score);
out_stream << name << " " << score <<endl;
} 

Last edited on
Thank you. It makes sense to me now. But when i did make the changes, the output file only shows two names for some reason

1 Bade Shrestha, Riyaj 80
85
89
100
79
90
66
77
80
97.5
78
2 Baker, Christopher 69
99.5
87.7
95.5
89
86.5
97
69
100
95
85
81
0
89.5
75.5
99
91
Could you show the whole code (and input files).

SamuelAdams' suggestion has a typo (I think) as it is testing the EOF for the same file twice - though it would definitely be better to name your input streams a little more distinctly than in_stream and in_stream1.

Just as a suggestion for developing a code:
- in the initial test stages keep your input files small: not 20+ items
- while testing, write to cout first and only once working recode it to output to a file
He's correct, I had the same name twice, changed it to
while((!in_stream.eof() && (!in_stream1.eof() )

Seeing your current code would be helpful.
Last edited on
Topic archived. No new replies allowed.