Jump to content

Helping hand for c++ homework?


Promise

Recommended Posts

Having a really hard time with this program. And thought I could get some help here...Thanks for any help in advanced!

 

 

The assignment is to write a code that will open 2 text files full of male and female GPAs.

 

This is an example of the text file. m = male, and f = female.

[hide=exmaple]

m 3.1

m 2.63

m 3.5

m 3.55

m 3.47

f 3.98

m 3.33

m 2.54

f 3.35

m 1.8

m 2.81

f 3.34

f 3.45

m 3.84

m 3.4

m 2.57

f 2.45

f 2.71

f 3.75

f 3.9

[/hide]

 

My program must be able to output the average of males and females into a seperate text file.

 

 

So far I am able to get the file and open one. I'm stumped on how to get the info from the file, and get the average.

 

This is what I have so far...

 


#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

bool openFiles (ifstream&, ofstream&);
int readFile (ifstream&, ofstream&);
void outLine (int, double, int, ofstream&);

int main()
{
int numlines = 0;
ifstream infile;
ofstream outfile;

bool opened;

opened = openFiles (infile, outfile);
if (!opened)
	exit (1);

numlines  = readFile (infile, outfile);
cout << "\n" << numlines << "lines read and written.\n";

infile.close();
outfile.close();

return 0;

}

bool openFiles (ifstream& input, ofstream& output)
{
string filename;
cout << "INPUT file name please: ";
cin >> filename;
input.open (filename.c_str());

 if (input.fail())
 { 
	 cout << "Input file not found or corrupt!\n";
	 return false;
 }

 cout << "OUTPUT file name please: ";
 cin >> filename;
 output.open(filename.c_str());
 if (output.fail())
 {
	 cout << "Output file: deevice not found or disk full!\n";
	 return false;
 }

 return true;
}

int readFile (ifstream& input, ofstream& output)
{
int first,
	line_count = 0;
double second;

if (input.eof())
	return line_count;
else
{
	while (input.good())
	{
		input >> first >> second;
		line_count++;
		outLine (first, second, line_count, output);
	}
}
return line_count;
}

void outLine (int f, double s, int c, ofstream& out)
{
out << setw(3) << c << ' ' << setw(4) << f << ' ' << setw(6) << s << endl;
}

Link to comment
Share on other sites

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Your variable "first" should be of the 'char' datatype, not int as you're reading in a alphabetic character rather than a number for the gender. Once you fix that the program will be able to read in the data and output it to the file. I'll get back to you on the averages in a moment.

Tecmaster532.png
Tecmaster532.png
lm9dzxe3.png
Owner of the Quest Point Cape

Link to comment
Share on other sites

As for processing the averages, rather than immediately outputting the data to a new file, what you need to do is to dump all of the incoming data into two lists (or dynamic arrays), one for male and one for female. On each line you read in you should be able to use the first variable to determine if the gender is female or male, and then add the second value to the correct list.

 

From there, once you've read in both files you can loop through both lists to calculate the two averages and then write them to the output file.

 

Hopefully I didn't confuse you too much. :P

Tecmaster532.png
Tecmaster532.png
lm9dzxe3.png
Owner of the Quest Point Cape

Link to comment
Share on other sites

It should be like this, correct? Made the change in red.

 


int readFile (ifstream& input, ofstream& output)
{
       [color="#FF0000"]char[/color] first,
               line_count = 0;
       double second;

       if (input.eof())
               return line_count;
       else
       {
               while (input.good())
               {
                       input >> first >> second;
                       line_count++;
                       outLine (first, second, line_count, output);
               }
       }
       return line_count;
}

Link to comment
Share on other sites

Yay! Just that one small change got my program to read all 20 lines instead of just 1.

 

This is the output file:

[hide=output]

 

1 109 3.1

2 109 2.63

3 109 3.5

4 109 3.55

5 109 3.47

6 102 3.98

7 109 3.33

8 109 2.54

9 102 3.35

10 109 1.8

11 109 2.81

12 102 3.34

13 102 3.45

14 109 3.84

15 109 3.4

16 109 2.57

17 102 2.45

18 102 2.71

19 102 3.75

