function call trouble

I'm prepping for class. Brand new to this. Trying to do a basic fucntion decleration, function call, and function definition. program is suppose to enter a positive or negative integer than return either a 'P' or 'N'. I can't figure out how to do the function call correctly so it doesn't return the character. Can anyone help?


#include <iostream>
using namespace std;
//Program to receive integer and display P for positive or N for negative.
//function call practice.

char answer (double number);
int main()
{
int choice;
double number;

cout << "enter a positive or negative number.\n";
cin >> choice;

choice = answer (number);

return 0;
}

char answer (double number)
{
if (number > 0)
return 'P';
else
return 'N';

}
> choice = answer (number);
Try cin >> number instead then.
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;
//Program to receive integer and display P for positive or N for negative.
//function call practice.

char answer (double number);
int main()
{
    char choice; // <--
    double number;
    
    cout << "enter a positive or negative number: ";
    cin >> number; // <--
    
    choice = answer (number);
    
    cout << "The answer is: " << choice << '\n'; // <--
    
    return 0;
}

char answer (double number)
{
    if (number > 0)
        return 'P';
    else
        return 'N';
}
thanks againtry, worked great except using
if (number>0) in the definition would only return N every time. making it if (answer>0) only returns a P value each time. any idea what is wrong with the if statement?

I don't get what you mean.

Have you run my version or your own? I am reasonably certain my version returns P if the number is positive, and N if it is negative.

Perhaps post your version?
Ahh my mistake, I was missing a line. I didn't copy and paste yours completely but rather added to mine what I thought I was missing from yours. And I missed a line. copied yours and That took care of it! Thank you!! works well! I didn't realize how big of a deal that cout<<choice<< would be, did the trick. much to learn on my part..

thank you!
Well done for finding it through your own efforts and not just copying.
Cheers :)
Topic archived. No new replies allowed.