| | |
Overload Boolean Comparison Operators
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2009
Posts: 4
Reputation:
Solved Threads: 0
I am trying to overload the Comparison Operators, to have them compare the RMR of two fitness trackers. I need to change the RMR from a double to an int for the comparison. How would I do this when the RMR is calculated by a function. Here is my code.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <iomanip> #include "FitnessTracker.h" using namespace std; struct Activity { string name; int burnForLo; // <150lbs int burnForMed; // 150-250 int burnForHi; // >250 }info[12]; void CreateStruct(Activity info[], ifstream &Activities); void StartTracker(FitnessTracker &Tracker); char DisplayAndMenu(FitnessTracker &Tracker); void AddCalories(FitnessTracker &Tracker); void ExerciseMenu(Activity info[], int &exercise); void CalculateCalsBurned(Activity info[], int &exercise, FitnessTracker &Tracker); void EndDay(FitnessTracker &Tracker); void main() { char Menu; int exercise; FitnessTracker Tracker(0, 0, 0, 'M'); ifstream Activities; CreateStruct(info, Activities); StartTracker(Tracker); do { Menu = DisplayAndMenu(Tracker); if (Menu == 'A' || Menu == 'a') AddCalories(Tracker); else if (Menu == 'D' || Menu == 'd') { ExerciseMenu(info, exercise); CalculateCalsBurned(info, exercise, Tracker); } else if (Menu == 'E' || Menu == 'e') EndDay(Tracker); }while (Menu != 'Q' && Menu != 'q'); } void CreateStruct(Activity info[], ifstream &Activities) { int i; Activities.open("exerciseTotals.txt"); for (i=0; i<12; i++) { Activities >> info[i].name >> info[i].burnForLo >> info[i].burnForMed >> info[i].burnForHi; } } void StartTracker(FitnessTracker &Tracker) { cout << endl << " Welcome to the Fitness Tracker!" << endl << endl; Tracker.intializeAge(); Tracker.intializeHeight(); Tracker.intializeWeight(); Tracker.intializeGender(); cout << "Thank you, let's begin." << endl; system("pause"); system("cls"); } char DisplayAndMenu(FitnessTracker &Tracker) { char Menu; cout << Tracker; cout << endl << "Please select an option:" << endl << "A)dd Calories Eaten" << endl << "D)o Some Exercise" << endl << "E)nd The Day" << endl << "Q)uit" << endl << "Your choice? "; cin >> Menu; while ((Menu != 'A') && (Menu != 'a') && (Menu != 'D') && (Menu != 'd') && (Menu != 'E') && (Menu != 'e') && (Menu != 'Q') && (Menu != 'q')) { cout << "That is not a valid option. Please try again: "; cin >> Menu; } return Menu; } void AddCalories(FitnessTracker &Tracker) { int Calories; cout << "How many calories did you eat? "; cin >> Calories; while (Calories < 0) { cout << "You cannot eat fewer than zero calories! Try again." << endl << "How many calories did you eat? "; cin >> Calories; } Tracker.AddCaloriesConsumed(Calories); cout << endl << "*****************" << endl << " " << Calories << " calories added" << endl << "*****************" << endl << endl; } void ExerciseMenu(Activity info[], int &exercise) { int i; do { cout << "Please choose an exercise from the list:" << endl; for (i=0; i<12; i++) { cout << i+1 << ") " << info[i].name << endl; } cout << "13.) None." << endl; cin >> exercise; }while ((exercise < 1) || (exercise > 13)); } void CalculateCalsBurned(Activity info[], int &exercise, FitnessTracker &Tracker) { int CalsBurned; if (Tracker.getWeight() < 150) CalsBurned = info[exercise-1].burnForLo; else if ((Tracker.getWeight() >= 150) && (Tracker.getWeight() <= 250)) CalsBurned = info[exercise-1].burnForMed; else CalsBurned = info[exercise-1].burnForHi; cout << endl << "*****************" << endl << " " << CalsBurned << " calories burned" << endl << "*****************" << endl; Tracker.AddCaloriesBurned(CalsBurned); } void EndDay(FitnessTracker &Tracker) { cout << endl << "**************End of Day**************" << endl; if (Tracker.AdjustWeight() < 0) cout << "You lost " << (Tracker.AdjustWeight()*-1) << " pounds" << endl; else if (Tracker.AdjustWeight() > 0) cout << "You gained" << Tracker.AdjustWeight() << "pounds" << endl; cout << "**************End of Day**************" << endl << endl; Tracker.setCaloriesBurned(0); Tracker.setCaloriesConsumed(0); Tracker.setWeight(Tracker.getWeight()+Tracker.AdjustWeight()); }
C++ Syntax (Toggle Plain Text)
// FitnessTracker.cpp #include <iostream> #include <iomanip> #include "FitnessTracker.h" using namespace std; // constructors: FitnessTracker::FitnessTracker() { } FitnessTracker::FitnessTracker(int A, int H, int W, char G) { Age = A; Height = H; Weight = W; Gender = G; CaloriesBurned = 0; CaloriesConsumed = 0; } // Overloaded Operators ostream & operator << (ostream &os, FitnessTracker &Tracker) { os << "Age: " << Tracker.Age << endl << "Gender: " << Tracker.Gender << endl << "Weight: " << Tracker.Weight << endl << "Height: " << Tracker.Height << endl << "RMR: " << Tracker.RMR() << endl << "Cals Burned: " << Tracker.CaloriesBurned + Tracker.RMR() << endl << "Cals Consumed: " << Tracker.CaloriesConsumed << endl << "Calorie Difference: " << Tracker.CalorieDifference() << endl; return os; } // utility functions: double FitnessTracker::RMR() { if (getGender() == 'M') return (66+(6.22*getWeight())+(12.7*getHeight())-(6.8*getAge())); else return (655+(4.36*getWeight())+(4.32*getHeight())-(4.7*getAge())); } int FitnessTracker::CalorieDifference() { return (getCaloriesConsumed()-(getCaloriesBurned()+RMR())); } double FitnessTracker::AdjustWeight() { return (CalorieDifference()/3500.0); } // intializing functions void FitnessTracker::intializeAge() { int Age; cout << "Please enter your Age: "; cin >> Age; setAge(Age); while (setAge(Age) == false) { cout << "That Age is not valid, please enter your Age again: "; cin >> Age; setAge(Age); } } void FitnessTracker::intializeHeight() { double Height; cout << "Please enter your Height: "; cin >> Height; setHeight(Height); while (setHeight(Height) == false) { cout << "That Height is not valid, please enter your Height again: "; cin >> Height; setHeight(Height); } } void FitnessTracker::intializeWeight() { double Weight; cout << "Please enter your Weight: "; cin >> Weight; setWeight(Weight); while (setWeight(Weight) == false) { cout << "That Weight is not valid, please enter your Weight again: "; cin >> Weight; setWeight(Weight); } } void FitnessTracker::intializeGender() { char Gender; cout << "Please enter your Gender (M or F): "; cin >> Gender; setGender(Gender); while (setGender(Gender) == false) { cout << "That Gender is not valid, please enter your Gender (M or F) again: "; cin >> Gender; setGender(Gender); } } // mutators: bool FitnessTracker::setAge(int A) { if(A <= 0) return false; Age = A; return true; } bool FitnessTracker::setHeight(double H) { if(H <= 0) return false; Height = H; return true; } bool FitnessTracker::setWeight(double W) { if(W <= 0) return false; Weight = W; return true; } bool FitnessTracker::setGender(char G) { if(G == 'm' || G == 'M') { Gender = 'M'; return true; } else if(G == 'f' || G == 'F') { Gender = 'F'; return true; } return false; } bool FitnessTracker::setCaloriesBurned(int CB) { if (CB < 0) return false; CaloriesBurned = CB; return true; } void FitnessTracker::AddCaloriesBurned(int CB) { CaloriesBurned = getCaloriesBurned() + CB; } bool FitnessTracker::setCaloriesConsumed(int CC) { if (CC < 0) return false; CaloriesConsumed = CC; return true; } void FitnessTracker::AddCaloriesConsumed(int CC) { CaloriesConsumed = getCaloriesConsumed() + CC; } // accessors: int FitnessTracker::getAge() { return Age; } double FitnessTracker::getHeight() { return Height; } double FitnessTracker::getWeight() { return Weight; } char FitnessTracker::getGender() { return Gender; } int FitnessTracker::getCaloriesBurned() { return CaloriesBurned; } int FitnessTracker::getCaloriesConsumed() { return CaloriesConsumed; }
C++ Syntax (Toggle Plain Text)
// FitnessTracker.h #ifndef FitnessTracker_H #define FitnessTracker_H #include <iostream> using namespace std; class FitnessTracker { public: // constructors: FitnessTracker(); FitnessTracker(int A, int H, int W, char G); // accessors: int getAge(); double getHeight(); double getWeight(); char getGender(); int getCaloriesBurned(); int getCaloriesConsumed(); // mutators: bool setAge(int A); bool setHeight(double H); bool setWeight(double W); bool setGender(char G); bool setCaloriesBurned(int CB); void AddCaloriesBurned(int CB); bool setCaloriesConsumed(int CC); void AddCaloriesConsumed(int CC); // utility functions: void display(); double RMR(); int CalorieDifference(); double AdjustWeight(); void intializeAge(); void intializeHeight(); void intializeWeight(); void intializeGender(); // Friend Functions friend ostream & operator<<(ostream& , FitnessTracker&); // private: int Age; double Height; double Weight; char Gender; int CaloriesBurned; int CaloriesConsumed; }; #endif
![]() |
Similar Threads
- Overloading Comparison Operators (C++)
- substitue of comparison operators (C++)
- Overloading operators (C++)
- Overloading matrix bracket operators (C++)
Other Threads in the C++ Forum
- Previous Thread: Breaking out of a while loop if condition met
- Next Thread: solving max flow by push relabel using C++
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





