Jump to content

Helping hand for c++ homework?


Promise

Recommended Posts

Markup..I'm horrible at c++. My mind works to fabricate steel frames, suspension, and machinery. This c++ does not compute well at all with me. :mad:

 

Havent finished the code, but this is what I have so far. Just trying to get it to work before I continue as I am getting a fatal error... :ohnoes: :ohnoes: :ohnoes:

 



#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 countMale = 0;
int countFemale = 0;
double sumMaleGPA;
double sumFemaleGPA;

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




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

Changing the else if to just else gets rid of the fatal error, but gives me a different error saying "syntax error : missing ';' before '{'" The red font is where the error is coming from. (Also where i changed the else if to just else.

 


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

					[color="#FF0000"]else(first == 'f')
					{[/color]
						sumFemaleGPA += second;
						countFemale++;
					}
               }
	}

Link to comment
Share on other sites

Hello promise.

 

For starters, your declaration of 'readFile' does not match you implementation.

 

Line 9: int readFile (ifstream&, ofstream&);

 

Line 66: int readFile (ifstream& input, ofstream& output, int& countMale, int& countFemale, double& sumMaleGPA, double& sumFemaleGPA)

 

Also it appears you aren't calling 'readFile' with all the arguments.

 

Line 30: numlines  = readFile (infile, outfile);

"300 programmers make their futile but glorious last stand against 1000000 angry players in The battle of Misthalin. They fight for honor, glory and new content sacrificing themselves so that their game may live on. This is Madness! This Is JAGEEEX!"
Link to comment
Share on other sites

Edit: Dudecrush beat me to it. :P

 

Don't change it to an else, keep it an else if. The only reason that it's stopping the fatal error is because that error is being caught before the fatal one.

 

The fatal error is in the fact that your function prototypes (at the top of the program), don't actually match the functions you have. For example int readFile (ifstream&, ofstream&); doesn't match the readFile function. Make sure you update them whenever you change the parameter list for the function.

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

Link to comment
Share on other sites

Hello promise.

 

For starters, your declaration of 'readFile' does not match you implementation.

 

Line 9: int readFile (ifstream&, ofstream&);

 

Line 66: int readFile (ifstream& input, ofstream& output, int& countMale, int& countFemale, double& sumMaleGPA, double& sumFemaleGPA)

 

 

Thank you for pointing that out. I missed while changing up that portion of the code. After doing that, I got another error.

 

" 'openFiles' : function does not take 2 arguments"

 

I never got that error till I changed the declaration for readFile.

 

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

Link to comment
Share on other sites

Also it appears you aren't calling 'readFile' with all the arguments.

 

Line 30: numlines  = readFile (infile, outfile);

"300 programmers make their futile but glorious last stand against 1000000 angry players in The battle of Misthalin. They fight for honor, glory and new content sacrificing themselves so that their game may live on. This is Madness! This Is JAGEEEX!"
Link to comment
Share on other sites

In line 30 you are trying to call a function, not declare one. Thus you need to pass it the names of the actual variables.

 

Line 30: numlines  = readFile (infile, outfile, countMale, countFemale, sumMaleGPA, sumFemaleGPA);

"300 programmers make their futile but glorious last stand against 1000000 angry players in The battle of Misthalin. They fight for honor, glory and new content sacrificing themselves so that their game may live on. This is Madness! This Is JAGEEEX!"
Link to comment
Share on other sites

In line 30 you are trying to call a function, not declare one. Thus you need to pass it the names of the actual variables.

 

Line 30: numlines  = readFile (infile, outfile, countMale, countFemale, sumMaleGPA, sumFemaleGPA);

 

I love you guys so much. I've worked on this code for hours. :wall: :wall: :wall: :wall:

 

All these little details are killing me!

 

Big props to people who do this for a living. :twss:

Link to comment
Share on other sites

Got the program to finally start without errors....

 

1.) It didnt put anything in my output list....I need to keep going over notes to look for this.

 

2.) Still need to learn how to accept multiple input files.

 

