vendredi 27 février 2015

What's wrong with this code, C++


Just leaning C++, here's the code.



#include <iostream>
using namespace std;
int absouteValue(int number);

int main()

{
cout << absouteValue(-6);

return 0;
}

int absouteValue(int number)

{

cout << "Enter a number" << endl;
cin >> number;


if (number < 0)
{
number = number * -1;

}

else
{
number = number;
}


return number;
}

I want the absoluteValue function to return the absolute value of a number that is inputted from the user. It is doing this somewhat but I am bumping in to the following problem:


In my main function, I don't want to have to input a value when I call the absoluteValue. I would like the user to do that. But I have to pass through a value into the parameter number. Even if I pass -6, and I input -31, the answer that the program returns is 31, which is correct. Is there a way to just call the function from main without having to pass through my own number?


Thanks for your help!




Aucun commentaire:

Enregistrer un commentaire