How can i improve my code and make it more readable as well as maintainable(3 months on and off with C++)

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
 #include"Fitness.h"




float Fitness::CalculateMacroRatio(float calories, float carbperc, float fatperc, float protienperc) {
	float carbformula{ carbperc / 100.0f }; float fatformula{ fatperc / 100.0f }; float proteinformula{ protienperc / 100.0f };
	//Calories from each macros
	kcal = calories;
	kcalcarbs = (calories * carbformula);
	kcalfat = (calories * fatformula);
	kcalprotein = (calories * proteinformula);
	//Now for the required grams needed for each macro
	//carbs 4 calories per gram
	//protien 4 calories per gram
	//fat 9 calories per gram
	carbs = kcalcarbs / 4;
	fat = kcalfat / 9;
	protien = kcalprotein / 4;
	return 1.0f;

}
//Calculates how many kcal you need also your bodyweight is converted into pounds from kg

float Fitness::DetermaineHowManyKcalRequired(float bodyweight, Fitness::ActivityLevels activitylevel, std::string unitmeasurement) {
	if (unitmeasurement == "lbs" || unitmeasurement == "LBS") {
		kcal = bodyweight * activitylevel;
		return kcal;
	}
	else if (unitmeasurement == "kg" || unitmeasurement == "KG") {
		BodyWeightInKG = bodyweight * 2.20462f;//Converts to pounds so i can do the fomula
		kcal = BodyWeightInKG * activitylevel;
		return kcal;
	}

}

float Fitness::ChangeBodyWeight(Fitness::ChangeWeight Option, Fitness::ChangeMass Speed) {
	if (Option == LooseWeight) {
		if (Speed == Slow) {
			deficitkcal = kcal - _slow;
			return deficitkcal;
		}
		else if (Speed == Normal) {
			deficitkcal = kcal - _normal;
			return deficitkcal;
		}
		else if (Speed == Fast) {
			deficitkcal = kcal - _fast;
			return deficitkcal;
		}
		else if (Speed == ReallyFast) {
			deficitkcal = kcal - _reallyfast;
			return deficitkcal;
		}
	}

}

std::string Fitness::MeasureMentUnitConverter(std::string Measurementuni) {
	if (Measurementuni == "a" || Measurementuni == "A") {
		return "lbs";
	}
	else if (Measurementuni == "b" || Measurementuni == "B") {
		return "kg";
	}

}

void Fitness::ShowVitaminInfo() {
	std::cout << "Your in the vitamin section bro\n";
}

bool Fitness::DetermineIfUserWantsVitamins(char usroption) {
	if (usroption == 'd' || usroption == 'D') {//I know i could have used one condition and forced for it to be lowercase/uppercase but i can't be bothered :(
		ShowVitaminInfo();
		return true;
	}
}

bool Fitness::DetermineUserOptionLogicPrint(char ursoption) {
	if (ursoption == 'a' || ursoption == 'A' || ursoption == 'b' || ursoption == 'B' || ursoption == 'c' || ursoption == 'C')
		return false;
}

float Fitness::StartPromt_1() {
	std::cout << "\t\t\t\tFitness pro get fit get learn\n";
	std::cout << "[a]: Workout-out how many calories you need?\n";
	std::cout << "[b]: Workout-out how many calories you need and macros?\n";
	std::cout << "[c]: Workout-out how many macros you need?\n";
	std::cout << "[d]: See how many vitamins you require for sleep?\n";//Right now it is more general
	std::cout << ">>>";
	std::cin >> UserOption;
	float Bodyweight_{};
	if (!DetermineUserOptionLogicPrint(UserOption)) {
		std::cout << "Please enter your bodyweight in either KG or lbs\n";
		std::cout << ">>>";
		std::cin >> Bodyweight_;
		std::cout << "[a]: Would you like your measurement in pounds(lbs)?\n";
		std::cout << "[b]: Would you like your measurement in kilograms(kg)?\n";
		std::cout << ">>>";
		return Bodyweight_;
	}
	//Need to work on this part of the code <<TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	else {
		std::cout << "Vitmains here bro\n";
	}
	
	
}

void Fitness::Start() {
	float Bodyweight_ = StartPromt_1();
	std::string Measurementunit{};
	std::cin >> Measurementunit;
	switch (UserOption) {
	case'a':
		char actvitylevelinput_;
		StartPromt1();
		std::cout << ">>>";
		std::cin >> actvitylevelinput_;
		std::cout << "You need " << DetermaineHowManyKcalRequired(Bodyweight_, ConvertActivityLevelToOptions(actvitylevelinput_), MeasureMentUnitConverter(Measurementunit)) << " calories to maintain your bodyweight\n";
		break;
	case'b':
		StartPromt1();
		std::cout << ">>>";
		std::cin >> actvitylevelinput_;
		float* temp = new float[3];
		temp = StoreMacroPercentages();
		CalculateMacroRatio(DetermaineHowManyKcalRequired(Bodyweight_, ConvertActivityLevelToOptions(actvitylevelinput_), MeasureMentUnitConverter(Measurementunit)), temp[0], temp[1], temp[2]);
		DisplayInfo();
		delete[] temp;
		break;
	};



}