20 102 3.9

 

[/hide]

 

So now that my program can rread all the lines, I need to sort the gender. Its required I make a separate function for this. Honestly don't even know where to start with this. Loop? If/else statements? ughhhhh :wall:

Link to comment
Share on other sites

Hey, no problem I'm here to help out.

 

My suggestion is that you create two arrays of 100 items each. (assuming you're program won't be processing more than 100 males or females)

 

Then, you can use if/else if/else statements to determine which gender (your 'first' variable) the person is and add them to the appropriate list. You can use two separate counters (m and f) to keep track of how many males and females you have. There are better ways to do it, but until you learn about dynamic memory or vectors this is probably the easiest way.

Tecmaster532.png
Tecmaster532.png
lm9dzxe3.png
Owner of the Quest Point Cape

Link to comment
Share on other sites

What you'll want to do is to create 4 variables in main, and then pass them as parameters into the readFile() function.

 

For example

 

int m = 0, f = 0; // Male and female counters
double male[100], female[100]; //Male and female arrays

 

Then for your function:

int readFile (ifstream& input, ofstream& output, int& m, int& f, double& male[], double& female[])

 

In the function you can then do something like this instead of writing to the outfile.

 

//read file here
if(first == 'm')
{
male[m] = second;
m++;
}
else if(first == 'f')
{
female[f] = second;
f++;
}

 

As long as you don't have more than 100 males or females this will result in two arrays back in main that contain the averages of the males and females respectively. Then you can simply loop through the two arrays as needed to tally up the averages like so:

 

double average = 0;

for(int i = 0; i < m; i++)
average += male[i];

average = average / m; 

Tecmaster532.png
Tecmaster532.png
lm9dzxe3.png
Owner of the Quest Point Cape

Link to comment
Share on other sites

EDIT: typed this before I saw your post!!! :mrgreen:

 

 

So something like this to get me started? I have no idea but making educated guesses...my notes all use while loops so I think I need to use loops? :?:

 

void initializeArrays (char m[], char f[]);
{

int i = 0;
char gender;

while (gender =='m' || gender == 'f')

}

and then there is where I am stumpped. How Do i put the data from the text file into that??

Link to comment
Share on other sites

int m = 0, f = 0; // Male and female counters
double male[100], female[100]; //Male and female arrays

 

 

Just to clarify, are we using double for the arrays because of the GPA decimal points or are we using double as a counter for male/female count?

 

Just need to be sure because my teacher turns into the spawn of satan when she grades.... :ohnoes:

Link to comment
Share on other sites

Just need to be sure because my teacher turns into the spawn of satan when she grades.... :ohnoes:

 

Eek!

 

Anywho, the doubles are for the two arrays that hold the gpa values because they are decimals. The 'm' and 'f' variables are ints and are simply counters that keep track of how many males and females we've processed so far. They're also used in calculating the averages later on because they contain the total number of males and females once you finish reading in the data.

Tecmaster532.png
Tecmaster532.png
lm9dzxe3.png
Owner of the Quest Point Cape

Link to comment
Share on other sites

That makes complete sense to me! :thumbsup:

 

Thank you so much for all your help so far!

 

So on the side, I've been trying to figure out how to get input data from multiple text files...my thinker is telling me to ask the user after the one text file as been input, if he would like to continue, or input another file.

 

Then use an if else statement?

Link to comment
Share on other sites

No problem!

 

The way we've set things up, as long as you're passing the 4 variables from main into the readFile function, you can simply call openFile and readFile again to read from a second file. The only difference is that you have to change your code to calculate the average and write to the output file later in the program after you're done reading from all the files you want to.

Tecmaster532.png
Tecmaster532.png
lm9dzxe3.png
Owner of the Quest Point Cape

Link to comment
Share on other sites

Oh right, fail.

 

I forgot that arrays are automatically passed by reference when you pass them into functions, so you don't need the & sign after the word double. My bad.

 

int readFile (ifstream& input, ofstream& output, int& m, int& f, double male[], double female[])

Tecmaster532.png
Tecmaster532.png
lm9dzxe3.png
Owner of the Quest Point Cape

Link to comment
Share on other sites

