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?
#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
usingnamespace 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?
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.
@ 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 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?
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.
#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
usingnamespace 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
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