float* Fitness::StoreMacroPercentages() {
	float* macropercent = new float[3];
	std::cout << "Enter Ratio you would like for your carbs?:";
	float carbper{}, fatper{}, protienper{};
	std::cin >> carbper;
	std::cout << "Enter Ratio you would like for your fats?:";
	std::cin >> fatper;
	std::cout << "Enter Ratio you would like for your protien?:";
	std::cin >> protienper;
	macropercent[0] = carbper;
	macropercent[1] = fatper;
	macropercent[2] = protienper;
	//didnt deallocate memory
	return macropercent;
}

void Fitness::StartPromt1() {
	std::cout << "Please enter your activity level from\n";
	std::cout << "[a]:Do nothing in the day? \n";
	std::cout << "[b]:Ocassionally move around? \n";
	std::cout << "[c]:A few activities in the day(somewhat active)? \n";
	std::cout << "[d]:Very active throughout the day? \n";
}

Fitness::ActivityLevels Fitness::ConvertActivityLevelToOptions(char optionforactivity) {
	if (optionforactivity == 'a') {
		return DoNothing;
	}
	else if (optionforactivity == 'b') {
		return OcassionalyMoveAround;
	}
	else if (optionforactivity == 'c') {
		return AfewActivities;
	}
	else if (optionforactivity == 'd') {
		return VeryActive;
	}
	//return DoNothing;
}



std::string Fitness::MeasurementYouWant() {
	std::cout << "\t\t\t\tWhat measurement would you like?\n";
	std::cout << "[a]: Pounds(lbs)\n";
	std::cout << "[b]: Kilograms(kg)\n";
	char measurementyouwant;
	std::cin >> measurementyouwant;
	if (measurementyouwant == 'a' || measurementyouwant == 'A') {
		return "kg";
	}
	else if (measurementyouwant == 'b' || measurementyouwant == 'B') {
		return "lbs";
	}
}

void Fitness::DisplayInfo() {
	std::cout << "\t\tCalories\n";
	std::cout << "Calories required for maintanence: " << kcal << "\n";
	std::cout << "Calories from carbs: " << kcalcarbs << "\n";
	std::cout << "Calories from fat: " << kcalfat << "\n";
	std::cout << "Calories from protein: " << kcalprotein << "\n";
	std::cout << "\n\nMacros\n";
	std::cout << "Carbs: " << carbs << "g\n";
	std::cout << "Fat: " << fat << "g\n";
	std::cout << "Protien: " << protien << "g\n";


}




------------------------------------------------------------fitness.h below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once
#include<iostream>
#include<fstream>


class Fitness {


private:
	enum ChangeMass
		{
			Slow=1,Normal,Fast,ReallyFast
		};
	enum ActivityLevels
		{
		DoNothing = 14, OcassionalyMoveAround,AfewActivities,VeryActive
		};
	enum ChangeWeight {
		LooseWeight = 1, MaintainWeight,GainWeight
		};
	float CalculateMacroRatio(float kcal, float carbperc, float fatperc, float protienperc);
	float DetermaineHowManyKcalRequired(float bodyweight, ActivityLevels activitylevel, std::string unit);//Calculated how many kcal you need(for either kg or lbs)
	void DisplayInfo();
	float ChangeBodyWeight(ChangeWeight Option, ChangeMass Speed);
	std::string MeasurementYouWant();
	ActivityLevels ConvertActivityLevelToOptions(char optionforactivity);
	std::string MeasureMentUnitConverter(std::string Measurement);
	float StartPromt_1();
	void StartPromt1();
	float* StoreMacroPercentages();
	const float _slow{ 200.0f }, _normal{ 300.0f }, _fast{ 350.0f }, _reallyfast{ 500.0f };
	float kcal{}, carbs{}, fat{}, protien{};
	std::string User{};
	float kcalcarbs{};
	float kcalfat{};
	float kcalprotein{};
	float BodyWeightInKG{};
	float deficitkcal{};
	float surpluskcal{};
	char UserOption{};
	bool DetermineIfUserWantsVitamins(char usropt);
	void ShowVitaminInfo();
	bool DetermineUserOptionLogicPrint(char ursoption);
	//float PrintImportantInfo(float Bodyweight);
public:
	void Start();
	
};
Last edited on
1) most people avoid float in favor of double unless you need to use float.
2) magic numbers (random constant values in the middle of code) are not friendly, make them a named constant. eg 4, 9 in line 17/18
3) why do all that work to return 1.0 in the first function? I get that the function sets member variables, but what is the result doing -- unclear. This maybe should be a void function.

4) line 39... loose is a prostitute. lose is what you want.

128) consider using a vector. dynamic memory is best avoided unless there is no other way.

5) consider toupper or tolower vs multi-conditions on strings like "lbs" etc. what if it had lBs? LbS?



1) I'll keep that in mind thank you :)
2) I feel like an idiot your right hahaha
3)Ough, again I think I may have been drunk will change that
4)........................ Yeah, English isn't my best language :D
5)Man you god of programming. But for real your 100% right. Thank you for the advice. Hopefully one day I'll be as good as you and be able to think like a programmer.
With the current code I really don't see the reason for the class since the class doesn't seem to contain any member variables.

Remember in C++ you don't need a class to hold everything.

Also it might be better if you try to keep the User Interface separate from the program logic. Again not everything must be in a class.

Edit: You also have several functions that have paths that fail to return a value. If you define a function to return a value it should always return a value.
Last edited on
Topic archived. No new replies allowed.