0
#include <iostream>

using namespace std;

void calc () {
    double num1, num2, result;
   char op;

   cout << "Enter your first number \n";
   cin >> num1;

   cout << "Now enter your second number \n";
   cin >> num2;

   cout << "And last but not least, what operator do you want to use? \n";
   cin >> op;

   if (op == '*') {
    result = num1 * num2;
   }
   else if (op == '+') {
    result = num1 + num2;
   }
   else if (op == '-' || op == '-') {
    result = num1 - num2;
   }
   else if (op == '/') {
    result = num1 / num2;
   }
   else {
    cout << "Invalid operator";
   }
   cout << "The result is " << result << endl;
}
}


int main()
{

 calc();

    //cout - console output
    //endl - end line
    //cin - console input
    // >> - input


}

so thats my code, and i was hoping to get the calc() function inside a different .cpp file, and then somehow include it into the main file (it will just make the whole project look cleaner in my opinion, thats why i want to do that)

Botje
  • 26,269
  • 3
  • 31
  • 41
exdii
  • 7
  • 4
  • 1
    Open your C++ textbook and search for the concepts "header files" and "separate compilation". – Botje Jul 15 '20 at 07:31
  • I am afraid that i do not understand a single thing in there xd – exdii Jul 15 '20 at 07:39
  • @exdii Then I suggest you really need to buy a book on C++ which can explain these things in detail. What you are asking about is pretty fundamental to C++ programming. Book suggestions [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – john Jul 15 '20 at 07:40

2 Answers2

0

You can always define the function in a Header file and then include that header file in your main program. You can learn all things about Multiple Files Programs from here Multiple Code Files Programs in C++

Rahul Badgujar
  • 142
  • 2
  • 8
0

So to move the calc function you just cut and paste it to a new file. The tricky part is combining the two files. To do that you create a header file and #include the header file in both your cpp files. The header file contains a declaration of the function. This is how the main file knows about the function in the other file. Here's how the header file might look, I'm assuming that you are going to call the file calc.h.

#ifndef CALC_H
#define CALC_H

void calc(); // function declaration (aka function prototype)

#endif

The CALC_H stuff is an include guard, look that up, it would take too long to explain here.

Then as I said you just put

#include "calc.h"

at the top of both your cpp files.

john
  • 85,011
  • 4
  • 57
  • 81
  • Both of my cpp files?????? I need 2 cpp files???? Im so confused. Ik that one of them is the main file, but what do i put in the second, just this? : `#include "calc.h"` `// the funtion here` ??? – exdii Jul 15 '20 at 07:57
  • OK, THANK YOU SO SO MUCH! It actually works! – exdii Jul 15 '20 at 08:11
  • @exdii Glad to help. So in the end you should have three files called (for instance) main,cpp, calc.cpp and calc.h. main.cpp has your `main` function, calc.cpp has your `calc` function and calc.h is as described above. – john Jul 15 '20 at 08:19
  • @exdii Usually you declare a function `func()` in func.h and you create the body of the function in func.cpp . It isn't necessary, but that's how you'll usually see this type of stuff being made – H-005 Jul 15 '20 at 08:36
  • @H-005 ok, uhm sure? – exdii Jul 15 '20 at 08:54