#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;
cout<< "\n";
}
int WholePart(int x)
{
cout<< "\n";
return x;
cout<< "\n";
}
Which ones, the couts after the returns?
A function's execution terminates at the return, going back to (aka returning to) the calling function. Those lines are never reached.
On your problem with the missing \n, once a function hits a return, the function exits. You would need to code another \n in main.
and idealy, your functions should cout anything unless that's what they were designed to do.
Since your WholePart function was designed to just give you the int value of a float, it shouldn't cout anything at all. Do all your output in main (or another function that was designed solely for output ;P)
float n , f;
cout<< "Please enter a mixed number with six or more decimal places. \n ";
cin >> n;
cout << "you enter : " << n << endl << endl; // endl same things as "\n"
cout << WholePart(n) << " whole part of decimal." << endl << endl;
#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 WholePart(int x);
int x;
int main()
{
float n;
int x;
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);
cout<<"\n";
cout<<WholePart(x);
}
int FracPart(float n)
{
int x;
x=(n*(10*10*10*10*10*10))+(0.5);
return x;
}
int WholePart(int x)
{
return x;
}
Ok so this is everything i did. Hopefully did it correctly. Running it works fine until everything is done then says it tried to use x without being initialized
The x in the function is a totally different entity from the one in main. You aren't assigning the return of FracPart to the x in main, so what are you doing with it?
Ok took the int x; out in main. That got rid of the error/crash.
However not I am getting an extra 0 on its own line.
Is this because of WholePart returning x with it not being defined there?
Edit: After tweeking it i see that WholePart is sending the 0 back. Shouldent it be sending the value back instead?
#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 WholePart(int x);
int x;
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);
cout<<"\n";
cout<<WholePart(x);
cout<<"]n";
}
int FracPart(float n)
{
int x;
x=(n*(10*10*10*10*10*10))+(0.5);
return x;
}
int WholePart(int x)
{
return x;
}
The WholePart seems pointless since it only returns 0 because nothing is there. Shouldent the fact part be the WholePart?
Get rid of the int x in FracPart; it's obscuring the global x on which you are calling WholePart.
Global variables are a messy solution. You could have just had
@tummychow Thanks :]
Now i have it returning a value. Except it is the exact same. So WholePart=FracPart. Why would i need them to be the same.
"The function FracPart returns the fractional part of the mixed number that was passed to it by main." - assignment
@oghmaosiris i use .123456 every time this way i can see what changes. It now returns 2x so it shows on screen
123456
123456press any...
I'm so confused by this solution. What's the output you would put in, and what would come out?
Judging from your original assignment you could (I know, type conversion) here's what I would do:
use modf (in cstdio iirc) to get the remainder of somefloat / 1 (the decimal part). Cast the float to an integer (float somenum = someint;) to get the integer part. Then you can do your rounding.