0

I've done this C++ data structures program is mostly correct, however I am having trouble with the RefFunction() parameters. They are not designed quite properly. They are not supposed to be pass by value, but rather pass by reference and I'm not sure how to do that. It takes a reference to an int, and a reference to a double. It queries the user to input values to be stored in the variables referenced by its arguments. Then be able to return in the class instance and print the values in main(). I greatly appreciate any help as I am very stuck. Thank you very much.

Header file:

#ifndef Prog1Class_h
#define Prog1Class_h


//A data structure of type Prog1Struct containing three variables
struct Prog1Struct
{
    int m_iVal;
    double m_dVal;
    char m_sLine[81];
};

// A class, Prog1Class, containing a constructor and destructor
// and function prototypes
class Prog1Class
{
public:
    Prog1Class(); 
    ~Prog1Class(); 

    void PtrFunction(int *, double *);
    void RefFunction(int, double);
    void StructFunction(Prog1Struct *);
};

#endif 

.CPP file

#include "Prog1Class.h"
#include <string>
#include <iostream>
using namespace std;

Prog1Class::Prog1Class() {}
Prog1Class::~Prog1Class() {}

// PtrFunction shall query the user to input values to be stored in the
// variables referenced by it's pointer arguments
void Prog1Class::PtrFunction(int *a, double *b)
{
    cout << "Input keyboard values of type integer and double"<<endl; 
    cin>>*a >>*b;
}

// RefFunction shall be a C++ Reference function and shall query the user to
// input values to be stored in the variables referenced by it's arguments
void Prog1Class::RefFunction(int a, double b)
{
    cout << "Input keyboard values of type integer and double"<<endl;
    cin >>a >>b;
}

// StructFunction shall query the user to input values to be stored in the
// three fields of the data structure referenced by its argument
void Prog1Class::StructFunction(Prog1Struct* s)
{
    cout << "Input keyboard values of type integer and double"<<endl; 
    cin >>s->m_iVal>>s->m_dVal;
    cout <<"Input a character string";
    cin.ignore(1000, '\n');
    cin.getline(s->m_sLine, 81, '\n'); 
}
dyp
  • 38,334
  • 13
  • 112
  • 177
Larry T.
  • 35
  • 2
  • 9
  • Please avoid pointers: In your case it is just void fn(int&, double&). –  Oct 15 '13 at 14:17
  • Passing by value is not bad, but changes are not passed to the caller: 'void Prog1Class::RefFunction(int a, double b)' –  Oct 15 '13 at 14:23

1 Answers1

2

In C++, you don't need to use pointers to pass by reference.

Declare the function like this:

void Prog1Class::RefFunction(int& a, double& b)

The changes you make to a and b inside RefFunction will be reflected in the original variables

digital_revenant
  • 3,274
  • 1
  • 15
  • 24
  • Thank you very much, the program compiles and runs correctly now. – Larry T. Oct 15 '13 at 15:24
  • "In C++, you don't need to use pointers to pass by reference." I would say that you NEVER "use pointers to pass by reference". Using pointers is pass by value. – newacct Oct 16 '13 at 00:07