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.
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.
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.
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");
}