HELP PLEASE: Program involving input/output file streams

My program is supposed to take input from a file and output it to another file. The input file should be in a format like this:

first_name1 last_name1 score1-1 score2-1 score3-1 ... score10-1
first_name2 last_name2 score1-2 score2-2 score3-2 ... score10-2
fist_name3 last_name3 score1-3 score2-3 score3-3 ... score10-3
.... .... .... ..... ..... ... ....

and the output file should be in a format like this:

first_name1 last_name1 score1-1 score2-1 score3-1 ... score10-1 average1
first_name2 last_name2 score1-2 score2-2 score3-2 ... score10-2 average2
fist_name3 last_name3 score1-3 score2-3 score3-3 ... score10-3 average3
.... .... .... ..... ..... ... .... .....

This is my code so far and it seems like the test_more function isn't working. I've put hours into this program and I've tried doing it using different methods and it still isn't working. What am I doing wrongly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <fstream>

using namespace std;

//takes first and last name from input file stream and puts it in output file stream
void process_name(ifstream& in_stream, ofstream& out_stream);

//assumes that first and last names have been processed
//takes test scores from input file stream and outputs them in the output file stream
//and outputs the average for the corresponding line
void process_scores(ifstream& in_stream, ofstream& out_stream);

//returns true if there are no more lines to process in input file stream
//returns false otherwise
bool test_more(ifstream& in_stream);

int main()
{

	char control = 'y';//this variable is used by user to stay in or exit out of do-while loop

	do
	{
		ifstream in_stream;
		in_stream.open("test.txt");

		ofstream out_stream;
		out_stream.open("test2.txt");

		bool more = true;
		while(more)
		{
			process_name(in_stream, out_stream);
			process_scores(in_stream, out_stream);
			more = test_more(in_stream);
		}

		in_stream.close();
		out_stream.close();

		cout << "\n\nWould you like to run the program again? (Y/N): ";
		cin >> control;
		while ((control != 'y') && (control != 'n'))
		{
			cout << "Incorrect input. Would you like to run the program again? (Y/N): ";
			cin >> control;
			control = tolower(control);
		}
		
	}while(control == 'y');
	
	cout << "Press key to exit: ";
	char exit;
	cin >> exit;

	return 0;
}

void process_name(ifstream& in_stream, ofstream& out_stream)
{
	short i(0);
	char next;
	do
	{
		in_stream.get(next);
		out_stream << next;
		if(next == ' ')
		{
			i++;
		}
	}while(i < 2);
	return;
}

void process_scores(ifstream& in_stream, ofstream& out_stream)
{
	short next, sum(0);
	for(short i = 0; i < 11; i++)
	{
		in_stream >> next;
		out_stream << next << ' ';
		sum += next;
	}
	double average = sum/10.0;
	out_stream << average;
	return;
}

bool test_more(ifstream& in_stream)
{
	char next;
	in_stream >> next;
	if(next == '\n')
	{
		return true;
	}
	else if(in_stream.eof())
	{
		return false;
	}
}
Your process_name is weird. Just use a string or a char array and >>.
In process_scores, your for loop is bad. There are 11 numbers starting from 0 that satisfy the condition < 11 thus 11 numbers will be expected. This might be one source of error.
I'm not sure about test_more. I don't advise using eof flag to check for end of file. If there is another white space or new line at the end of your file, this function will fail. It would be better to keep reading until a failure occurs. Then you probably should change your code a bit. Remove test_more, make the other two return booleans. In them put if(in_stream.fail) return false;. Then call them with while(process_name(...) && process_scores(...) ); or something like that.
Easiest way would be reading the entire files contents to a C char array and writing that. Should only be 2 lines to do so.

Assuming you just need to make an exact duplicate of the contents into another file.

You could replace the following code, with what is below. I only did the reading side for example.

1
2
3
4
5
6
7
bool more = true;
while(more)
{
  process_name(in_stream, out_stream);
  process_scores(in_stream, out_stream);
  more = test_more(in_stream);
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fstream in_stream;
in_stream.open( "test.txt", ios::in );

if( in_stream.is_open() )
{
  string strLine;
  while( getline( in_stream, strLine ) )
  {
    stringstream ss( strLine );
    string fn;
    string ln;

    ss >> fn;
    ss >> ln;
    cout << fn << " " << ln << endl;

    while( true )
    {
      int score;
      ss >> score;

      if( !ss.good() )
        break;

      cout << score << endl;
    }
  }
}
else
  cout << "Error opening file " << errno << endl;
Topic archived. No new replies allowed.