Bashing away in C++

When learning code, pundits say one of the best things you can do is modify existing code to make it do something you want it to do.

For the last few day’s I’ve been going through the The Complete Idiots Complete Guide to a Career in Computer Programming (Jessie Liberty 1999). On page 124-125 it talks about using functions and local variables and includes the code to convert a user entered Farenheit temperature into a Celsius temperature which is then output to screen.

Here’s the original code.

// demonstrates using functions and local variables

#include <iostream>

float Convert(float);
int main()
{
float TempFer;
float TempCel;

std::cout << “Please enter the temperature in Fahrenheit: “;
std::cin >> TempFer;
TempCel = Convert(TempFer);
std::cout << “\nHere’s the temperature in Celsius: “;
std::cout << TempCel << std::endl;
return 0;

}
float Convert(float TempFer)
{
float TempCel;
TempCel = ((TempFer – 32) * 5) / 9;
return TempCel;
}

My cycle computer calculates my energy expenditure in Calories (no idea why it will not show this in kJ, despite being able to configure the system to display metric or imperial during set-up),  I thought I’d modify the program to take a Calorie value and output a kJ value.   As my training log automatically displays energy used in kJ, so the only practical aspect to my code modification is to learn to make software do what I want.  The modified code follows:

// demonstrates using functions and local variables

// modified on 15-05-2104 by Paul Yeatman to convert Calories to kilojoules
#include <iostream>

float Convert(float);
int main()
{
float TempCal;
float TempKjs;

std::cout << “Please enter the energy used in calories (Cal): “;
std::cin >> TempCal;
TempKjs = Convert(TempCal);
std::cout << “\nHere’s the energy used in kilojoules (kJ): “;
std::cout << TempKjs << std::endl;
return 0;

}
float Convert(float TempCal)
{
float TempKjs;
TempKjs = TempCal * 4.1858;
return TempKjs;
}

Of course, once the code is written it needs to be compiled and output to a new file and then run.  To do this, I’m telnetting into a headless server I’ve set up running ubuntu, coding in the nano text editor (it has colour coded hints, but no line numbering) and then compiling and running things using the command line.  I’ve set up my main computer with a compiler as well, but that’s for if I want to port anything into windows.  The beauty of C++ is I only have to write the code once and then use an OS specific compiler to port it to a new system.

*Update 20170309*

Looking at the code, the output’s actually in Joules!  To produce KJ, I needed to add a /1000 to TempKjs = TempCal * 4.1858; making it TempKjs = (TempCal * 4.1858)/1000;

 

*Update 20170728*

While preparing for Stanford’s CS106B as part of my Poor Humans Bachelor of Computer Science, I installed Qt-Creator, a modern IDE.  That’s much better than the round about what I was doing it with my server.  The server might still be good for compiling (I’m yet to check out many of the IDE’s features).