How can I convert this function into a float?

#include <iostream>
using namespace std;

int main()

int testing (int c, float d)
{
//if d is greater than c, return the integer part of d, else return c

if (d > c)
return (int) d;
else
return c;
}



The function returns the decimal value resulting from dividing d by c if d > c and returns the decinal resulting from dividing c by d if c is greater or equal to d
1
2
3
4
5
6
7
8
float testing (int c, float d)
{
//if d is greater than c, return the integer part of d, else return c
if (d > c)
return (decimalPartOf d/c);
else
return (decimalPartOf c/d);
}
Last edited on
I am trying to compile this but its returning an error on line 6 (line 2 on your reply) for {

#include <iostream>
using namespace std;
int main ()
{
float testing (int c, float d)
{
//if d is greater than c, return the integer part of d, else return c
if (d > c)
return (d%c);
else
return c;
}
system ("PAUSE");
return 0;
}
You appear to be trying to define your function testing inside another function (main). You cannot define functions inside other functions.
Your code doesn't match your comment.

ny24gray wrote:
//if d is greater than c, return the integer part of d, else return c


return(d%c) doesn't reflect this.
Last edited on
How can I declare decimalPartof?

#include <iostream>
using namespace std;
float testing (int c, float d)
{
//if d is greater than c, return the integer part of d, else return c
if (d > c)
return (decimalPartOf d/c);
else
return (decimalPartOf c/d);
}
system ("PAUSE")
return 0;
}
How can I declare decimalPartof?


What's meant there is for you to write some code. Your original questions was how to make the function return a float. Has your question now expanded into how to get the decimal part of a number?
My apologies, as I was just jumping around. What I need to do is change the function testing on the program below so that the function returns the decimal value resulting from dividing d by c if d > c and returns the decimal resulting from dividing c by d if c is greater or equal to d


#include <iostream>
using namespace std;

int testing (int c, float d)
{
//if d is greater than c, return the integer part of d, else return c

if (d > c)
return (int) d;
else
return c;
}
Then returning the decimal value would be as simply as returning d divided by c or c divided by d where necessary. One thing you will need to ensure is that neither of them is an integer; a decimal divided by an integer will result in an integer.

Presumably that's what you're after. If you're looking to return solely either the integer or fractional part of the result then that's a little different.
Cast the return to a float.

return (float)d / c;
Ok so I did that, I put the entire program together and it's returning an error on the if (d>c)

#include <iostream>
using namespace std;
int main ()
{
float testing (int c, float d);
int x;
float y;
cout << "Enter an integer and a decimal number"<< endl;
cin >> x >> y;
cout << testing (x, y);
{
//if d is greater than c, return the float part of d, else return c
if (d>c)
return (float) d/c;
else
return (float)c/d;
}
system ("PAUSE");
return 0;
}
You don't seem to have written a function named testing.

float testing (int c, float d); This is the prototype of the function named testing.
cout << testing (x, y); This is an attempt to call the function named testing.
1
2
3
4
5
6
7
{
//if d is greater than c, return the float part of d, else return c
if (d>c)
return (float) d/c;
else
return (float)c/d;
}
This is a chunk of code that is part of the function named main, in which you try to use the variables d and c which do not exist in the function named main.
Is this moving in the right direction now?


#include <iostream>
using namespace std;
int main ()
{
float testing (float c, float d);
float c;
float d;
cout << "Enter an integer and a decimal number"<< endl;
cin >> c >> d;
cout << testing (c, d);
{
//if d is greater than c, return the decimalpartof d, else return c
if (d>c)
return (float)d/c;
else
return (float)c/d;
}
system ("PAUSE");
return 0;
}
Is this moving in the right direction now?


No. Are you, or are you not, trying to write a function named testing?

If you are, then you cannot put it in the middle of another function (main is another function).
Now back to square one. Receiving a warning that I am converting an int to a float. How can I correct this?


#include <iostream>
using namespace std;
float testing (float c, float d);
//functionality of testing
int main()
{
float c;
float d;
cout << "Enter an integer and a decimal number"<< endl;
cin >> c >> d;
cout << testing (c, d);
return 0;

//if d is greater than c, return the decimalpartof d, else return cfloat testing
if (d>c)
return (float)d/c;
else
return (float)c/d;
system ("PAUSE");
return 0;
}
You're still mixing the definition of main with that of your function.
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
#include <iostream>
using namespace std;

float testing (float c, float d);      // Function declaration

//--------------------------------------------------------------

int main()                             // Start of main()
{
    float c;
    float d;
    cout << "Enter an integer and a decimal number"<< endl;
    cin >> c >> d;
    cout << testing (c, d) << endl;

    system("pause");
    return 0;
}                                      // End of main()

//--------------------------------------------------------------

float testing (float c, float d)       // Function definition
{
    if (d>c)
        return (float)d/c;
    else
        return (float)c/d;
}
Thank you !!!
Topic archived. No new replies allowed.