//Lab 5                               Ray Perales 
//This program will compute a user's hat size, jacket size, and waist measurement
// based on a three function modular program.

#include <iostream>
#include <iomanip>
#include <fstream> 
#include <cmath>

// Prototype declarations
double findHatSize (double height, double weight);
double findJacketSize (double height, double weight, int age);
double findWaist (double weight, int age);

using namespace std;

int main()
{   
    ifstream fin;
    fin.open("readLab5.cpp");
    if (fin.fail())
    {
       cerr << "error opening input file. \n\n";
       system("pause");
       exit(1);
    }
    ofstream fout;
    fout.open("outputLab5.out");
    if (fout.fail())
    {
       cerr << "error opening output file.\n\n";
       system("pause");
       exit(1);
    }
    fout.setf(ios::fixed);
    
    int age;
    double height, weight, HatSize, JacketSize, waist;
    fout << "Height: ";
    fout <<setw(7)  <<"Weigtht: ";
    fout <<setw(5)  <<"Age: ";
    fout <<setw(11) <<"Hat size: ";
    fout <<setw(13) <<"Jacket Size: ";
    fout <<setw(9)  <<"Waist: \n";
    
    while (fin >> height >> weight >> age)
    {
    HatSize = findHatSize(height, weight);
    JacketSize = findJacketSize(height, weight, age);
    waist = findWaist(weight, age);
    
    fout.precision(0);
    fout << setw(6) << height;
    fout << setw(6) << weight;
    fout << setw(6) << age;
    fout.precision(4);
    fout << setw(11) << HatSize;
    fout << setw(11) << JacketSize;
    fout << setw(11) << waist << endl;
    }
    
    system("pause");
    return 0;
}

//function definition for hat size.
double findHatSize(double height, double weight)
{
       double hatSize;
       hatSize = (height/weight)*2.90;
       return hatSize;
}
//function definition for jacket size.
double findJacketSize(double height, double weight, int age)
{
       double jacketSize;
       if (age > 30)
          jacketSize = (height * weight / 288) + ((age - 30)/10)* 0.125;
       else
          jacketSize = (height * weight / 288);
       return jacketSize;
}
//function definition for waist size.
double findWaist(double weight, int age)
{
       double waist;
       if (age > 28)
            (weight/ 5.7) + ((age - 28) / 2) * .01;
       else
            waist = (weight/ 5.7);
       return waist; 
} 

/* Input reading int
73 205	20
73 205	55

no output to output file */

Dani AI

Generated

Two quick diagnostics already raised by replies are on the right track: confirm the program is opening the correct data file () and look for logic that can leave values uninitialized (). The following checklist and fixes focus on reliable diagnosis and the two most common silent-failure causes: stream/read failures and subtle arithmetic/initialization bugs.

First, confirm the input file the executable actually sees. Many IDEs set the working directory to a build/bin folder, not the source folder. Either drop the data file beside the produced executable or open it with an absolute path. Add simple runtime checks and diagnostics so you can see what the program does when it runs (whether the open succeeded, whether the while-loop executes, and how many lines are parsed). See the reference for file opening behavior for details: std::ifstream::open.

Second, protect against uninitialized values and integer truncation. Initialize accumulators/returns at declaration (for example, double waist = 0.0;) so a returned value is never indeterminate. Also ensure age-based adjustments use floating-point arithmetic; expressions like (age - 30)/10 will do integer division when age is an int. For correct fractional corrections use a float literal or cast, for example:

double jacketCorrection = (age - 30) / 10.0 * 0.125;
double waistCorrection   = (age - 28) / 2.0  * 0.01;

Finally, use compiler warnings and runtime sanitizers to catch these problems early. Enable -Wall -Wextra and run with Address/UndefinedBehavior sanitizers or valgrind; they will flag uninitialized reads and many other issues. GCC warning options: GCC Warning Options. After fixing the logic and confirming the file path, add a temporary cout dump of read values to confirm output before relying on file output.

Recommended Answers

All 2 Replies

Are you sure about the input file's name ??

fin.open("readLab5.cpp");

You almost got the code tags right, however next time you might want to utilize the "Preview Post" button to make sure that the code tags really are right.

double findWaist(double weight, int age) {
	double waist;
	if (age > 28)
		(weight/ 5.7) + ((age - 28) / 2) * .01;
	else
		waist = (weight/ 5.7);
	return waist;
}

after if (age > 28) it must be waist = (weight/ 5.7) + ((age - 28) / 2) * .01;
and fin.open("readLab5.cpp"); - why .cpp

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.