I'm not familiar with c . I've been doing a question. I need to pass the values from the first two functions(weight & height) to calcbmi function. There's an error in for loop in calcbmi function. Also it says "cannot convert 'float*' to 'float'" in main method calcbmi.
#include <iostream>
#include <cmath>
using namespace std;
void inputweight(float weight[], int size) {
cout << "Enter weights in kilogram: ";
int i = 0, w;
while (i < size) {
cin >> w;
if (w >= 45 && w <= 200) {
weight[i] = w;
i ;
}
else{
cout << "The value should be between 45-200." << endl;
}
}
cout << "Weights: ";
for (int i = 0; i < size; i ) {
cout << weight[i] << " ";
}
cout << endl;
}
void inputheight(float height[], int size) {
float h;
cout << "Enter height in cm: ";
for (int i = 0; i < size; ) {
cin >> h;
if (h >= 140 && h <= 200) {
height[i] = h;
i ;
}
}
cout << "Heights: ";
for (int i = 0; i < size; i ) {
cout << height[i] << " ";
}
cout << endl;
}
void calcbmi (float weight, float height, float bmi, int size) {
for (int i = 0; i < size; i ) {
bmi[i] = weight[i] / (height[i] / 100 * height[i] / 100);
}
cout << "BMI values:";
for (int i = 0; i < size; i ) {
cout << bmi[i] << " " << endl;
}
}
int main() {
int size;
cout << "Enter numebr of students: ";
cin >> size;
float weight[size], height[size], bmi[size];
inputweight(weight, size);
inputheight(height, size);
calcbmi(weight, height, bmi, size);
return 0;
}
Any help would be appreciated.
CodePudding user response:
Your calcbmi function should take arrays, not floats:
void calcbmi(float weight[], float height[], float bmi[], int size)
Your program will work after making that change. However, there are other issues, for example, you are creating arrays of variable size, which is not allowed in strict c . You might want to consider using std::vector, which will make much of this easier
CodePudding user response:
Look at void calcbmi (float weight,float height, float bmi, int size)
and you'll see that bmi
is a float
- not a float[]
, so you can't do bmi[i] = ...
. The same goes for weight
and height
.
The proper signature of the function should be:
void calcbmi (float weight[],float height[], float bmi[], int size)
Your program is however not strictly conforming C since you are using variable length arrays:
float weight[size],height[size],bmi[size];
I suggest replacing those with:
std::vector<float> weight(size), height(size), bmi(size);
It could look like this:
#include <iostream>
#include <limits>
#include <string_view>
#include <vector>
// One function that can be used for both weights and heights:
bool inputvector(std::vector<float>& values, std::string_view heading,
std::string_view unit, float min, float max)
{
std::cout << "Enter " << heading << "s in " << unit << '\n';
for(unsigned i = 0; i < values.size();) {
std::cout << heading << ' ' << (i 1) << ": ";
// check that input succeeds and deal with failure
if(not(std::cin >> values[i])) {
// something went bad
if(std::cin.eof()) return false; // no way to recover
// some bad input, clear the failbit and empty the input buffer:
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input, try again.\n";
} else if(values[i] >= min && values[i] <= max) {
i;
} else {
std::cout << "Only " << heading << "s between " << min << " and "
<< max << ' ' << unit << " are allowed, try again.\n";
}
}
std::cout << heading << "s:";
for(auto value : values) std::cout << ' ' << value;
std::cout << '\n';
return true;
}
void calcbmi(std::vector<float>& weight, std::vector<float>& height,
std::vector<float>& bmi)
{
for(unsigned i = 0; i < bmi.size(); i ) {
bmi[i] = weight[i] / (height[i] / 100 * height[i] / 100);
}
std::cout << "BMI values:";
for(auto value : bmi) std::cout << ' ' << value;
std::cout << '\n';
}
int main() {
int size;
std::cout << "Enter number of students: ";
// check that input succeeds and that it's greater than zero:
if(std::cin >> size && size > 0) {
std::vector<float> weight(size), height(size), bmi(size);
if(inputvector(weight, "weight", "kg", 45, 200) &&
inputvector(height, "height", "cm", 140, 200))
{
calcbmi(weight, height, bmi);
}
}
}
If you are not going to use bmi
after calcbmi
, you don't really need to declare it outside the calcbmi
function. You could just calculate the BMI values and print them directly in the calcbmi
function.