Returning values from a void function

I am trying to return the values avgTime and avgGross from the void functions but have not been able to find anything that works. Typically, I would just put the cout statements in the void functions, but my professor requires that we have a separate void function to display everything. Does anybody have an idea of how I can get the values I find for avgTime and avgGross to be accessible in void display, without the use of arrays. Thank you.


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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#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:
	Movie();		//constructor
					
	void driver()
	{
		openFile();
		readFile();
		calcAvgTime();
		calcAvgGross();
		display();
	};		
};

Movie::Movie()
{
	//initialize variables
	int sumTime = 0;
	float sumGross = 0;
	float avgTime;
	float avgGross;
	
}

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";
	}
	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()				//calculates the average run time of the movies
{
	//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);															
	
	inMovie.clear();								//resets the read state of the inMovie file
	inMovie.seekg(0, std::ios::beg);
}

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);
}

void Movie::display()
{
	cout << "For the SciFi genre: \n\n";
	cout << "The average movie length is: " << fixed << setprecision(1) << avgTime << " minutes" << endl;
	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 program currently displays:
It's Movie Time!

For the SciFi genre:

The average movie length is: 0.0 minutes
The average gross box office is: 0.0 million
Press any key to continue . . . 




Last edited on
If you want to return something you shouldn't use void as the return type.
Typically, a function such as calcAvgGross wouldn't just calculate the value; it would also return it. Why do none of the functions return anything?
I am not exactly sure. The professor just requires that we use the void functions that are listed in my program and unfortunately, void functions don't return values.
Not sure if this will help any, but here are the functions and explanations for this project.

ifstream inMovie - input file containing the science fiction movie information
int sumTime - holds the sum of the running length values from the input file
float sumGross - holds the sum of the box office gross amount in millions
float avgTime - calculated movie running time average
float avgGross - calculated movie box office gross amount in millions

Program has private member functions (at a minimum):
void openFile() - open the input file
void testFile() - test if the input file opened properly
void readFile() - read the contents of the input file
void closeFile() - close the input file
void calcAvgTime() - calculate the average time and populates the average variable
void calcAvgGross() - calculate the average box office and populates the average variable
void display() - display the output value(s)

Last edited on
calcAvgTime and calcAvgGross could store their output in member variables and display can print them
void calcAvgTime() - calculate the average time and populates the average variable


Your class has these member variables:
1
2
3
float sumGross;
float avgTime;
float avgGross;


"Populate" means "set the value". You're meant to set the value of these member variables as you calculate them.

What your functions appear to currently do is create WHOLE NEW variables, set the values in those WHOLE NEW VARIABLES, and then you throw them away. Don't do that.

Look, here:
1
2
3
4
5
6
7
void Movie::calcAvgGross()
{
	int year;
	int time;
	float gross = 0;
	float sumGross = 0;
	float avgGross; // HERE. RIGHT HERE 

Right there, you create a WHOLE NEW VARIABLE named avgGross. DON'T.
Last edited on
Ahhhh yes! That's what it was, thank you.
void readFile() - read the contents of the input file
Your calcAvgGross() and calcAvgTime() functions also read the file. It's possible that the prof expects you to store the contents of the file in some data members and have calcAvgGross() and calcAvgTime() use that data.

If you do keep calcAvgGross() and calcAvgTime() as is, then I suggest that you clear the stream and seek to the beginning at the start of each function. The way it is now, the code only works if you call calcAvgTime() before calling calcAvgGross(), which makes the code fragile.
Topic archived. No new replies allowed.