Function Header/ Source Files

Hi,
I am just now becoming exposed to C++ (ForDummies book)and programming in general and I'm confused on how to properly use headers and multiple source files. At this stage in writing this project I'me working on, I only have one of a few functions I'll be using. I have:
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
#include <iostream>
#include "Functions.h"
using namespace std;

int WeekNumber;
int WeekNumberFunction ();

int main()
{
    string Weekday;
    int Date;
    int Month;
    cout << "Enter weekday:";
    cin >> Weekday;
    cout << "Enter date:";
    cin >> Date;
    cout << "Enter month:";
    cin >> Month;

    int WeekNumberFunction ();
    cout << "Week number:" << WeekNumber << endl;
    return 0;


}


1
2
3
4
5
6
7
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

int WeekNumberFunction ();


#endif // FUNCTIONS_H_INCLUDED 


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
29
30
31
32
33
34
#include <iostream>
#include "Functions.h"

using namespace std;

int WeekNumber;

WeekNumberFunction ()
{
    if (7>Date>1)
    {
        return WeekNumber = 1;
    }
    else if (14>Date>7)
    {
        return WeekNumber = 2;
    }
    else if (21>Date>14)
    {
        return WeekNumber = 3;
    }
    else if (28>Date>21)
    {
        return WeekNumber = 4;
    }
    else if (32>Date>28
    {
        return WeekNumber = 5;
    }
    else
    {
        cout << "No week could be established.";
}


When I run the console, regardless of the Date I enter, the value "0" is returned. I suspect there may be multiple errors here, but could you help me understand what isn't going the way I think it should be? Could you point out what's not happening from my thought process?
My thought process:

The function WeekNumberFunction is located in a source file not in main. A header is being used so that main is aware of it. I use this function in main to return a value to WeekNumber (1-5) based on the Date.

Thank you for your time.

P.S.
If you have any recommendations on how to approach learning C++ for an absolute beginner, it would be appreciated.
Your problem is that you are using globals. Each file has it's own global WeekNumber. You'll need to use extern if you really want to do it this way.
Thanks, I understand what your saying, but, Zhuge, should I:
1)replace all the declarations for the int WeekNumber with extern int WeekNumber
or
2)replace all the declarations for the function int WeekNumberFunction () with extern int WeekNumberFunction () ?
Also, your post ("if you really want to do it this way")suggests that there is a better way of doing this. Is there a better way?

Pass the variable as an argument. Also, if (7>Date>1) &c won't do what you think it does. You'll need to do something like this: if(7 > Date && Date > 1)
Isn't line 20(in the main method) just a function prototype? That shouldn't be needed unless I am missing something
Last edited on
Sorry, but I'm a little confused. Firedraco, what does "Date && Date" mean? When you say I should pass the variable as an argument, what do you mean?

in C++, you can only compare two things at a time basically.. so you first have to determine that
(7 > Date) /*that 7 is greater than Date*/ && /*and*/ (Date > 1) /* that Date is greater than 1*/

&& is the AND 'logical operator' see http://www.cplusplus.com/doc/tutorial/operators/

You can't say, "is date less than 7 greater than 1?" the compiler doesn't understand.. just like that sentence isn't syntactically correct.

You're supposed to ask the computer "Is Date less than 7, and is Date greater than 1?"

Last edited on
As for passing the variable as an argument..

1
2
3
4
5
6
int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}
This code was taken from http://www.cplusplus.com/doc/tutorial/functions/

What they are saying is you have 2 different variables in your program called WeekNumber, and two variables inaccessible by your function called Date and Month.


By passing the variables as arguments, you define the variables once, and then tell the function what those variables are by passing them as arguments.
Last edited on
Topic archived. No new replies allowed.