Inputing variables from main file into function file

I'm trying to get a problem to work. I simplified it to the sum of 2 numbers so that maybe it'd be easy to figure out. I want to create a class, functions, but I want to input the variables in main and then use them in those functions. I made this a 2 variable summation for simplification.

sum.h
1
2
3
4
5
6
7
8
#include <string>
using namespace std;

class SummingVariables
{
public:
	int get_sum(int, int);
};


sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

#include "sum.h"


int SummingVariables::get_sum(int a, int b)
{   
	int sum = a+b;
	return sum;
	cout << sum;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
using namespace std;

#include "sum.h"

int main()
{
int a;
int b;

cout << "Enter a" << endl;
cin >> a;
cout<< "Enter b" << endl;
cin>> b;

SummingVariables get_sum();

}



Thank you.
closed account (48T7M4Gy)
You have to create a SummingVariables object. Add these two lines to main():

1
2
SummingVariables sv;
	cout << "Answer is: " << sv.get_sum(a,b);
closed account (48T7M4Gy)
Also, line 13 in sum.cpp comes after the return statement so it will never display the sum. :)
Thanks, it works, but I can't get it to pause after I return the sum.
closed account (48T7M4Gy)
system("pause") is one way, the purists will jump on you, but tell them it's just for demo purposes. ;)

Another way is open up cmd.exe in your project (debug) directory and run your .exe file from there.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/1988/
Thanks!
Topic archived. No new replies allowed.