Problem with overloading

This is a discussion on Problem with overloading within the C++ Programming forums, part of the General Programming Boards category; I am a newbie to C++ and am working on a simple program with an overloaded function GetNumber. If GetNumber ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    2

    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!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User electRONix's Avatar
    Join Date
    Jun 2004
    Posts
    13
    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
    my spider-sense be tingling.

    Ron - SCU Math/CS Major

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    2
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator overloading slight problem
    By reddzer in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2006, 04:30 PM
  2. oops? not sure. operator overloading (possible) problem
    By w00tw00tkab00t in forum C++ Programming
    Replies: 8
    Last Post: 02-08-2006, 05:38 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 02:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21