Unexpectedly moved decimal points in C ++

This is my first stackoverflow post. I am currently retraining C ++, and it is difficult for me to understand what happens in the process of unexpectedly changing the value of the double userBase [j] .height variable in this system.

As you can see in this first shot, everything works fine: enter image description here

Now, every time I want to print a table of all the collected information from the 5th array of the structure, the values ​​for the height suddenly changed and it moved 2 decimal points. enter image description here

I am currently figuring out where this problem comes from. I suspect that this came from setprecision (), but I do not know how this happened.

My source code:

#include <iostream>    
#include <iomanip>
#include <windows.h>

using namespace std;

const int size = 5;

struct users{

    double height, ft, in, weight, age;
    double bmi;
    int bmiLevel;
    string gender;
};

int main(){


users userBase[size];

    for(int i = 0; i<size; i++){

    cout << "Enter your weight (lbs): ";
    cin  >> userBase[i].weight;
    userBase[i].weight = userBase[i].weight*0.453592;


    cout << "Enter your height (feet): ";
    cin  >> userBase[i].ft;
    cout << "Enter your height (inches): ";
    cin  >> userBase[i].in;
    userBase[i].ft = userBase[i].ft * 30.48;
    userBase[i].in = userBase[i].in * 2.54;
    userBase[i].height = userBase[i].ft + userBase[i].in;

    cout << "\n\n\nheight and weight are converted to kilograms and centimeters respectively \n\n";

    cout << "Weight: " << userBase[i].weight << " kg" << endl;
    cout << "Height: " << userBase[i].height << " cm" << endl;

    userBase[i].height = userBase[i].height * 0.01;
    userBase[i].bmi = userBase[i].weight / (userBase[i].height*userBase[i].height);
    cout << "Your Body Mass Index (BMI): " << userBase[i].bmi << endl;

    if(userBase[i].bmi<18){
        cout << "Underweight! ";
    }
    if(userBase[i].bmi>18 && userBase[i].bmi<25){
        cout << "Ideal";
    }
    if(userBase[i].bmi>25 && userBase[i].bmi<30){
        cout << "Overweight";
    }
    if(userBase[i].bmi>30){
        cout <<"Obese";
    }
    system("CLS");
    }


    cout<<left<<fixed<<setprecision(2);
         cout<<"Weight         Height        BMI       Status \n";
        for(int j = 0; j < size; j++){

             cout<<setw(0)<<userBase[j].weight
                 <<setw(10)<<"kg"
                 <<setw(0)<<userBase[j].height
                 <<setw(10)<<"cm"
                 <<setw(10)<<userBase[j].bmi;

    if(userBase[j].bmi<18){
        cout << "Underweight! ";
    }
    if(userBase[j].bmi>18 && userBase[j].bmi<25){
        cout << "Ideal";
    }
    if(userBase[j].bmi>25 && userBase[j].bmi<30){
        cout << "Overweight";
    }
    if(userBase[j].bmi>30){
        cout <<"Obese";
    }
    cout << endl;
             }
}

I tried to figure it out now for about an hour, and any help given to this post would be greatly appreciated!

+4
source
1

, :

, 0,01, .

, , , 100, .

, @someprogrammerdude !

+3

Source: https://habr.com/ru/post/1693970/


All Articles