Hey guys, I'm new to C++, I'm reading the C++ primer book, sixth edition, and in chapter 2, listing 2.6, there is a sample code to convert pounds to stones. Essentially it just takes the input from the user in stones and multiply that by 14 which returns the total number of pounds.
I wanted to modify this and return the number of stones from the number of pounds input by the user. However I ran into a snag when the return for most reverse conversion ended up in floating point numbers. So I changed the instantiated prototype of the function that does divides the input by 14 to a floating type function, as well as the function itself on the body.
I have searched the web to see if there are a ways to return floating point numbers however I feel that I do not understand C++ well enough to see how they work exactly, perhaps someone here can show how to return floating point numbers from the second function in my case and I may understand from there (hopefully).
Here is code below. I prefer doing everything on github so all of the corrections, are in one area if you don't mind, otherwise, I'll keep a watch on my e-mail.
Thanks in advanced. Again I'm completely new to C++ and most programming languages, so please be detailed as possible as it will help me out :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
The original code from the book.
// convert.cpp -- converts stone to pounds
#include <iostream>
int stonetolb(int); // function prototype
int main()
{
using namespace std;
int stone;
cout << "Enter the weight in stone: ";
cin >> stone;
int pounds = stonetolb(stone);
cout << stone << " stone = ";
cout << pounds << " pounds." << endl;
return 0;
}
int stonetolb(int sts)
{
return 14 * sts;
}
Modified code to return stones to pounds:
https://github.com/slimg00dy/LearningC/blob/master/ch2-StoneToPounds.cpp
|