New to C++

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 
Last edited on
slimg00dy on GitHub wrote:
15
16
17
18
 float lbtostone(int pds)
 {
  return pds / 14;
 }
The issue is that you need to divide by 14.0f instead of 14, otherwise you will have integer division followed by conversion to float.

By the way, unless you have a need to use float, use the double type instead (and 14.0 instead of 14.0f).
Last edited on
Hello LB,

Thank you for that detailed output. I've made the changes to double, when instantiating and declaring the function, I've also made the change to 14.0.

I understand that double is supposed to give me double precision, in laymen's terms, more numbers after the fractional point (eg. 1.589548954893282).

After making the changes and compiling the source, I still get the same output.

Also, how did you do that "Code on Github wrote:"

Thanks again in advanced.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Convert.cpp -- converts stones to pounds
#include <iostream>
double lbtostone(int); // function prototype instantiated
int main()
{
  int pounds;
  std::cout << "Enter the weight in pounds: ";
  std::cin >> pounds;
  int stone = lbtostone(pounds);
  std::cout << pounds << " pounds = ";
  std::cout << stone << " stone." << std::endl;
  return 0;
}

double lbtostone(int pds)
{
 return pds / 14.0;
}


1
2
3
./ch2-StoneToPounds
Enter the weight in pounds: 156
156 pounds = 11 stone.
Last edited on
On line 9 you take the double precision float and store it into an integer, truncating the decimal places.

As for the quote, it is like on most forums: [quote=name of person]text[/quote]
Hello again LB,

Thank you for catching that, you're right, I was passing stone as an "int" rather than a "double"

I have another question, do I need to instantiate my lbtostone function as "double"
slimg00dy wrote:
I have another question, do I need to instantiate my lbtostone function as "double"
I don't understand your question; functions cannot be instantiated.
Hi,

I was referring to the function prototype. It does look like the "double" is required in the prototype.
Topic archived. No new replies allowed.