4
/*Header.h file*/

#include <iostream>
using namespace std;

int main(){
    /*Menu*/
    /*case1:

        int x;
        cout << "Input ammount:";
        cin >> x
        sugarcake(x)*/

    /*case2:

       something else

    */
}

/*sugarcake.cpp file*/

#include <iostream>
#include "Header.h"
using namespace std;

void sugarcake(int x) {
    cout << (x * 0.75) << "st egg" << endl;
    cout << (x * 0.75) << "dl sugar" << endl;
    cout << (x * 0.5) << "tsk vanillasugar" << endl;
    cout << (x * 0.5) << "tsk blabla" << endl;
    cout << (x * 0.75) << "dl wheatflour" << endl;
    cout << (x * 18.75) << "gram butter" << endl;
    cout << (x * 0.25) << "dl water" << endl;
}

How do i make this work or have i understood it completely wrong? (Started out with C++ yesterday so please be kind and i haven't been able to find any clear answer that works for me elsewhere)
TL DR: Call the function sugarcake(int x) in sugarcake.cpp inside the Header.h

Widdin
  • 47
  • 1
  • 4
  • I suggest that you keep reading the book that you're using to teach yourself c++. – eerorika Nov 12 '15 at 12:30
  • There are suggestions here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Nov 12 '15 at 12:38
  • Yes, you have missunderstood how headerfiles are supposed to be used - I'd highly recommend to read a cpp tutorial or book. – MikeMB Nov 12 '15 at 13:41

2 Answers2

3

You should not put code in the header file, instead use two source (.cpp) files, and let the header file only contain a declaration of (in your case) the sugarcake function.

So the header file should look something like

// These first two line (and the last line of the file) is part of
// a header include guard (see https://en.wikipedia.org/wiki/Include_guard)
#ifndef HEADER_H
#define HEADER_H

void sugarcake(int x);

#endif // HEADER_H

The sugarcake.cpp file:

#include <iostream>
#include "header.h"  // Technically we don't need to include the header
                     // file here, but it's always a good idea to do
                     // anyway

void sugarcake(int x) {
    ...
}

And finally the main.cpp file:

#include <iostream>
#include "header.h"

int main() {
    int x;
    std::cin >> x;
    sugarcake(x);
}

How to build this depends on your environment, but if you're using GCC from the command line then you do like this

$ g++ -Wall -Wextra main.cpp sugarcake.cpp -o sugarcake

The -Wall and -Wextra options are to enable extra warnings, always a good idea. The -o options tells g++ what to name the executable program it creates. The two source files are the files you just created.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Still don't get it, what's the reason to have the header.h file? Doesn't feel like it's doing something :o Bcuz now you have the menu in a main.cpp file wich does everything? Or does the header.h "connects" all the cpp files? [main.cpp] sugarcake(5); --> [Header.h]void sugarcake(->5) --> [sugarcake.cpp] ... 5 – Widdin Nov 12 '15 at 12:41
  • The purpose is to separate roles, in a very big program, it will be easier to find functions and read code, because the program is separated in a lot of header files, which each one is doing one thing only. – ranu Nov 12 '15 at 12:43
  • @Widdin In a simple case like this you don't really need the header file, you could just have the `sugarcake` declaration in the `main.cpp` file. However, for larger projects putting common declarations, classes, symbolic constants (`const`, `constexpr` or even preprocessor macros) and other declarations that needs to be shared is really needed to avoid copy-pasting a lot of declarations (and copy-paste coding is prone to introduce subtle (or not so subtle) errors). – Some programmer dude Nov 12 '15 at 12:45
1

We use header files to describe functions which will be used after in another occasion per say.

So I will show how I organize my functions in the header files:

/* header.h
 * include guards below, it is very import to use it to be sure you'r not
 * including things twice.
 */
#ifndef HEADER_H
#define HEADER_H

// I write function prototypes in the header files. 
int sum(int &a, int &b);

#endif // HEADER_H

The definition of the function is written in the header.cpp

// header.cpp

#include "header.h"

int sum(int &a, int &b) 
{ 
    int total = a + b; 
    return total; 
}

To use that function in main.cpp you just have to include the header.cpp

// main.cpp
#include <iostream>
#include "header.h"

int main(int argc, char *argv[]) 
    {
        int a = 5;
        int b = 4;

        int total = sum(a, b);

        std::cout << "The total sum of the 4 and 5 numbers is: " << total << std::endl;
    } 

See how it is made? Then you can compile the cpp files and it will work!

ranu
  • 622
  • 15
  • 25