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.
#include<iostream>
#include<fstream>
#include<string>
#include <iomanip>
usingnamespace 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;
}
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.
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:
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.