My text requires me to use specific variables. So I had to change what you gave me a little. I was required to use countFemale, countMale, sumFemaleGPA, and sumMaleGPA. So after substituting in what you typed, does this all still look correct?

 

Also, should the function be an int or a void? Its not returning a value for some reason? #-o

 

EDIT: Forgot the last bit of code....sorry haha. :mrgreen:

 

 

int countMale = 0;
int countFemale = 0;
double sumMaleGPA[100];
double sumFemaleGPA[100];

 

 


int readFile (ifstream& input, ofstream& output, int& countMale, int& countFemale, double sumMaleGPA[], double sumFemaleGPA[])
{
char first;
double second;

if(first == 'm')
{
	sumMaleGPA[countMale] = second;
	countMale++;
}
else if(first == 'f')
{
	sumFemaleGPA[countFemale] = second;
	countFemale++;
}

}

Link to comment
Share on other sites

Ok, I was just thinking about this a little bit more and realized that you don't actually need to use the individual data for anything, only the average.

 

So, instead of making it an array, just make sumMaleGPA and sumFemaleGPA regular doubles and add the value of the current line to them.

 

I.E.

 

if(first == 'm')
{
 sumMaleGPA += second;
 countMale++;
}

 

(You do need to add the & sign back after the word double now because they're no longer arrays)

 

Because you only need the average of each of the GPA's you can just keep a running sum of the male GPA's and the female GPA's and then just divide them by the count at the end.

 

Once you finish reading in the data you'll be left with sumMaleGPA and sumFemaleGPA which contain the total of all of the male and female GPA's and the countMale and countFemale variables which then contain the number of males and females. You can then just divide to get the averages and write to the file.

 

Sorry for misleading you there with the arrays for a while, I was reading into the problem a bit too far and thought you had to do other processing down the line that you didn't.

Tecmaster532.png
Tecmaster532.png
lm9dzxe3.png
Owner of the Quest Point Cape

Link to comment
Share on other sites

I'm down to this, but its not returning a value. :-k

 

int countMale = 0;
int countFemale = 0;
double sumMaleGPA;
double sumFemaleGPA;

 

 


int readFile (char first, double second, ifstream& input, ofstream& output, int& countMale, int& countFemale, double& sumMaleGPA, double& sumFemaleGPA)
{

if(first == 'm')
{
	sumMaleGPA += second;
	countMale++;
}
else if(first == 'f')
{
	sumFemaleGPA += second;
	countFemale++;
}

}

Link to comment
Share on other sites

      char first;
       int line_count = 0;
       double second;

       if (input.eof())
               return line_count;
       else
       {
               while (input.good())
               {
                       input >> first >> second;
                       line_count++;
                       //The if/else if block should go here
               }
       }
       return line_count;

 

Don't forget about the rest of the 'stuff' that was in the function before, you still need to read in the data from the file to populate those first and second variables and keep counting the lines. The while loop is also what keeps the system going until you reach the end of the file.

 

Edit: you also don't need to be passing the first/second variables into the function, they should be declared locally.

Tecmaster532.png
Tecmaster532.png
lm9dzxe3.png
Owner of the Quest Point Cape

Link to comment
Share on other sites

EDIT AGAIN: (AHHHH)

 

This is the current code....

 



int readFile (ifstream& input, ofstream& output, int& countMale, int& countFemale, double& sumMaleGPA, double& sumFemaleGPA)
{
   char first;
       int line_count = 0;
       double second;

       if (input.eof())
               return line_count;
       else
       {
               while (input.good())
               {
                       input >> first >> second;
                       line_count++;
if(first == 'm')
{
	sumMaleGPA += second;
	countMale++;
}
else if(first == 'f')
{
	sumFemaleGPA += second;
	countFemale++;
}                        


               }
	}

double average = 0;

for(int i = 0; i < countMale; i++)
average += sumMaleGPA;

average = average / countMale; 
return line_count;
}


Link to comment
Share on other sites

I don't think you need any more help, you've got all the code there, you just need to logically follow it through and understand what your code is actually doing. From there you can fix it.

 

Edit: remember to consider EVERY line, don't skip a single one.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.