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
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);
elsereturn (decimalPartOf c/d);
}
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;
}
#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;
}
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
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.
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;
elsereturn (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.
#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;
}
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;
}