3.) Average (shouldn't be too hard for me.) :---)

 

 

 


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

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

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

int main()
{
int numlines = 0;

ifstream infile;
ofstream outfile;

bool opened;

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

numlines  = readFile(infile, outfile, countMale, countFemale, sumMaleGPA, sumFemaleGPA);
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& 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;
}


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

You're getting there!

 

You still need to calculate the averages in the main() function and then write them to the file.

 

double average = 0;

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

       average = average / countMale; 
       return line_count;

 

You should move this block of code to the main() function after you read the contents of the file. Then do it again with a different 'average' variable name to calculate the male and female averages.

 

From there you can write it to the file with a simple outfile << "Write stuff here";

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

Link to comment
Share on other sites

You're getting there!

 

You still need to calculate the averages in the main() function and then write them to the file.

 

double average = 0;

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

       average = average / countMale; 
       return line_count;

 

You should move this block of code to the main() function after you read the contents of the file. Then do it again with a different 'average' variable name to calculate the male and female averages.

 

From there you can write it to the file with a simple outfile << "Write stuff here";

 

 

The spawn of satan AKA my teacher, HATES when we do math in the main function. I need to do the math in a separate function! <_<

Link to comment
Share on other sites

In that case you can just create a function called calcAverage() which takes in one of the counters and one of the totals and returns the average. Then in main you can just do something like:

 

double maleAvg = calcAverage(countMale, sumMaleGPA);
double femaleAvg = calcAverage(countFemale, sumFemaleGPA);

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

Link to comment
Share on other sites

You already have the output file pointer that you connected to the output file in the openFiles function, so all you have to do is create a new function and pass in that outfile variable as well as the averages and all you have to do in that function is outfile << "Write your stuff here";

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

Link to comment
Share on other sites

My assignment requires me to find the following:

[hide=requirements]

 

Opens the input and output files, and sets the ouput of the floating point numbers to two decimal places in a fixed decimal format.

 

Find the sum of the female and male GPAs

 

Find the total amount of males and females

 

Find the Average GPA for males and females.

 

Outputs the relevant results

 

There can be no global variables.

[/hide]

 

The only thing I cannot do is:

-output everything into a textfile

 

This is my entire code so far

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

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



int main()
{

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

int numlines = 0;

ifstream infile;
ofstream outfile;

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

averageGrade(countMale, sumMaleGPA);
averageGrade(countFemale, sumFemaleGPA);


numlines  = readFile(infile, outfile, countMale, countFemale, sumMaleGPA, sumFemaleGPA);
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& 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;
}


double averageGrade(int count, double sumGPA)
{
double averageGrade = sumGPA / count;
return averageGrade;

}


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

You already have the output file pointer that you connected to the output file in the openFiles function, so all you have to do is create a new function and pass in that outfile variable as well as the averages and all you have to do in that function is outfile << "Write your stuff here";

 

An example being:

 

outfile << countMale << sumMaleGPA << countFemale << sumFemaleGPA << endl;

 

 

It's that simple? :shock:

Link to comment
Share on other sites

       averageGrade(countMale, sumMaleGPA);
       averageGrade(countFemale, sumFemaleGPA);

 

This call should be after the call to readFile() and should be filling the variables averageMaleGrade and averageFemaleGrade respectively like this:

 

       averageMaleGrade = averageGrade(countMale, sumMaleGPA);
       averageFemaleGrade = averageGrade(countFemale, sumFemaleGPA);

 

There's no point calculating the averages if we don't have any data yet. :P

 

As for outputing to a file, once you have the averages calculated you need to write a quick function to write the output to a file.

 

To help with reading from multiple files later we'll move the code to open the output file into this new function as well.

 

bool writeData(int countMale, int countFemale, double averageMaleGrade, double averageFemaleGrade)
{
        ofstream output;
        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;
        }

       output << "Number of Males: " << countMale << endl;
       output << "Average Male GPA: " << setprecision(3) << averageMaleGrade << endl << endl;
       output << "Number of Females: " << countFemale << endl;
       output << "Average Female GPA: " << setprecision(3) << averageFemaleGrade;

       output.close();
        return true;
}

 

Because we're now opening and closing the output file in this function we can now remove all the other code related to the outfile in main and the openFiles function.

This also means that you can now close infile in main after you read the file but before calculating the averages and then call openFiles, readFile again to read from a second file, then calculate the averages.

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

Link to comment
Share on other sites

This is my output in the text file. The male/female counts are correct! But the Average GPA...ughhhhhhh.....don't know how a negative got in there. SO close to being done! Thank you everyone for helping out <3:

 

Did not realize the outfile is pretty much the same thing as cout....

 

Number of Males: 12

 

Average Male GPA: -7.71e+060

 

Number of Females: 8

 

Average Female GPA: -1.16e+061

 

 

Also, it asks for an output file twice...need to get rid of one...but which one....im going to guess the one in side of bool writeData?

 

I misinterpreted my teachers instructions...I had to text the program with several text files. But the program did not need to accept multiple infules.

 

 

 


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

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

int readFile (ifstream&, ofstream&, int&, int&, double&, double&);
double averageGrade(int, double);


int main()
{

int countMale = 0;
int countFemale = 0;
int numlines = 0;
double sumMaleGPA;
double sumFemaleGPA;
double averageMaleGrade;
double averageFemaleGrade;

ifstream infile;
ofstream outfile;

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

numlines  = readFile(infile, outfile, countMale, countFemale, sumMaleGPA, sumFemaleGPA);

averageMaleGrade = averageGrade(countMale, sumMaleGPA);
averageFemaleGrade = averageGrade(countFemale, sumFemaleGPA);

cout << "\n" << ' ' << numlines << "lines read and written.\n";

writeData(countMale,countFemale, averageMaleGrade, averageFemaleGrade);

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


double averageGrade(int count, double sumGPA)
{
double averageGrade = sumGPA / count;
return averageGrade;

}


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


bool writeData(int countMale, int countFemale, double averageMaleGrade, double averageFemaleGrade)
{

string filename;

ofstream output;
   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;
        }

   output << "Number of Males: " << countMale << '\n' << endl;
   output << "Average Male GPA: " << setprecision(3) << averageMaleGrade << '\n' << endl;
   output << "Number of Females: " << countFemale << '\n' << endl;
   output << "Average Female GPA: " << setprecision(3) << averageFemaleGrade;

output.close();
   return true;
}

Link to comment
Share on other sites

You'll want to get rid of the outfile load in the loadFiles function, the one in write data is the one we're using to write the output.

 

This code in the readFile function is also completely superfluous and can be removed:

 

       double average = 0;

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

       average = average / countMale; 

 

As for the outputs being wrong, try throwing in a cout statement on the averageMaleGrade and averageFemaleGrade variables after all of the output << statements. This will dump them to the console so that you can see if they are correct or not. If they're not try backtracking around the program with those cout statements to see where they're not being set properly.

 

Unfortunately I do have to go to bed now, but I'll check back in the morning. :)

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

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.