Need help! (HOMEWORK)

Write a program that defines a value returning function named halfIt() that receives an integer parameter. The function should return the number passed to it, divided by 2. Since the number being returned may contain a decimal, it should return a double.

main() function should:

1. prompt the user for the integer
2. call your halfIt() function to divide the number in half, storing the returned
result in a local variable defined in main
3. print the result out from main. i.e. the halfIt() function should have no cin’s or cout’s.

Please help anyone!
What have you written so far?

Have you read the tutorials on this site?
This is what I have so far. I just don't know what parameters to use for my function. I have been reading the book I was given from school and also the power point slides.

#include <iostream>
#include<cmath>

using namespace std;

int halfIt()

{




}
int main()
{


return ;
}
daernac wrote:
I just don't know what parameters to use for my function.
The assignment text outright tells you exactly what the parameter and return types should be:
daernac assignment wrote:
Write a program that defines a value returning function named halfIt() that receives an integer parameter. The function should return the number passed to it, divided by 2. Since the number being returned may contain a decimal, it should return a double.
Last edited on
I read a little bit on the site and I came up with this...it's still not what I want.


#include <iostream>
#include<cmath>

using namespace std;

double halfIt(int a, double b)

{
double divide;

divide = a / b;

return divide;

}
int main()
{
int c;
c = halfIt (14, 2.0);

cout << "The result is " << c;
system("pause")
}
Last edited on
How do I make it stop!! lol...
It looks like all you need to do now is actually prompt the user in main() for the integer. If you really want to conform to the specification (which I recommend doing), you'll want to remove that second parameter from halfIt() as well since the assignment doesn't want you to input as a parameter the divisor; it just wants you to always divide by 2.
Ok. I made some changes. I am a bit confused as to why the getline function worked in this one, but at least it prompts for me to type when I run it. however, the result is always the same. How can I change that? I want it to only always divide by 2 and that's it.

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

double halfIt(int a)

{
double divide;
string z;
divide = a / 2.0;

getline(cin, z);

return divide;

}
int main()
{
int c;
c = halfIt (14);

cout << "The result is: " << c;
system("pause");
}
Your halfIt function should not be doing anything related to user input. In fact, it should only be one line:

return a / 2.0;
Last edited on
Wow...thank you so much. I'll keep you updated!
Topic archived. No new replies allowed.