C++ Program Help: Calculate Average Weight & Height by Age & Gender

  • Comp Sci
  • Thread starter essdb1
  • Start date
  • Tags
    C++ Program
In summary: Your code is not in a state that it can be easily understood. It needs to be cleaned up so that it can be read by a human being.
  • #1
essdb1
1
0
//******************************************************************
//Sherman Brown
//CIS 150-01 Programming in C/C++
//April 17, 2011
//Program #7
//Organizing data by age and gender, calculating average weight/height.
//*******************************************************************

//Preprocess Directives
#include <iostream>
#include <iomanip>
#include <fstream>

//Namespace used
using namespace std;

//global variables
int age;
int count1;
int count2;
int count3;
int count4;
int count5;
int count6;
int minoravgheightF;
int minoravgweightF;
int minoravgheightM;
int minoravgweightM;
int adultavgheightF;
int adultavgweightF;
int adultavgheightM;
int adultavgweightM;
int oldavgheightF;
int oldavgweightF;
int oldavgheightM;
int oldavgweightM;
char gender;
double height;
double weight;


void avgfunc(int minoravgheightF,int minoravgweightF,int adultavgheightF,int adultavgweightF,int oldavgheightF,int oldavgweightF,int minoravgheightM,int minoravgweightM,int adultavgheightM,int adultavgweightM,int oldavgheightM,int oldavgweightM);
void printavg();

//main function/value returning function
int main()
{

ifstream inputfile; //input stream variable
ofstream outputfile; //output stream variable

inputfile.open("Prog7Input.txt"); //opens file

if (!inputfile) //if...else ensures file opens correctly
{
cout << "File not found." << endl;
cout << "Ending now." << endl;
}
else
{
inputfile >> gender >> age >> height >> weight; //gets first line of input from file
}

while (!inputfile.eof()) //necessary .eof like stated in assignment
{
void avgfunc();
inputfile >> gender >> age >> height >> weight;
}
void printavg();


return 0;
}
//******************************************************************
void avgfunc() //void function that reads and processes
{
if ((gender == 'f') || (gender == 'F'))
{
if ((age >= 0) && (age <=18), count1++)
{
minoravgheightF = minoravgheightF + height;
minoravgweightF = minoravgweightF + weight;
}
else if ((age >= 19) && (age <=35), count2++)
{
adultavgheightF = adultavgheightF + height;
adultavgweightF = adultavgweightF + weight;
}
else if (age >=36, count3++)
{
oldavgheightF = oldavgheightF + height;
oldavgweightF = oldavgweightF + weight;
}
else
{
cout << "Unanalyzable data." << endl;
}
}
else if ((gender == 'm') || (gender == 'M'))
{
if ((age >= 0) && (age <=18), count4++)
{
minoravgheightM = minoravgheightM + height;
minoravgweightM = minoravgweightM + weight;
}
else if ((age >= 19) && (age <=35), count5++)
{
adultavgheightM = adultavgheightM + height;
adultavgweightM = adultavgweightM + weight;
}
else if (age >=36, count6++)
{
oldavgheightM = oldavgheightM + height;
oldavgweightM = oldavgweightM + weight;
}
else
{
cout << "Unanalyzable data." << endl;
}

}
//******************************************************************
void printavg() //void function that print to outfile
{
outputfile << "Age Range: Male Height: Male weight: Female Height: Female Weight:" << endl;
outputfile << fixed << showpoint << setprecision(1);
outputfile << setw(6) << "0-18" << setw(13)
<< minoravgheightM / count1 << setw(13)
<< minoravgweightM / count1 << setw(13)
<< minoravgheightF / count2 << setw(13)
<< minoravgweightF / count2 << endl;
outputfile << setw(6) << "19-35" << setw(13)
<< adultavgheightM / count3 << setw(13)
<< adultavgweightM / count3 << setw(13)
<< adultavgheightF / count4 << setw(13)
<< adultavgweightF / count4 << endl;
outputfile << setw(6) << "36+" << setw(13)
<< oldavgheightM / count5 << setw(13)
<< oldavgweightM / count5 << setw(13)
<< oldavgheightF / count6 << setw(13)
<< oldavgweightF / count6 << endl;
;
}






















One, I have no clue how to fix this. Yes it is an assignment as I clearly left that viewable, I just need a little help.
And showing me how to make this code easily viewable on this site would be useful!
 
Physics news on Phys.org
  • #2
What you want to do is enclose your code in code tags. Put [ CODE] in front and [ /CODE] at the end, removing the space I put after the opening square bracket. And thanks for caring about that! :)

Code:
//******************************************************************
//Sherman Brown
//CIS 150-01 Programming in C/C++
//April 17, 2011
//Program #7
//Organizing data by age and gender, calculating average weight/height.
//*******************************************************************

//Preprocess Directives
#include <iostream>
#include <iomanip>
#include <fstream>

//Namespace used
using namespace std;

//global variables
int age;
int count1;
int count2;
int count3;
int count4;
int count5;
int count6;
int minoravgheightF;
int minoravgweightF;
int minoravgheightM;
int minoravgweightM;
int adultavgheightF;
int adultavgweightF;
int adultavgheightM;
int adultavgweightM;
int oldavgheightF;
int oldavgweightF;
int oldavgheightM;
int oldavgweightM;
char gender;
double height;
double weight;void avgfunc(int minoravgheightF,int minoravgweightF,int adultavgheightF,int adultavgweightF,int oldavgheightF,int oldavgweightF,int minoravgheightM,int minoravgweightM,int adultavgheightM,int adultavgweightM,int oldavgheightM,int oldavgweightM);
void printavg();

//main function/value returning function
int main()
{

	ifstream inputfile;	//input stream variable
	ofstream outputfile;	//output stream variable

	inputfile.open("Prog7Input.txt"); //opens file

	if (!inputfile) //if...else ensures file opens correctly
	{
		cout << "File not found." << endl;
		cout << "Ending now." << endl;
	}
	else
	{
		inputfile >> gender >> age >> height >> weight; //gets first line of input from file
	}
	
	while (!inputfile.eof()) //necessary .eof like stated in assignment
	{
		void avgfunc();
		inputfile >> gender >> age >> height >> weight;
	}
void printavg();	return 0;
}
//******************************************************************
void avgfunc() //void function that reads and processes
{
		if ((gender == 'f') || (gender == 'F'))
		{
			if ((age >= 0) && (age <=18), count1++)
			{
				minoravgheightF = minoravgheightF + height;
				minoravgweightF = minoravgweightF + weight;
			}
			else if ((age >= 19) && (age <=35), count2++)
			{
				adultavgheightF = adultavgheightF + height;
				adultavgweightF = adultavgweightF + weight;
			}
			else if (age >=36, count3++)
			{
				oldavgheightF = oldavgheightF + height;
				oldavgweightF = oldavgweightF + weight;
			}
			else
			{
				cout << "Unanalyzable data." << endl;
			} 
		}
		else if ((gender == 'm') || (gender == 'M'))
		{
			if ((age >= 0) && (age <=18), count4++)
			{
			minoravgheightM = minoravgheightM + height;
			minoravgweightM = minoravgweightM + weight;
			}
			else if ((age >= 19) && (age <=35), count5++)
			{
			adultavgheightM = adultavgheightM + height;
			adultavgweightM = adultavgweightM + weight;
			}
			else if (age >=36, count6++)
			{
			oldavgheightM = oldavgheightM + height;
			oldavgweightM = oldavgweightM + weight;
			}
	else
		{
		cout << "Unanalyzable data." << endl;
		} 
		
}
//******************************************************************
void printavg() //void function that print to outfile
{
	outputfile << "Age Range:  Male Height:  Male weight:  Female Height:  Female Weight:" << endl;
	outputfile << fixed << showpoint << setprecision(1);
	outputfile << setw(6) << "0-18" << setw(13)
				  << minoravgheightM / count1 << setw(13) 
				  << minoravgweightM / count1 << setw(13) 
				  << minoravgheightF / count2 << setw(13) 
				  << minoravgweightF / count2 << endl;
	outputfile << setw(6) << "19-35" << setw(13)
				  << adultavgheightM / count3 << setw(13)
				  << adultavgweightM / count3 << setw(13)
				  << adultavgheightF / count4 << setw(13)
				  << adultavgweightF / count4 << endl;
	outputfile << setw(6) << "36+" << setw(13)
				  << oldavgheightM / count5 << setw(13)
				  << oldavgweightM / count5 << setw(13)
				  << oldavgheightF / count6 << setw(13)
				  << oldavgweightF / count6 << endl;
;
}

Some of the problems that jump out at me are:
1. A massive list of global variables. Avoid global variables unless you truly need them (and that should happen from almost never to never).
2. That parameter list in the avgfunc() function is crazy long. You could put them into a structure and pass the structure to any function that needs all those parameters. Or perhaps they can be organized differently. For example, you could have the function only need avgHeight and avgWeight (plus any other important stuff I'm missing), and call it for each case (minor/male, minor/female) etc. In any case, that data can almost certainly be better organized.
3. The declaration of avgfunc() has a ton of parameters, but the definition does not. The parameter lists must match.
4. This is c++, so perhaps this should be a class?

A close look at your main is needed, as well. This is the part I mean:
Code:
        if (!inputfile) //if...else ensures file opens correctly
	{
		cout << "File not found." << endl;
		cout << "Ending now." << endl;
	}
	else
	{
		inputfile >> gender >> age >> height >> weight; //gets first line of input from file
	}
	
	while (!inputfile.eof()) //necessary .eof like stated in assignment
	{
		void avgfunc();
		inputfile >> gender >> age >> height >> weight;
	}
void printavg();	return 0;
}
After checking to make sure the file opened correctly, you should use this kind of thing to loop until it hits EOF:
Code:
while (inputfile >> gender >> age >> height >> weight)
{
    // Process line
}
You shouldn't have to read a line in before going into the while loop, nor check for EOF explicitly.

You also appear to be trying to call avgfunc wrong (same with printavg). It's not:

void avgfunc();

Ignoring the fact that you declared it to need a big list of parameters, which are missing, that void is wrong. It should be just:

avgfunc(...parameters go here...);

Lastly, since this is homework help, note it says not to post homework here in the stickied posts. The proper forum for that is:

Science Education > Homework & Coursework Questions > Engineering, Comp Sci, & Technology

Don't worry, we check that one too. Wish it wasn't mixed in with engineering and such, but what can you do.

That's what I see after a quick pass through, anyways. Hope that helps!

EDIT: Missed one thing. When you detect that there was a problem opening the file, you say "Ending now.", but you don't. Pretty much all the code for when it does open correctly should be in the else block.

ANOTHER EDIT: He asked you to call .eof() in the assignment? Ouch. You should read these short pages:

http://www.fredosaurus.com/notes-cpp/io/cinloop.html

http://www.fredosaurus.com/notes-cpp/io/anti-idiom-cin.html
 
Last edited:

What is the purpose of this program?

The purpose of this program is to calculate the average weight and height for individuals of different ages and genders using data input by the user.

How is the average weight and height calculated?

The average weight and height are calculated by taking the sum of all weights and heights for a specific age and gender group and dividing it by the number of individuals in that group.

What information do I need to input for the program to work?

You will need to input the age, gender, weight, and height for each individual you want to calculate the average for. The program will prompt you for this information.

What if I make a mistake in inputting data?

If you make a mistake in inputting data, the program will prompt you to re-enter the correct information. It is important to double check your inputs to ensure accurate results.

Can this program be used for any age or gender?

Yes, this program can be used for any age and gender. However, it is important to note that the accuracy of the results may vary depending on the sample size and diversity of the data input.

Back
Top