Thread: += Operator

  1. #16
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    I know how to do rationals, my problem is with the actually code. I've been messing around with everyone's suggestions and ideas. Still no luck.

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Besides the problems you are having with the mechanics of compiling and running programs, you need to review some basics on how to declare, define, and call functions. Maybe the following will help.

    Here is an example of a very simple function:
    Code:
    void display(int n)
    {
    	cout<<n<<endl;
    }
    All functions require a return type. In the display() function above, the return type is 'void', which stands for 'nada', 'nothing', 'zip'. That means, the function doesn't return anything. Functions also can have things called 'parameters'. The parameter in the function above is 'int n'. That means the function is expecting any integer to be sent to it. The integer value that is sent to the function will be stored in the variable 'n'.

    Here is what the function looks like as part of a program:

    Code:
    #include <iostream> //contains cout
    
    using namespace std; 
    //makes it so you don't have to write 'std::cout'
    //instead you can just write 'cout'
    
    
    //function:
    void display(int n)
    {
    	cout<<n<<endl;
    }
    
    
    int main()
    {
    	int num = 10;
    	display(num);
    
    	return 0;
    }
    In main(), the function is called by this line:

    display(num);

    That instructs the program to send the value contained in the variable 'num' to the function named display(), and then execute the code in the function. Since num is equal to 10, 10 is sent to the display() function. The value 10 is then stored in the variable 'n', which is the function parameter. Then, the code in the function is executed. After the last line in the function is executed, execution returns to the place in main() where the function was called, and then the statements thereafter in main() are executed.

    Here is another example of a function:
    Code:
    double multiplyBy3(int x)
    {
    	double temp = x * 3.0;
    	return temp;
    }
    This time the return type is double(a double is a number with a decimal point like 2.5). The return type has to be specified as 'double' because the function returns a double value in this line:

    return temp;

    The multiplyBy3() function also has an int parameter, so it must be sent an int.

    Adding the multiplyBy3() function to the previous program looks like this:

    Code:
    #include <iostream> //contains cout
    
    using namespace std; 
    //makes it so you don't have to write 'std::cout'
    //instead you can just write 'cout'
    
    double multiplyBy3(int x)
    {
    	double temp = x * 3.0;
    	return temp;
    }
    
    void display(int n)
    {
    	cout<<n<<endl;
    }
    
    
    int main()
    {
    	int num = 10;
    	display(num);
    
    	int number = 5;
    	double result = multiplyBy3(number);
    	
    	cout<<result<<endl;
    
    	return 0;
    }
    The function multiplyBy3() is called in main() by this line:

    double result = multiplyBy3(number);

    The function is sent the int value stored in number. Since number is equal to 5, 5 is sent to the multiplyBy3 function, and then 5 is stored in the variable 'x', which is the function parameter. Then, the code in the function is executed. When the return statment is executed:

    return temp;

    the function gets the value in temp, and sends it back to main(). Since temp equals 15.0, 15.0 is sent back to main(). When a function returns a value, the function call is replaced by the return value. So, this line in main():

    double result = multiplyBy3(number);

    becomes:

    double result = 15.0;

    Then, the line:

    cout<<result<<endl;

    displays the return value. Note that you cannot do this to display 'result':

    display(result);

    The display() function requires an int to be sent to it, not a double, and result is type double. Therefore, you would get an error if you tried that: the types have to match.

  3. #18
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Quote Originally Posted by 7stud
    Well, I think that means one of two things:

    1) Your compiler is junk.
    2) Your copy and paste technique needs work.
    My copy paste technique is fine thank you very much... I'm using g++... I've been messing around with that program and all it ever does it output 0. It's like it never gets to int main()

    because it doesn't output:
    0
    0

    it outputs just
    0

    Also with the *= I'm trying to multiply a numerator that is part of the rational class by a normal intger for example 8
    So what I'm trying to do is this:
    third(rational class) *= 8;
    Last edited by StarOrbs; 05-03-2005 at 09:52 AM.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Perhaps you're calling some old program?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    I fixed my problem, thanks everyone for your patience and help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 01:23 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM