Functions

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;
}
Last edited on
Ok. You get right on that. Good luck :)
Last edited on
If you don't know how to do it say so. No need to be condescending.
Well, I tried scanning your post for a question mark and didn't see any, so I assumed it was a statement and not a question.

Are you asking for help, or..?
Its a question.
Questions have question marks. You could rephrase your post to make it a question.

What, specifically, do you want help with?
OK, I edited, and here is the question:
Can someone help me break the users number up into two pieces using a function?
Last edited on
Taboo wrote:
Can someone help me break this up into two pieces using a function?

Yes.

You can mark this as solved now.
Last edited on
Its not solved you dolt. So quit your trolling.
But I answered your question...

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.
See, that's still not specific. What part of this function are you having trouble with?

Your code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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;

	return 0;
}

already does this. So all you want is to put the line, rh=int(a*10)%10; into a function?

You declare a function like this:
1
2
3
4
5
<return type> <function name>(<list of arguments>)
{
	<code>;
	[return <value>];
}


for example

1
2
3
4
5
int float_to_int(float f)
{
	int i = int(f);
	return i;
}


You can modify that for your purpose:
1
2
3
4
5
void float_to_int(float f, int& lhs, int& rhs)
{
	lhs = int(f);
	rhs = /* You can figure this part out */
}
Actually creating one.
hey Taboo,

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?


#include <iostream>
using namespace std;

void calcLeftSide(float);
void calcRightSide(float);

int main() {

int a;
float (a=0.0);
int lh=0;
int 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;
}
That's because that code is exactly the same as your previous code, except with the lines
1
2
void calcLeftSide(float);
void calcRightSide(float);

added.

I'm guessing that is a copying error?
[code] Your code goes here [/code]
1
2
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)
Last edited on
I think he has to do it mathematically.
Topic archived. No new replies allowed.