.eof() problem with functions

Ok I can get this program to read and work fine when I tested it with just the first line of data, but when I try to get it to loop through the whole file it just creates an infinite loop...

I tried to put the input.eof() at the end of get_Oper, so at the end of the read from the line. I originally tried "while(!input.eof())" instead of using the the stop sentinel set up I have now...

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
#include <fstream>

using namespace std;

int get_Data(ifstream& input, ofstream& output, char numin);
int convert_from_Roman_to_Decimal(char numin);
char get_Oper(ifstream& input, int& stop);
void calc_Romans(int num1, int num2, char oper, int& result);
void print_Roman_Result(int result, ofstream& output);

int main()
{
	ifstream input;
	ofstream output;
	char numin, oper;
	int num1 = 0, num2 = 0, result = 0, stop = 0;
	
	input.open("mp4romanletr-data.txt");
	output.open("outfile.txt");

	while(stop != 1)
	{
	output << "The first number is ";
	num1 = get_Data(input, output, numin);
	output << " ( " << num1 << " )" << endl;
	
	output << "The second number is ";
	num2 = get_Data(input, output, numin);
	output << " ( " << num2 << " )" << endl;
	
	output << "The operator is ";
	oper = get_Oper(input, stop);
	output << oper << endl;
	
	calc_Romans(num1, num2, oper, result);
	
	output << "The result is ";
	print_Roman_Result(result, output);
	output << " ( " << result << " )" << endl;	
	}
	
	input.close();
	output.close();
	return 0;
}

int get_Data(ifstream& input, ofstream& output, char numin)
{
	int x, y = 0;
	x = 0;
	
	input.get(numin);
	while (y < 1)
	{
		if (numin == ' ')
		{
			input.get(numin);
		}
		else y++;
	}	
	while(numin != ' ')
	{
		output << numin;
		x = x + convert_from_Roman_to_Decimal(numin);
		input.get(numin);
	}
	
	return x;
}

char get_Oper(ifstream& input, int& stop)
{
	char oper;
	int y = 0;
	
	input.get(oper);
	while (y < 1)
	{
		if (oper == ' ')
		{
			input.get(oper);
		}
		else y++;
	}	
	
	if (input.eof())
		stop++;
	return oper;
}


and the conversions don't matter...

input file:
MCCXXVI CV +
MCCXXVI MCCXXVI /
V I -
MDCLXVI III *
DL DXXXXVIII -
D L /
MDI CXI +
XXV IIII /
XI CII *

any help?
Last edited on
I don't want anyone to do my homework, just explain why the .eof() doesn't work how I'm trying to use it...
First of all, tell me what are you trying to verify in that condition: if(input.eof()) ?. You want to check if the program reached the end-of-file pointer? If you do, you must to do it like this: if(!input.eof()). Tell me if it works.
I'm trying to get the program to loop through all the data until the end of file is reached. The problem I'm having now is I can either get it to read only the first line and end, or just loop infinitely until my computer runs out of memory. Trying !input.eof() in line 86 only reads the first line. Changing the condition from the stop sentinel to !input.eof() in line 21 does the opposite, looping infinitely.

It's not life or death since I have two weeks to finish it, but I hate hitting road blocks. I can ask my professor to explain the filestream better I suppose, to try to figure out character by character what's going wrong.
Your program is hard to read. If you want Boolean values, use a bool variable.
Also you can break of a loop with break

You could read the data as
1
2
3
4
string a,b;
char oper;

while( cin >> a >> b >> oper ){ //process one line 
I know this would be way easier... and then send each numeral to convert using string arrays with a for loop to increment the position... but this is the format we're required to use... I know, it's dumb.
If you guys care, I fixed it... here's what I did for posterity:
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
#include <fstream>
using namespace std;

... function prototypes

int main()
{
	// Initialize variables
	ifstream input;
	ofstream output;
	char numin, oper;
	int num1, num2, result;
	
	input.open("mp4romanletr-data.txt");
	output.open("outfile.txt");

	while (!input.eof() && input) // Executes while not end of file
	{

	... same output and function calls

	}
	
	input.close();
	output.close();
	return 0;
}

int get_Data(ifstream& input, ofstream& output, char numin)
{
	int x = 0;
	
	input.get(numin); // Priming read
	
	// Skips spaces until valid input
	while (numin == ' ')
	{
		input.get(numin);
	}
	
	while (numin != ' ') // Reads Roman numeral until a space is reached
	{
		output << numin;
		// Converts one character at a time and adds to number
		x += convert_from_Roman_to_Decimal(numin); 
		input.get(numin);
	}
	
	return x;
}

char get_Oper(ifstream& input)
{
	char oper;
	
	input.get(oper); // Priming read
	
	// Skips spaces until valid input
	while (oper == ' ')
	{
		input.get(oper);
	}	
	
	input.ignore(10, '\n'); // Ignores end of line character
	
	return oper;
}

...
Topic archived. No new replies allowed.