Completely Stuck

Pages: 123
I have to create a program that accepts a mixed number from the user (6 digits past the decimal min) in the main () function, and passes the mixed number to two functions, WholePart() and FractPart() respectively. The function wholePart() returns the integer part of the mixed number that was past to int. The function FracPart() returns the fractional part of the mixed number that was passed to in by main(). the fractional part should be rounded to four decimal places and the rounding should be performed by the function Rounder().

The only hint he gave me was

Step 1: Multiply by 10^n
Step 2: Add .05
Step 3: Delete the fractional part of the result
Step 4: Divide by 10

This is what i got so far. I don't really understand the functions very well. Could anyone help me out a bit? I am also completely stuck. Any hints or tips to give me a nudge in the right direction?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> //used for cout and cin statements.
#include <string> //used for potential strings
#include <iomanip> //used for potential manipulation of data outcomes
#include <fstream> //needed for saving data file

using namespace std;

int FracPart(int n); //function prototype

int main()
{
	float n;
cout<< "Please enter a mixed number with six or more decimal places. \n ";
cin>> n;
cout<<n<<" is the decimal number you have entered. \n";
cout<<FracPart(n);
}
int FracPart(int n)
{
	return (n*(10*10*10*10*10*10))+(0.5);

}


This should give me the number fully rounded, And adds the .05. However it does not work. I think the problem is in the int FracPart. Any ideas?
not going return float when you define it to return int

int FracPart(int n)
shouldbe
float FracPart(int n)
If you put the input from the user into a string (or character array) and then convert that into an int or float in your functions, it'll probably be a lot simpler.

DrakeMagi, if s/he were to pass an int into the function, it would lose all decimal points anyways.

try float FractPart(float n)
Last edited on
@ oghmaosiris The problem is that i am required to do it this way :/

@ Drakemagi Thanks :] Fiddled with that for awhile kept giving me errors finally got that to work. Now on to getting it to round instead of giving me a huge number^e
i think you want this.
int FracPart(float n)
@drake

Thanks that worked like a charm

I am a little confused on step 3. What fractional part am i suppose to get rid of? There is not fractional part left.

Also the FractPart() should be rounded to 4 decimal places which does not make since. Since multipling it by 10^n will get rid of them. Am i mis understanding the problem?
Last edited on
now since i read what you want down.

you need a whole part , fartpart , input variable.

send input variable to function( wholepart , fracpart )
Last edited on
Do i need to Create a function rounder or is there a command i should look up?
you need 3 function WholePart() , FracPart() , Rounder().
wholepart return wholenumber
fracpart return fracnumber
rounder rounds fracpart

according to what you wrote.
Yup that is correct. So i need to also make a rounder function. What each function is suppose to do is confusing me.

lets say i enter 1.123456 the code returns
1123456.
So there is not Frac part left.
The rounder is obviously there to round
I don't understand what the Frac or the Whole part should do.
hint to get whole part use int variable.

your fracpart is a rounder not a fracpart
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream> //used for cout and cin statements.
#include <string> //used for potential strings
#include <iomanip> //used for potential manipulation of data outcomes
#include <fstream> //needed for saving data file

using namespace std;

int FracPart(float n); //function prototype

int main()
{
float n;
cout<< "Please enter a mixed number with six or more decimal places. \n ";
cin>> n;
cout<<n<<" is the decimal number you have entered. \n";
cout<<FracPart(n);
}
int FracPart(float n)
{
	int x;
	x=(n*(10*10*10*10*10*10))+(0.5);

return x;
} 

int WholePart(int x)
{


}
}


This is what i have done. I got the int part I just don't understand what the wholepart is suppose to do. So i have no idea what to start working on inside that function
You can just pass through the initial float and then return x.

it will convert the float to an int and forget the rest of the decimal.
So
1
2
3
4
5
int x;
	x=(n*(10*10*10*10*10*10))+(0.5);

return x;

Should be in the WholePart()?
WholePart return value
No,

1
2
3
4
int WholePart(int x)
{
     return x;
}


when you do that, the float automatically gets turned to an int and the decimals are forgotten.
do the same with FracPart but - WholePart. as oghmaosiris suggested but with floats
then we get to the rounder
Last edited on
Ok working on what you guys said. However, i have a question
So WholePart should just be return x? That is all that goes in there?
1
2
3
4
5
6
7
int WholePart(int x)
{
cout<< "\n";
return x;
cout<< "\n";

}


Can't get this to enter the new line.
The answer always comes out
(answer)please press any button to close
I want it to be
(answer)
Please press any button to close
Last edited on
yes that all , according to what you wrote above in your first post.
Last edited on
Ok well if that is all when you run the program it only returns x once. Shouldent it be two?
One from FractPart and WholePart?
Pages: 123