OK, this code works as it is, but I need to modify it so that it uses a function to break the users number into two pieces. Can someone help me break the users number up into two pieces using a function?
#include <iostream>
using namespace std;
int main() {
float a =0.0;
int lh=0, rh=0;
cout << "Enter a decimal number between 10.0 and 99.9: ";
cin >> a;
lh=int(a);
rh=int(a*10)%10;
cout << "You entered "<< lh << " " << "and " << rh << " tenth(s)" << endl;
system("pause");
return 0;
}
Let me be frank. No-one is going to do your homework (even if this is not homework, no-one is going to do it for you) for you (especially with that rude attitude). I asked what you needed help with specifically. Can you be more specific?
If you state what, specifically, you are having trouble with, then I will help.
I said it, I needed help creating a function (from this program that I wrote) that splits the users number into two parts. It works likes this, but I need to create a function. And the attitude was in response to you trying to troll me.
chrisname does come off as condescending, but he's right in that your original post was not specific enough. I believe what you're trying to do is take the user input and split it into the part to the left of the decimal point and the part to the right of the dec. point.
For example:
input = 30.2
lhs = 30
rhs = 2
If that's correct, you're very close to done! You just need to reorganize your code into functions. This is a good skill to learn, so I'll give you an example that's similar to yours but not quite the same. Let's say we wanted to split the user input into two parts in a more general way. You would want main to look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
int main() {
float a =0.0;
int lh=0, rh=0;
cout << "Enter a decimal number between 10.0 and 99.9: ";
cin >> a;
lh = calcLeftSide(a);
rh = calcRightSide(a);
cout << "You entered "<< lh << " " << "and " << rh << " tenth(s)" << endl;
system("pause");
return 0;
}
You can see "calcLeftSide" is a function that takes a float and returns an int. "calcRightSide" is also a function that takes a float and returns an int. chrisname wrote "calcLeftSide" for you above, though he called it "float_to_int" (the first one, the one with return type "int"). I think you can write "calcRightSide" yourself now.
Here is what I have, but I cannot get the right side of the decimal to show up. If I put 10.50 is prints like this: 10 and 0 tenths. Could ya'll help me correct it so it will read properly?
int a; //a is an integer
float (a=0.0); //statement has no effect (well a is set to zero but the float conversion does nothing)
What is the purpose of your program? If you are going to work ints and tenths with independency, you better read them directly cin >> lh; cin.ignore(1); cin >> rh (because of finite precision)