I'm trying to make a program that will read two txt files, with GPA information (one for females and one for males) arranged like this:

M 4.0
M 3.7
M 2.9

I've been instructed to used pass by reference void functions with no global variables. My major issue is how do I make a function to open the files then another to initialize variables? Here's what I've got so far, it's pretty rough.

If some one can even just explain conceptually how to do something like this, that would be great.

//GPA.cpp
//CMP-110
//October 09'



#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

    //Function Prototypes
void openFiles(
void initialize(
void sumGrades(
void averageGrades(
void printGrades(


int main()
{

openFiles()


}


void openFiles(ifstream& gpa)
{
    ifstream mData;
    ifstream fData;
    ofstream outGpa;

    mData.open("c:\\maleGPA.txt");
    fData.open("c:\\femaleGPA.txt");

    outGpa.open("c:\\finalGpa.txt");
    outGpa << fixed << showpoint;
    outGpa << setprecision(2);

    
}
    
void initialize(&)
{
    int mCount = 0;
    int fCount = 0;
    double mGPA = 0.00;
    double fGPA = 0.00;
    int countFemale = 0;
    int countMale = 0;
    double sumFemaleGpa = 0.00;
    double sumMaleGpa = 0.00;
}

void sumGrades(&)
{

I'm trying to make a program that will read two txt files, with GPA information (one for females and one for males) arranged like this:

M 4.0
M 3.7
M 2.9

I've been instructed to used pass by reference void functions with no global variables. My major issue is how do I make a function to open the files then another to initialize variables? Here's what I've got so far, it's pretty rough.

If some one can even just explain conceptually how to do something like this, that would be great.

//GPA.cpp
//CMP-110
//October 09'



#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

    //Function Prototypes
void openFiles(
void initialize(
void sumGrades(
void averageGrades(
void printGrades(


int main()
{

openFiles()


}


void openFiles(ifstream& gpa)
{
    ifstream mData;
    ifstream fData;
    ofstream outGpa;

    mData.open("c:\\maleGPA.txt");
    fData.open("c:\\femaleGPA.txt");

    outGpa.open("c:\\finalGpa.txt");
    outGpa << fixed << showpoint;
    outGpa << setprecision(2);

    
}
    
void initialize(&)
{
    int mCount = 0;
    int fCount = 0;
    double mGPA = 0.00;
    double fGPA = 0.00;
    int countFemale = 0;
    int countMale = 0;
    double sumFemaleGpa = 0.00;
    double sumMaleGpa = 0.00;
}

void sumGrades(&)
{

OK, I'm not sure exactly what you're after here but this snippet might help you a little:

#include<iostream>
#include<fstream>
#include<string>
#include<vector>

using namespace std;

////////////////////////////////////////////////////////////////////////////////
/// opens a file and reads its contents into a vector of strings.
////////////////////////////////////////////////////////////////////////////////
/// \param<filename> const reference to a string
/// \param<content> reference to a vector of strings
////////////////////////////////////////////////////////////////////////////////
void readInputFile(const string &filename, vector<string> &content)
{
	ifstream infile;
	infile.open(filename);


	string data;
	while(getline(infile, data))
		content.push_back(data);

	infile.close();
}

////////////////////////////////////////////////////////////////////////////////
/// main function
////////////////////////////////////////////////////////////////////////////////
int main()
{
	// NOTE: these are not global variables, they are local to main()!
	vector<string> maleData, femaleData;		

	// read the contents of the male and female data files into the 
	// vectors declared above.
	readInputFile("c:/maleGPA.txt", maleData);
	readInputFile("c:/femaleGPA.txt", femaleData);

	// now the vectors (maleData and femaleData) are populated with 
	// the data from the two text files

	// TODO: Now you can process the contents of the vectors by 
	// passing them as references into other functions.
	// you'll have to do these yourself!
		
}

NOTE: I haven't compiled or tested it, but it should be more or less correct!

Explanation:
The above snippet sets up a function (readInputFile) which takes two references as parameters, a const reference to a string (filename) and a reference to a vector of strings (content).
The function opens the specified file and then reads its contents line by line into the passed in vector of strings.

The main function sets up two arrays of strings, one for male data and one for female data.
We then call the readInputFile function twice, passing in a filename and a vector each time.
The vector passed into readInputFile is then populated with the data in the file.

It might not be exactly what you're trying to do, but it at least shows you how to pass by reference. You can always modify it to do what you want and then build upon it.

The next step would be to create any other variables you might need in main and then create additional functions which take references in order to process the data and output it to your output file.

Anyway, I hope this is of some help
Cheers for now,
Jas.

EDIT:
p.s. I haven't included any checks to ensure that the file opened correctly, you might want to make sure that you do this! J.

Edited 7 Years Ago by JasonHippy: n/a

I appreciate your quick response!

I haven't worked with vectors yet, but I see a little better now how to pass by reference.

So I'm going to need variables in main() that will get their values changed by the functions? I'm having difficulty understanding the difference between a global variable and what ever the variable is called in main() that changes.

How do I initialize variables in a function, then pass the values to values in main? don't I have to initialize variables in main for them to even compile?

I appreciate your quick response!

I haven't worked with vectors yet, but I see a little better now how to pass by reference.

So I'm going to need variables in main() that will get their values changed by the functions? I'm having difficulty understanding the difference between a global variable and what ever the variable is called in main() that changes.

How do I initialize variables in a function, then pass the values to values in main? don't I have to initialize variables in main for them to even compile?

OK well, to clear things up we'll take a quick look at how different parameter passing methods affect the way that parameters to functions are handled inside functions.

There are three main ways of passing parameters to functions.
1. Passing by value
2. Passing by reference
3. Passing a pointer

Here's a demonstration of passing by value:

#include<iostream>

void somefunc(int foo);

int main()
{
        int foo=4;
        somefunc(foo);
        cout << foo; // will ouput 4!
}

void somefunc(int foo)
{
        foo *= 2;
}

In the above code, somefunction takes an int as a parameter. The int parameter is passed by value. When passing by value the function creates a copy of the value passed into it. So in main the value of foo is passed into the function somefunc. Inside somefunc a copy of foo is created. Any changes made to the copy of foo inside somefunction are not reflected in the foo in main as they are two completely separate objects.
So in the above code, foo is created in main and initialised to 4. It is then passed by value into somefunc. somefunc creates a copy of foo and then changes the value of the copy of foo to 8. When somefunc returns, the value of foo in main is still 4.

However, if you pass a pointer or a reference into a function as a parameter, then the actual original object is referred to. No copy is made and any changes made to the parameter inside the function will be seen outside of the function.

so revisiting our previous example and passing a reference:

#include<iostream>

void somefunc(int &foo);

int main()
{
        int foo=4;
        somefunc(foo);
        cout << foo; // will now ouput 8!
}

void somefunc(int &foo)
{
        foo *=2;
}

or passing a pointer:

#include<iostream>

void somefunc(int *foo);

int main()
{
        int foo=4;
        somefunc(&foo); // use the address of operator to convert to a pointer
        cout << foo; // will also ouput 8!
}

void somefunc(int *foo)
{
        *foo *= 2;
}

In both of the above snippets foo is initialised to 4 in main. Foo is then passed into somefunc by reference or as a pointer. Somefunc changes the value of foo to 8. After somefunc returns to main, the value of foo is 8.

The only exceptions to the pass-by-value rule are arrays. If you pass an array into a function by value, it gets treated as if it was a reference or a pointer. So no local copy of the array is made inside the function. Any changes made to the array inside the function will be seen outside of the function.

Also to prevent a function modifying the values of parameters passed as pointers or references you should pass them as const references or const pointers.

So going back to your program; all you need to do is create some variables in main.
You can then create your functions and pass (by reference) any of the variables from main.

Because you're passing by reference, no copies of the parameters are made, so any changes made to the variables inside the function will also be seen in main.

With regard to global variables, any variables declared inside main ARE NOT global variables, they are local to main. Variables declared inside main are not visible to other functions in your program, so they are out of scope (unless they are passed by reference or by pointer).

global variables are declared like this:

#include<iostream>

// This is a global variable
float bar=0.0f;

void somefunc(int &foo);

int main()
{
        int foo=4;
        bar=4.5f;
        somefunc(&foo); // use the address of operator to convert to a pointer
        cout << foo; // will also ouput 8!
        cout << bar; // will output 9.45
}

void somefunc(int *foo)
{
        *foo *= 2;
        bar=9.45f;
}

In the above snippet you can see I've altered the pass-by-reference example.
I've added a global variable called bar, bar is visible to all functions declared in the above snippet. It does not need to be passed into somefunc or main because it has global scope.
So where you've been told not to use global variables, this is what your teacher/professor was talking about!

I hope that clears things up for you!
Cheers for now,
Jas.

Comments
Very helpful, and detailed

Your detailed explaination REALLY clarified things for me. I had several concepts twisted up. I've stayed away from global variables, but thought that variables in main where global for some reason. I just need to do make functions that pass their values to the variables declared in main.

Thanks so much! You'll are life savers!

This article has been dead for over six months. Start a new discussion instead.