Problems parsing/using void functions

In my program, we are supposed to uses classes and void functions to create a program that gives the average box office and run time of five movies from a file. Whenever I run my program, the void calcAvgTime function works properly and displays what it should. The void calcAvgGross function on the other hand is nearly identical but does not display the average gross box office.

*****This is the code:

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
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
#include<fstream>
#include<string>
#include <iomanip>
using namespace std;

class Movie
{
private:
	ifstream inMovie;		//private member variables
	int sumTime;
	float sumGross;
	float avgTime;
	float avgGross;

	void openFile();
	void testFile();
	void readFile();
	void closeFile();
	void calcAvgTime();
	void calcAvgGross();
	void display();

public:
					
	void driver()
	{
		openFile();
		readFile();
		calcAvgTime();
		calcAvgGross();
	};		
};

void Movie::openFile()					//Opens the file
{
	inMovie.open("SciFiMovies.txt");
}

void Movie::readFile()						//Reads the file and gives output if the file is open
{
	string file;
	if (inMovie.is_open())
	{
		cout << "It's Movie Time!\n\n";
		cout << "For the SciFi genre: \n\n";
	}
	else
		testFile();
}

void Movie::testFile()						//Gives user an error if the file does not open
{
	cout << "Unable to open the file.";
	exit(1);
}

void Movie::calcAvgTime()
{
	//variables
	int year;
	int time = 0; 
	float gross; 
	int sumTime = 0;
	float avgTime;


	while (inMovie >> year >> time >> gross)		//parsing, reads the file as int, int, float
	{
		string name;
		if (getline(inMovie >> ws, name))			//removes white space
		{
		 (sumTime += time);							//get the sum of the movie times
		}
	}

	avgTime = (static_cast<float>(sumTime) / 5);																
	cout << "The average movie length is: " << fixed << setprecision(1) << avgTime << " minutes" << endl;
}

void Movie::calcAvgGross()
{
	int year;
	int time ;
	float gross = 0;
	float sumGross = 0;
	float avgGross;


	while (inMovie >> year >> time >> gross)
	{
		string name;
		if (getline(inMovie >> ws, name))
		{
			(sumGross += gross);
		}

		avgGross = (sumGross / 5);
		cout << "The average gross box office is: " << fixed << setprecision(1) << avgGross << " million" << endl;
	}
}

void Movie::closeFile()
{
	inMovie.close();
}

int main()
{
	Movie movObj;
	movObj.driver();
	system("pause");
	return 0;
}




*****This is the file I am reading from:
1
2
3
4
5
2016 116 303.1 Passengers 
2014 169 677.5 Interstellar 
2015 141 630.2 The Martian 
2013 91 723.2 Gravity 
2016 116 203.4 Arrival


*****This is what my program currently displays
It's Movie Time!

For the SciFi genre:

The average movie length is: 126.6 minutes
Press any key to continue...


*****Note: The setup of this program may seem a little weird, but it is required to be this way. Also, void functions have to be used and arrays/pointers are not allowed.
Last edited on
I think the issue here is that after you read through the file the first time, the file's read state is still at end of file. So when you go to read through it the second time, it starts at the end and reads nothing in. Try resetting the read state to the beginning of the file before reading it the second time:

1
2
inMovie.clear();
inMovie.seekg(0, std::ios::beg);
you appear to open the file once, read it in the first calculate program, and then the second calculate has nothing left in the file to read.

some ideas:
you can close and re-open the file. This is a little crude.
you can look at the file routines and find one that puts the 'read here next' marker back to the front of the file. This is better.
you can read the file once into a data structure, and reuse the data. This is the best approach usually (unless file is too big, 10s of GB+) as reading a file is much slower than having the data in ram. However given what you already did, this is probably too much work at this point, and rewinding the file to the beginning is your best 'minimal effort to fix' approach.

Please use code tags. No one wants to read a blob of text with no formatting. <> on the side editor.
Last edited on
PLEASE learn to use code tags, it makes reading and commenting on source code MUCH easier.

HINT, you can edit your post and add code tags.

http://www.cplusplus.com/articles/jEywvCM9/

More often than not a lot of problems with code can be spotted easily when the code is properly formatted.
Gotcha, sorry about that.
Topic archived. No new replies allowed.