Skip to content

Instantly share code, notes, and snippets.

@titus-shoats
Created May 9, 2017 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save titus-shoats/d9abe8752f6239e76ed6de1fe84f7540 to your computer and use it in GitHub Desktop.
Save titus-shoats/d9abe8752f6239e76ed6de1fe84f7540 to your computer and use it in GitHub Desktop.
Returning multiple values by reference
#include "stdafx.h"
#include <iostream>
using namespace std;
void FreezeWindow();
void FreezeWindow() {
char response;
cin >> response;
}
enum ERR_CODE{SUCCESS, ERROR};
ERR_CODE Factor(int n, int &pSquared, int &pCubed);
int main() {
int number, squared, cubed;
short error;
ERR_CODE result;
cout << "Enter a number (0-20); ";
cin >> number;
result = Factor(number, squared, cubed);
if (result ==SUCCESS) {
cout << "number: " << number << endl;
cout << "square: " << squared << endl;
cout << "cubed: " << cubed << endl;
}
else {
cout << "Error encountered" << endl;
}
FreezeWindow();
return 0;
}
ERR_CODE Factor(int n, int &rSquared, int &rCubed) {
// set value to 1, dont change square or cube values
if (n > 20) {
return ERROR;
}
else {
// if n is 3
// Change square, and cube values, set value to 0
rSquared = n*n; // 9
rCubed = n*n*n; // 27
return SUCCESS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment