I have one header file that my teacher requires. I am using a global variable so I can move the value from one function to another. Looking up the problem, someone recommended using a namespace. That did not work. Additionally, I added some guards like someone else recommended, but that did not help either. I have multiple files and the commonly accepted solution of extern in header and then the declaration in the .cpp does not work for me for some reason. Global variables in header file
Methods.h
#pragma once
#define METHODS_H
#include <cfguard.h>
#ifdef METHODS_H
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
// declaring global variables
namespace global_variables
{
int number_of_employees;
}
#endif
One usage of the variable in WSCreate.cpp
// for loop to make sure amount of employees is integer
int count = 0;
int triggered = 0;
while (true){
cout << "Enter the number of employees you would like to enter\n";
string employees_in_string_format;
getline (cin, employees_in_string_format);
for (int i = 0; i < employees_in_string_format.length(); ++i)
{
triggered = 0;
if (isdigit(employees_in_string_format[i]) == false)
{
count = 1;
triggered = 1;
}
else
{
count = 0;
}
}
if (triggered == 1)
{
cout << "One of your inputs for the amount of employees you have is not a positive integer. Please enter \n positive integers\n";
}
else if (triggered == 0)
{
cout << "Your inputs are validated!\n";
number_of_employees = stoi(employees_in_string_format);
break;
}
}
WSRead.cpp (another usage of this global variable)
for (int i = 0; i != number_of_employees; i++)
{
cout << endl;
employees >> printing;
cout << printing << " ";
employees >> printing;
cout << printing << "\t ";
employees >> printing;
cout << printing << "\t\t";
employees >> printing;
cout << printing << "\t\t";
employees >> printing;
cout << printing;
cout << endl;
}
Again, sorry if it is a duplicate question, but I could not find the solution to my problem.
Thanks in advance.
If you need more code, let me know.