returning parameters to main

I am trying to make a simple calculator and the only problem I am having so far is returning my variables to the main function for another function to use. This is what I have so far.
#include <iostream>
#include <math.h>


using namespace std;

//Prototypes

//Retrieves the operator, operand and returns operator
//and operand

double scan_data(char&, double&);

//Reads operator and operand and uses switch statement
//to perform correct operator on the operand

double do_nex_op(char, double, double&);

//Reads result and outputs the result of the operator
//and the operand

void do_next_op(double);

int main()
{
char op;
double num;
double num2;
double sum;

// obtain operator and operand, and display operator
// and operand

scan_data(op,num);

// obtain results from operator and operand

do_nex_op(op,num,sum);

// display results

do_next_op(sum);

return 0;

}

// function to obtain original values

double scan_data()
{
char op;
double num;

cout<< "This program is a very basic calculator \n";
cout<< "use only operators +,-,*,/, or ^";
cout<<" to raise a number to a power\n\n";
cout<< "Enter a valid operator:\n";
cin>> op;
cout<< "\n Enter any number:\n";
cin>> num;

cout<< op << num;

return op,num;

}

// performs operation based on operator input

double do_nex_op(op, num)
{


}


nevermind a function can't return more then one variable (so i've read), so i guess i have to find another approach
nevermind a function can't return more then one variable (so i've read), so i guess i have to find another approach


This is true, but functions can change values of variables, so you may want to look into that.
Pass the variables by reference. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

void foo( int &num ) //ampersand here to pass by reference
{
	num = 15;
}

int main()
{
	int x = 10;
	foo( x );

	return 0;
}
you could also use structures:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
typedef struct
{
	int a;
	int b;
	int c;
}myType;

myType foo( ) 
{
	myType X;
	X.a = 1;
	X.b = 3;
	X.c = X.a + X.b;
	return X;
}

int main()
{
	myType X = foo();
	cout<< X.a << "+" << X.b << "=" << X.c << endl;
	return 0;
}


1+3=4
Last edited on
Topic archived. No new replies allowed.