Skip to content

Instantly share code, notes, and snippets.

@DerekV
Created October 12, 2012 22:42
Show Gist options
  • Save DerekV/3882060 to your computer and use it in GitHub Desktop.
Save DerekV/3882060 to your computer and use it in GitHub Desktop.
Pass by refrence, example with alternatives
#include <iostream>
#include <utility>
using namespace std;
void getHoursRate(double &hours, double &rate)
{
cout << "enter hours:" << endl;
cin >> hours;
cout << "enter rate:" << endl;
cin >> rate;
}
void getHoursRate2(double *hours, double *rate)
{
cout << "enter hours:" << endl;
cin >> *hours;
cout << "enter rate:" << endl;
cin >> *rate;
}
struct HoursAndRate
{
double hours;
double rate;
};
HoursAndRate getHoursRate3()
{
HoursAndRate input;
cout << "enter hours:" << endl;
cin >> input.hours;
cout << "enter rate:" << endl;
cin >> input.rate;
return input;
}
pair<double,double> getHoursRate4()
{
pair<double,double> userInput;
cout << "enter hours:" << endl;
cin >> userInput.first;
cout << "enter rate:" << endl;
cin >> userInput.second;
return userInput;
}
int main()
{
double hours;
double rate;
// method 1
getHoursRate(hours,rate);
cout << "hours is " << hours << " and rate is " << rate << endl;
// method 2
getHoursRate(hours,rate);
cout << "hours is " << hours << " and rate is " << rate << endl;
// method 3
HoursAndRate userInput = getHoursRate3();
cout << "hours is " << userInput.hours << " and rate is " << userInput.rate << endl;
// method 3
pair<double, double> pair = getHoursRate4();
cout << "hours is " << pair.first << " and rate is " << pair.second << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment