please help small problem ( making header file and c++ test )

Hi,
I have an assignment due today that says

javascript:tx('Write a C++ program that calculates the sum of three integers. Your answer should have three (3) C++ files as described below:
 A header file
 It should define the prototype of a function calculating the sum of three integers. That is, this function has three parameters, and the data type of each parameter is integer. The value returned from this function is the sum of these three parameters.
 A C++ program
 It includes the implementation of the function defined in the header file.
 A C++ test program
 The purpose of this program is to test the performance of the C++ program you have
developed.
 Its working procedure can be:
 Define three variables. The data type of each variable should be integer.
 Calling the function calculating the sum of three integers by passing the values of
the variables defined in the previous step.')


and what i did is this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <iostream>
using namespace std;

int addNumbers ( int x, int y, int z){
    
    int sum=x+y+z;
    
    
    
    return sum;
    
}



int main() {
    
    int a,b,c;
    cout<<"Please enter the 3 integers: ";
    cin>> a >> b >> c;
    cout<<"the sum of the 3 integers is: ";
    cout<< addNumbers(a , b , c)<<endl;
    
    
    
    return 0;
}


i don't know how to divide it into 3 files ( header, c++ program, c++ test)

please help
First, you need a compiler.

1. Create a project (solution) - e.g mysum

2. Create a header file (mysum.h)
1
2
3
4
#include <iostream>
using namespace std;

int addNumbers ( int x, int y, int z);


3. Create an implementation file (mysum.cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "mysum.h"

int addNumbers ( int x, int y, int z){
    
    int sum=x+y+z;
     
    return sum; 
}

int main() {
    
    int a,b,c;
    cout<<"Please enter the 3 integers: ";
    cin>> a >> b >> c;
    cout<<"the sum of the 3 integers is: ";
    cout<< addNumbers(a , b , c) <<endl;
    
    return 0;
}


4. Compile the solution and you get the test program mysum.exe. Create a new folder named mysum and copy the three files to it, then submit it to your (instructor)

Hope that helps.
What compiler are you using?
Am using xCode on OS
When i go to add file and pick c++ ot creates to files hpp and cpp
Under the same name should i delete the cpp and rename the hpp to .h ?
closed account (48T7M4Gy)
Its quite ok to rename the header as a .h file instead of .hpp

Remember though any #include must be compatible with your choice of name and suffix

You don't have to keep both files if you only want a header file
Last edited on
> When i go to add file and pick c++ ot creates to files hpp and cpp
Under the same name should i delete the cpp and rename the hpp to .h ?
Just leave the cpp file alone.
The .hpp file should behave just like the .h header file. Just replace any ".h" you can find in my code with ".hpp".
Last edited on
Awesome! so far so good!
now i have 3 files in my project
"mysum.h" which has the prototype
"mysum.cpp" which has
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include "mysum.h"

int addNumbers ( int x, int y, int z){
    
    int sum=x+y+z;
     
    return sum; 
}

int main() {
    
    int a,b,c;
    cout<<"Please enter the 3 integers: ";
    cin>> a >> b >> c;
    cout<<"the sum of the 3 integers is: ";
    cout<< addNumbers(a , b , c) <<endl;
    
    return 0;
}


and an empty "my sum.hpp" file

now i don't actually get the compiling part sorry. how to i create that test program?
i only have Xcode installed in my mac
the instructor said you have to compile C++ programs on modern Unix platforms without explaining anything
You compile your solution, if something happens to go wrong just copy the error logs from the compiler and show it to us.

Upon success, the compiler should produce an executable file. It is the test program that you need.
At least in every compiler there should be some instructions on how to compile a project or solution. And you can find it easily by just googling...
I used Terminal on MAC and i did what i read on google and then
i got an "a.out" file which runs the program fine on terminal!

but now am guessing this test program won't work on Unix system right?
what should I do?

closed account (48T7M4Gy)
why are you keeping the .hpp file? You don't need it.
> but now am guessing this test program won't work on Unix system right?
If Unix is also a Mac OS then yes it should.
ok i deleted it but then how can i run this on Unix?
i compiled using terminal app on mac using the g++ mysum.cpp
and it gave me an a.out file

what should i do
closed account (48T7M4Gy)
I thought you said you have a mac with xcode. That's all I have in front of me and I can help with that but I have no Unix. I'm using XCode 7.1
alright then thanks all for tall the help!!!!

i will submit the files a.out, my sum.cpp my sum.h

thanks
closed account (48T7M4Gy)
mac osx is unix-based AFAIK but it's not Unix
You need to pay more attention to what your tutor wrote:

Your answer should have three (3) C++ files as described below:
A header file
[...]
A C++ program
[...]
A C++ test program
Topic archived. No new replies allowed.