Hey!
I've recently started trying my hand at C++. This is my first application based on temperature conversion (original I know /s).
It would be really helpful if some experienced C++ dev could have a look at the source and comment on it's readability and cleanliness. Maybe suggest a more efficient way of doing things etc.
We've all gotten into bad coding practice at some point, and its easier to fix before it becomes a habit. :P
Also had another question: (couldn't find a definite answer on google)
- Is calling a function from another function bad practice? What if they're both in different classes?
Thanks.
The fully compiled executable is attached to this post.
Source:
Code:
// TestApp.cpp : Your road to temperature conversion!
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class temperature {
public:
double getFahr(double cel) {
double fahr = 1.8 * cel + 32;
return fahr;
}
double getCel(double fahr) {
double cel = fahr - 32;
cel = cel / 1.8;
return cel;
}
};
class interact {
private:
bool celcius; // If celcius is 0 then its fahr -> cel, if its 1 then its cel -> fahr conversion.
public:
bool verifyChoice(short choice) {
if (choice == 1) {
celcius = 0;
return celcius;
}
else if (choice == 2) {
celcius = 1;
return celcius;
}
else {
cout << "Error: Pick a valid choice. (1, 2)" << endl;
exit(EXIT_FAILURE);
}
}
};
int main()
{
temperature temp;
interact inter;
short choice;
cout << "Please choose!" << endl << "(1) Convert Degrees Celcius to Degrees Fahrenheit." << endl << "(2) Convert Degrees Fahrenheit to Degrees Celcius." << endl << endl << "Enter your choice below!" << endl;
cin >> choice;
if (inter.verifyChoice(choice) == 1) {
cout << "Initiating conversion from Degrees Fahrenheit to Degrees Celcius..." << endl << endl << "Please enter the value you wish to convert :-" << endl;
double value;
cin >> value;
cout << "The conversion was successful! " << value << " degrees fahrenheit is equal to " << temp.getCel(value) << " degrees celcius.";
}
else {
cout << "Initiating conversion from Degrees Celcius to Degrees Fahrenheit..." << endl << endl << "Please enter the value you wish to convert :-" << endl;
double value;
cin >> value;
cout << "The conversion was successful! " << value << " degrees celcius is equal to " << temp.getFahr(value) << " degrees fahrenheit.";
}
cout << endl << endl << "Thank you!";
char stay; // couldn't figure out how to keep .exe window open, so.. :P
cin >> stay;
}
PS: Yes I understand it's bad practice to put classes and main function together, merged them for the sake of this thread.