Homework Help !!

Write your question here.
Problem: Write a C++ program that read 5 people's height and weight data from the given file myinput.txt and then calculate EACH person's body mass index (BMI) using the formula below. After the calculation, your program should output each person's BMI to a file called myoutput.txt.

Note: In each line of the myinput.txt, the first data is the height in inches, the second data is the weight in lbs. Formula for body mass index: weight / (height )2 x 703
#include <fstream>
#include <iostream>
#include <cmath>

using namespace std;
int main()
{

ifstream inFile;
ofstream outFile;

float height1, height2, height3, height4, height5;
float weight1, weight2, weight3, weight4, weight5;
float BMI;
float base;
float square = pow(base,2);

inFile.open("C:/temp/lab4_caverinput.txt");
outFile.open("C:/temp/lab4_caveroutput.txt");

inFile >> height1, height2, height3, height4, height5, weight1, weight2, weight3, weight4, weight5

BMI = weight / (height)pow(2) * 703;

cout << "Please convert height into inches" << endl;
cout << "Please convert weight into lbs" << endl;
cout << "BMI is: " << BMI << endl;
outFile << "BMI is: " << BMI << endl;

inFile.close();
outFile.close();

return 0;

}
what is your question? Also, use code tags.
Last edited on
I was trying to see if this correct because it is confusing. What you mean code tags ?? I never heard of them
shadowCODE meant that on the right of the posting box, there are some buttons. One is for code. You basically copy your code then paste between the two headers.

With that said, you are missing a ';' after inFile >> ... weight5.

Without the use of arrays or classes, your work looks OK until after you read in the heights and weights.

BMI = weight / (height)pow(2) * 703;


This part should be calculated for each height and weight match, as long as that is the correct formula. To this end you should probably consider using a loop (if you have learned about those yet) to do the reading and conversions and output.

Please explain your 2 cout statements regarding conversion. Who is converting height to inches or weight into lbs? Or is that part of what you are supposed to do? Because the way the problem is described, it looks like the numbers go straight from the file to a calculation back to a separate file.

Not sure professionally when a file is supposed to be closed, but personally I close files just after I am completely through with them, i.e. I would close inFile just after the line inFile >> ... statement.
Last edited on
I end up fixing the program but I keep receiving an error known as Error 1 error C4700: uninitialized local variable 'base' used c:\temp\visual studio 2012\hw3_caver\hw3_caver\hw3_caver.source.cpp 22 1 HW3_Caver
check that you are using a variable that has not been initialized.
what do you mean ??
1
2
3
4
5
6
int a;         //uninitialized
int b = 2;  //initialized
int c;         //uninitialized

c = b + 2;  //Legal
c = a + b; //Not Legal. (Error: a initialized) 
but base does have no number though this is my program

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

using namespace std;
int main()
{

ifstream inFile;
ofstream outFile;

float height1, height2, height3, height4, height5;
float weight1, weight2, weight3, weight4, weight5;
float BMI1, BMI2, BMI3, BMI4, BMI5;
float base;
float square = pow(base,2);

inFile.open("C:/temp/hw3_caverinput.txt");
outFile.open("C:/temp/output.txt");

inFile >> height1 >> weight1;
inFile >> height2 >> weight2;
inFile >> height3 >> weight3;
inFile >> height4 >> weight4;
inFile >> height5 >> weight5;

BMI1 = weight1 / pow(height1,2) * 703;
BMI2 = weight2 / pow(height2,2) * 703;
BMI3 = weight3 / pow(height3,2) * 703;
BMI4 = weight4 / pow(height4,2) * 703;
BMI5 = weight5 / pow(height5,2) * 703;

cout << "BMI1 is: " << BMI1 << endl;
outFile << "BMI1 is: " << BMI1 << endl;
outFile << "BMI2 is: " << BMI2 << endl;
outFile << "BMI3 is: " << BMI3 << endl;
outFile << "BMI4 is: " << BMI4 << endl;
outFile << "BMI5 is: " << BMI5 << endl;

inFile.close();
outFile.close();

return 0;

}
use code tags. to wrap your code. Like this.
[code]this is code[/code]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <fstream>
#include <iostream>
#include <cmath>

using namespace std;
int main()
{

ifstream inFile;
ofstream outFile;

float height1, height2, height3, height4, height5; 
float weight1, weight2, weight3, weight4, weight5;
float BMI1, BMI2, BMI3, BMI4, BMI5;
float base;
float square = pow(base,2);

inFile.open("C:/temp/hw3_caverinput.txt");
outFile.open("C:/temp/output.txt");

inFile >> height1 >> weight1;
inFile >> height2 >> weight2;
inFile >> height3 >> weight3;
inFile >> height4 >> weight4;
inFile >> height5 >> weight5;

BMI1 = weight1 / pow(height1,2) * 703;
BMI2 = weight2 / pow(height2,2) * 703;
BMI3 = weight3 / pow(height3,2) * 703;
BMI4 = weight4 / pow(height4,2) * 703;
BMI5 = weight5 / pow(height5,2) * 703;

cout << "BMI1 is: " << BMI1 << endl;
outFile << "BMI1 is: " << BMI1 << endl;
outFile << "BMI2 is: " << BMI2 << endl;
outFile << "BMI3 is: " << BMI3 << endl;
outFile << "BMI4 is: " << BMI4 << endl;
outFile << "BMI5 is: " << BMI5 << endl;

inFile.close();
outFile.close();

return 0;

}

Use code tags < >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <fstream>
#include <iostream>
#include <cmath>

using namespace std;
int main()
{

ifstream inFile;
ofstream outFile;

float height1, height2, height3, height4, height5; 
float weight1, weight2, weight3, weight4, weight5;
float BMI1, BMI2, BMI3, BMI4, BMI5;
float base;
float square = pow(base,2);

inFile.open("C:/temp/hw3_caverinput.txt");
outFile.open("C:/temp/output.txt");

inFile >> height1 >> weight1;
inFile >> height2 >> weight2;
inFile >> height3 >> weight3;
inFile >> height4 >> weight4;
inFile >> height5 >> weight5;

BMI1 = weight1 / pow(height1,2) * 703;
BMI2 = weight2 / pow(height2,2) * 703;
BMI3 = weight3 / pow(height3,2) * 703;
BMI4 = weight4 / pow(height4,2) * 703;
BMI5 = weight5 / pow(height5,2) * 703;

cout << "BMI1 is: " << BMI1 << endl;
outFile << "BMI1 is: " << BMI1 << endl;
outFile << "BMI2 is: " << BMI2 << endl;
outFile << "BMI3 is: " << BMI3 << endl;
outFile << "BMI4 is: " << BMI4 << endl;
outFile << "BMI5 is: " << BMI5 << endl;

inFile.close();
outFile.close();

return 0;

}


Last edited on
Did you understand my last code about uninitialized variable?

you cannot operate on a variable that is not initialized.

1
2
float base;    //uninitialized. base has not value at this point
float square = pow(base,2);  //Result in an error by using an uninitialized variable. 
What code. Where I suppose to insert the bodes
Looks to me like you are not using base OR square in your program, just the pow function later on to calculate BMI. Just get rid of them.
So get rid of float base;
and float square
I got it thank you so much
Topic archived. No new replies allowed.