Skip to content

Instantly share code, notes, and snippets.

@Parables
Last active June 13, 2020 00:40
Show Gist options
  • Select an option

  • Save Parables/2313a54ab95215e6b2fa0695ee67cc88 to your computer and use it in GitHub Desktop.

Select an option

Save Parables/2313a54ab95215e6b2fa0695ee67cc88 to your computer and use it in GitHub Desktop.
BMI calc
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
using namespace std;
//
typedef struct{
double bmiValue;
int category;
}BMI;
typedef struct {
string name;
int age;
double weight;
double height;
BMI bmi;
}Student;
//array of student
Student students[30];
int option=0, lastIndex =-1;
string categories[7]= {"Under Weight", "Normal Weight", "Over Weight", "Obese", "Severely Obese", "Morbidly Obese", "Super Obese"};
// function prototype
BMI calculateBMI( double , double );
int getCategory(double);
void showMenu();
void printData();
int main() {
do showMenu();
while(option >=1 && option <=2);
cout<<"Invalid option... programme terminated";
}
void showMenu(){
option = 0;
cout<<"\n\nStudent BMI Calc"<<endl;
cout<<"\t 1. Add a student\n\t 2. Print student records"<<endl;
cout<<"Enter your option: "; cin>>option;
if (option == 1){
Student newStudent;
cout<<"Enter the student's name: "; cin>> newStudent.name;
cout<<"Enter the student's age: "; cin>> newStudent.age;
if(newStudent.age>=18){
cout<<"Enter the student's height: "; cin>> newStudent.height;
cout<<"Enter the student's weight: "; cin>> newStudent.weight;
newStudent.bmi = calculateBMI(newStudent.weight, newStudent.height);
}
lastIndex +=1;
students[lastIndex] = newStudent;
cout<<"New student "<<students[lastIndex].name<<" has been added.... bmi: "<<students[lastIndex].bmi.bmiValue<<", category: "<<categories[students[lastIndex].bmi.category]<<endl;
} else if (option == 2){
printData();
}
}
BMI calculateBMI( double weight, double height){
BMI bmi;
bmi.bmiValue = weight/ (height*height);
bmi.category = getCategory(bmi.bmiValue);
return bmi;
}
int getCategory(double bmiVal)
{
if(bmiVal <= 18.4 ) return 0;
else if(bmiVal >= 18.5 && bmiVal<= 24.9) return 1;
else if(bmiVal >= 25.0 && bmiVal<= 29.9) return 2;
else if(bmiVal >= 30.0 && bmiVal<= 34.9) return 3;
else if(bmiVal >= 35.0 && bmiVal<= 39.9) return 4;
else if(bmiVal >= 40.0 && bmiVal<= 49.9) return 5;
else return 6;
}
//Print Results
void printData(){
int aboveCount,belowCount;
stringstream studentsAbove, studentsBelow;
lastIndex += 1;
cout<<"Printing out "<<lastIndex<<" stdent records"<<endl;
for(int i=0; i<lastIndex; i++){
if(students[i].age>=18) {
aboveCount+=1;
int catIndex = students[i].bmi.category;
studentsAbove << setw(20) << left <<students[i].name<< setw(8) << left <<students[i].age<<setw(8) << left <<students[i].height<<setw(8) << left <<students[i].weight<<setw(8) << left <<students[i].bmi.bmiValue<<setw(30)<<left<<categories[catIndex]<<endl;}
else{
belowCount+=1;
studentsBelow << setw(20) << left <<students[i].name<< setw(8) << left <<students[i].age<<endl;
}
}
cout<<"\n\nSTUENT UNDER 18: "<<belowCount<<"\n************************************************\n"<<setw(20) << left<< "Name"<<setw(8) << left <<"Age\n************************************************"<<endl;
cout<<studentsBelow.str()<<"\n************************************************"<<endl;
cout<<"\n\nSTUDENTS ABOVE 18: "<<aboveCount<<"\n******************************************************************\n"<<setw(20) << left<< "Name"<<setw(8) << left <<"Age"<<setw(8) << left <<"Height"<<setw(8) << left<<"Weight"<<setw(8) << left <<"BMI"<<setw(30)<<left<<"BMI Category"<<"\n******************************************************************"<<endl;
cout<<studentsAbove.str()<<"\n******************************************************************"<<endl;
lastIndex -= 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment