need help 'by reference'

This is a discussion on need help 'by reference' within the C++ Programming forums, part of the General Programming Boards category; the first function works i can enter information, but when I call the second function it's supposed to display the ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    need help 'by reference'

    the first function works i can enter information, but when I call the second function it's supposed to display the information from the first function...do I 'by reference'? where do I put the & and even the other & or *. I am just learning this c++ and I missed some lectures at the beginning of this semester because I was in the hospital...so help and be kind...lol

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    #using <mscorlib.dll>
    
    using namespace std;
    
    struct point
    {
    public:
    	float quantity;
    	float price;
    };
    
    
    void get_input(char, int&, float&);
    float calculate(char, int, float);
    
    int main()
    {
    	int numbinput, quantity;//, calculate;
    	float price, total;
    	char product;
    do
    {
    	cout << endl << "Enter a number: " << endl;
    
    	cout << "1.  Enter Information" << endl;
    	cout << "2.  Calculate Sales" << endl;
    	cout << "3.  Update Informaton" << endl;
    	cout << "4.  Exit" << endl <<endl;
    	cin >> numbinput;
    
    	switch (numbinput)
    	{
    	case 1:
    		{
    			get_input(product, quantity, price);
    			break;
            }
    
    	case 2:
    		{
    			total = calculate(product, quantity, price);
    			break;
    		}
    	}
    }
    while (numbinput != 4);
    	return 0;
    }
     
    
    void get_input(char& product, int& quantity, float& price)
    {
    		//char product;
    		//int quantity;
            cout << "Product Name: ";
    		cin >> product;
    		cout << "Quantity: ";
    		cin >> quantity;
    		cout << "Price: ";
    		cin >> price;
    		//cout << "Press the spacebar to continue...";
    		//getch();
    }
    
    float calculate (char& product, int& quantity, float& price)
    {
    	//char prod;
    	//int quant;
    	float total;
    
    	cout << "Product: " << product;
    	cout << "Quantity: " << quantity;
    	cout << "Price: " << price;
    	total = price * quantity;
    
    	return total;
    }

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    You are already passing by refrence to your functions. I'm not sure I understand your question. Your prototypes seem incorrect you are passing by refrence. char & should be the prototype not type char.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    The point of passing a value by reference is so that you can modify it within the function and the changes will be saved to the variables that were passed to the function.

    Try compiling and running this, and notice how the function that takes references (with the '&') actually changes the variables passed to it, while the other one doesn't. Also noticed that the variable in main() doesn't have to have the same name as the one in the function, because they are two different variables.
    Code:
    #include <iostream>
    using namespace std;
    
    void ChangeData(int&, int&, int&);
    void UseData(int, int, int);
    
    int main()
    {
        int num1 = 0;
        int num2 = 0;
        int num3 = 0;
    
        cout << "Before calling ChangeData: ";
        cout << num1 << ", " << num2 << ", " << num3 << endl;
    
        ChangeData(num1, num2, num3);
    
        cout << "After calling ChangeData: ";
        cout << num1 << ", " << num2 << ", " << num3 << endl;
    
        UseData(num1, num2, num3);
    
        cout << "After calling UseData: ";
        cout << num1 << ", " << num2 << ", " << num3 << endl;
    
        return 0;
    }
    
    void ChangeData(int& one, int& two, int& three)
    {
        one = 1;
        two = 2;
        three = 3;
    }
    
    void UseData(int one, int two, int three)
    {
        one = 16;
        two = 17;
        three = 18;
    }
    So, hopefully that helps you understand that your calculate function isn't changing the passed in values, it is only using them to calculate the total. Other notes:

    1. Make sure the function prototype (above main()) matches the function definition (where you actually write the function). In your posted code, you have:

    void get_input(char, int&, float&);

    but in your definition you have

    void get_input(char& product, int& quantity, float& price)

    Make sure you add the '&' to the prototype at the top. The same goes for the calculate function. If the prototype does not have '&', the definition shouldn't either.

    2. You have a product name stored as a char. That holds a single character, not a string. I'd suggest when you test your method only typing in a single character as the product name for now (e.g. 'L'). Then, once you have everything else working you can figure out how to get an entire string instead of a single char.

    3. Your calculate function calculates the total, but the total is never output. Either output the total in the function or save the return value when you call the function in main.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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