Dev-Cpp
Dev-Cpp copied to clipboard
Access Violation at Address 0010BE4B in module 'DevCpp.exe'.
Read of address 00000000
Do you have code to reproduce the error ... ?
This seems to happen specifically when I create separate classes and header files:
This is my main file "main.cpp":
`#include
/* Part 1.1: Calculate BMI */ float GetBMI(float weight, float height) { return weight/(pow(height,2)); }
/* Part 1.2: BMI Categories */ string GetBMICategory(float BMI) { if (BMI < 18.5) { return "Underweight";
}
else if ((BMI >= 18.5) && (BMI <= 24.9))
{
return "Normal Weight";
}
else if ((BMI >= 25) && (BMI <= 29.9))
{
return "Overweight";
}
else
{
return "Obese";
}
}
/* Part 1.3: Estimating Ideal Weight Based on Height and Age using Devine Formula / float GetIdealBodyWeight(float height, string gender) { float m_heightInches, m_inchesOver5feet; m_heightInches = height39.3701; m_inchesOver5feet = m_heightInches - 60; // Note: 60 inches is basically 5 feet
if(gender == "m")
{
return 50 + 2.3 * m_inchesOver5feet;
}
else if (gender == "f")
{
return 45.5 + 2.3 * m_inchesOver5feet;
}
}
/* Part 2.1: Calculate the Basal Metabolic Rate (BMR) / float GetBasalMetabolicRate(float weight, float height, string gender, int age) { if(gender == "m") //Men { return 66 + (13.7weight) + (5height) - (6.8age); } else if (gender == "f") //Women { return 655 + (9.6weight) + (1.8height) - (4.7*age); } }
/* Part 2.2: Calculate the Calories / float GetCalories(int activityLevel, float BMR) { if(activityLevel == 1) //Sedentary { return BMR1.2; } else if(activityLevel == 2) //Lightly Active { return BMR1.375; } else if(activityLevel == 3) //Moderately Active { return BMR1.55; } else //Very Active { return BMR*1.725; }
}
/* Main Program Body / int main(int argc, char* argv) { /* User Inputs */ float m_weight, m_height, m_BMI, m_idealBodyWeight, m_BMR, m_calories; int m_activityLevel, m_age, m_sumCaloriesEatenDaily; string m_gender, m_BMICategory;
cout<<"Please input your gender, Please input \'m' for Male and \'f' for Female: "<<endl;
cin>>m_gender;
cout<<"Please input your Age:"<<endl;
cin>>m_age;
cout<<"Please input your Weight(kg):"<<endl;
cin>>m_weight;
cout<<"Please input your Height(m):"<<endl;
cin>>m_height;
cout<<"\nPlease Select \'1, 2, 3, or 4' Based on Your Weekly Level of Activity:"<<endl;
cout<<"====Select===========================================================Activity Level======================"<<endl;
cout<<"==== 1 ================================== Sedentary (little Or No Exercise)"<<endl;
cout<<"==== 2 ================================== Lightly Active (light Exercise / Sports 1-3 days/week)"<<endl;
cout<<"==== 3 ================================== Moderately Active (moderate exercise / Sports 3-5 days/week)"<<endl;
cout<<"==== 4 ================================== Very Active (Hard Exercise / Sports 6-7 days/week)"<<endl;
cin>>m_activityLevel;
//Double Check Values
while ((m_activityLevel < 1) || (m_activityLevel > 4))
{
cout<<"Error, Please re-input Your Weekly Level of Activity based on the Table given Above:"<<endl;
cin>>m_activityLevel;
}
//BMI
m_BMI = GetBMI(m_weight, m_height);
std::cout << std::setprecision(2) << std::fixed;
cout<<"Your BMI is: "<<m_BMI<<endl;
//BMI Categories
m_BMICategory = GetBMICategory(m_BMI);
cout<<"Your BMI Category is: "<<m_BMICategory<<endl;
//Ideal Body Weight
m_idealBodyWeight = GetIdealBodyWeight(m_height, m_gender);
std::cout << std::setprecision(2) << std::fixed;
cout<<"Your Ideal Weight is: "<<m_idealBodyWeight<<"kg"<<endl;
//Basal Metabolic Rate (BMR)
m_BMR = GetBasalMetabolicRate(m_weight, m_height, m_gender, m_age);
std::cout << std::setprecision(2) << std::fixed;
cout<<"Your Basal Metabolic Rate (BMR) is: "<<m_BMR<<"cal/day"<<endl;
//Calories Needed
m_calories = GetCalories(m_activityLevel, m_BMR);
std::cout << std::setprecision(2) << std::fixed;
cout<<"Your Daily Required Calorie Intake is: "<<m_calories<<"cal/day"<<endl;
//Generate Malaysian Food Menu
FoodMenu foodMenu;
foodMenu.SetFoodChoice();
m_sumCaloriesEatenDaily = foodMenu.GetFoodChoiceCalories();
cout<<"Your Daily Calorie Intake Based on Your Food Choice is: "<<m_sumCaloriesEatenDaily<<"cal/day"<<endl;
return 0;
}
`
My Header File "FoodMenu.h" `#ifndef FOODMENU_H #define FOODMENU_H
#include
class FoodMenu { public: FoodMenu(); void SetFoodChoice(); int GetFoodChoiceCalories();
protected:
//string m_malaysianFoodList[10];
//int m_malaysianFoodCalories[10];
};
#endif `
My Class File "FoodMenu.cpp"
`#include "FoodMenu.h"
#include
//Part 3: Food Menu and Food Choice Calories string m_malaysianFoodList[10] = {"Nasi Lemak", "Mee Goreng", "Roti Canai", "Nasi Goreng", "Murtabak", "Rendang", "Kuey Teow", "Nasi Kandar", "Curry Puff", "Pisang Goreng"}; int m_malaysianFoodCalories[10] = {400, 660, 318, 289, 674, 162, 744, 720, 246, 252}; int m_userFoodChoice[4];
FoodMenu::FoodMenu() {
cout<<"\n"<<endl;
cout<<"====MALAYSIAN FOOD=======================CALORIES====="<<endl;
int j = 1;
for(int i = 0; i < 10; i++) //Malaysian Foods Input
{
cout<<j<<"."<<m_malaysianFoodList[i]<<" "<<m_malaysianFoodCalories[i]<<endl;
j++;
}
}
void SetFoodChoice() {
//Ask User to Input
cout<<"Based on the Above Food List of Popular Malaysian Food, Key in \'1,2,3...or 10' \nBased on what you eat daily for your typical meals:"<<endl;
//Breakfast
cout<<"Please input in \'1,2,3...or 10' for What you Normally Eat for Breakfast:";
cin>>m_userFoodChoice[0];
//Lunch
cout<<"Please input in \'1,2,3...or 10' for What you Normally Eat for Lunch:";
cin>>m_userFoodChoice[1];
//Tea-time
cout<<"Please input in \'1,2,3...or 10' for What you Normally Eat for Tea-time:";
cin>>m_userFoodChoice[2];
//Dinner
cout<<"Please input in \'1,2,3...or 10' for What you Normally Eat for Dinner:";
cin>>m_userFoodChoice[3];
//Store the foodName/Choices in Arrays
}
int GetFoodChoiceCalories() {
int m_index;
int m_foodChoiceCalories[4];
int m_sumCalories;
for(int i = 0; i<4; i++)
{
m_index = m_userFoodChoice[i];
m_foodChoiceCalories[i] = m_malaysianFoodCalories[m_index];
}
m_sumCalories = accumulate(begin(m_foodChoiceCalories), end(m_foodChoiceCalories), 0, plus<int>());
return m_sumCalories;
} `
Not sure if the problem is the code or is it a bug.
Thank you for the help =)