Write a C++ function named fracpart() that returns the fractional part of
any number passed to it. For example, if the number 256.879 is passed to fracpart(), the
number 0.879 should be returned. Have fracpart() call the whole() function you wrote in
Exercise 12. The number returned can then be determined as the number passed to
fracpart() less the returned value when the same argument is passed to whole(). The
completed program should consist of main() followed by fracpart() followed by whole().

#include <iostream>
#include <cmath>
using namespace std;
int whole(double);
double fracpart(double);
int main()
{

    int whole(double x);
        double x=0;
        int y;
        //cout << " Enter double precision number :\n";
        //cin >> x;
        y = x;
        double z = (x - y);
        cout <<"returned value"<< y << endl;
        return y;

    double fracpart(double num);
    {
        double x;
        float fraction;
        cout << " Enter double precision number :\n";
        cin >> x;
        fraction = whole(x) - (long)x;
        cout <<" Answer is  :"<< fraction <<endl;
        return fraction;
    }

    char aaaa;
    cin >> aaaa;
    return 0;

I don't really see a question in there any where. If I was to venture a guess though, I would say one of your problems is in this line:

fraction = whole(x) - (long)x;

x should stay as a double and you should subtract the whole part from it:

fraction = x - whole(x);

Another obvious thing I see, is your functions should be outside main not inside. Whole doesn't even have curly braces({}) to define it.

Edited 2 Years Ago by tinstaafl

I can't seem to get how to setup these funcions and then call them and integrate them into a main()....
Can you help me understand what is wrong with this..
Thank you very much

#include <iostream>
#include <cmath>
using namespace std;

int whole(double);
double fracpart(double);
int main()
{

    double x;
    cout << " Enter double precision number :\n";
    cin >> x;
    cout << " Fractional part is :" << fracpart(x) << endl;

    char aaaa;
    cin >> aaaa;
    return 0;
}
double fracpart(double x);
{
    float result;
    result = x - whole(x);
    return result;
}

int whole(double x);
{
    int y;
    y = x;
    return y;
}

Your code looks ok except that you've put semi-colons(;) in the declaration line(int whole(double x)). They're needed for the prototype but not in the actual function. Remove those and it should compile and work.

This question has already been answered. Start a new discussion instead.