-
Problem with overloading
I am a newbie to C++ and am working on a simple program with an overloaded function GetNumber. If GetNumber is sent a bool argument it returns 0 or 1, if GetNumber is sent a integer argument it multiplies that number by itself then returns the product. Now here is my code that doesn't work:
Code:
#include <iostream>
#include <conio.h>
//Simple Overloading program
int GetNumber(bool testbool);
int GetNumber(int num);
using namespace std;
int main()
{
bool input= false;
int output=0, number=0, product=0;
// prompt for user input and store in input and number
cout << "Enter true or false...";
cin >> input;
cout << "Enter an integer...";
cin >> number;
// call the overloaded function using their seperate parameters
output = GetNumber(input);
product = GetNumber(number);
// output the solutions
cout << " Your true and false answer converted to..." << output
<< endl << "Your integer was squared and the product is..." << number;
getch();
return 0;
}
int GetNumber(bool testbool)
{
int convrt=0;
if (testbool) // tests for a nonzero answer
convrt = 1;
else
convrt = 0;
return convrt;
}
int GetNumber(int num)
{
return num * num;
}
Now it compiles with no errors and no warnings, but when ran the program doesn't save any of the user inputs into the variables input and number. If you can tell me what I am doing wrong i would appreciate it greatly!
-
When prompted to enter true or false are you literally typing true or false ? or 0 and 1. Because if you are typing true for instance, that will cause problems because you're inputting a string to a boolean value. Try 0 and 1 and it should work ok. Also you initialize your bool with false which is fine but in your boolean overloaded function you set it with 1 and 0. You should stick with true and false keywords.
-
found the problem
Code:
// output the solutions
cout << " Your true and false answer converted to..." << output
<< endl << "Your integer was squared and the product is..." << number;
change to
Code:
// output the solutions
cout << " Your true and false answer converted to..." << output
<< endl << "Your integer was squared and the product is..." << product;
just make sure u output the right variable
-
>the program doesn't save any of the user inputs into the variables input and number.
Don't type "true" or "false" unless you use the boolalpha manipulator, 1 and 0 should be used if you don't:
Code:
// prompt for user input and store in input and number
cout << "Enter true or false...";
cin >> boolalpha >> input;
cout << "Enter an integer...";
cin >> number;
>// output the solutions
>cout << " Your true and false answer converted to..." << output
> << endl << "Your integer was squared and the product is..." << number;
I think you want to print product instead of number. :)
Code:
int GetNumber(bool testbool)
{
int convrt=0;
if (testbool) // tests for a nonzero answer
convrt = 1;
else
convrt = 0;
return convrt;
}
This is overly complex. Why not just return the result of the boolean expression?
Code:
int GetNumber(bool testbool)
{
return testbool != false;
}
Or, since bool types are restricted to 0 and 1 anyway, you could simplify it even more like this:
Code:
int GetNumber(bool testbool)
{
return testbool;
}
Which begs the question, why a function anyway? ;)
-
I want to thank you all for the help!
In response to Prelude:
" ...Which begs the question, why a function anyway?..."
Only because it was one of the problems in the Learn C++ book I am reading and that was one of the specific guidlines it gave me.