Question about porting code from Visual Studio to Geany.
We're doing functions now.
I can make it all be one big file, but is there a way to have it as three separate files that reference each other?
He wants to have it in three separate files: header file that describes functions, file that defines them, and main() file for reasons X, Y, and Z that I've no choice but to accept and that thus aren't particularly relevant.
Snip from today's introductory example (ignore the 'math' in the filenames, mentally replace it with an appropriate name - I also haven't done data validation yet, so ignore that too):
math_H.h
Code:
#include <iostream>
using namespace std;
#ifndef randomManip
#define randomManip
double employeeGrossPay (int hours, double wage);
//Preconditions: both are non-negative numbers, hours is an intiger and wage is a decimal value.
//Postconditions: Returns the gross pay of one employee, giving time and a half for hours past 40.
double totalGrossPay (double currentTotal, double latestEmployee);
//Preconditions: both are non-negative numbers, currentTotal is the running tally and latestEmployee
// is to be added to that.
//Postconditions: Running tally of total gross pay is updated by summing the two numbers.
#endif
math_Imp.cpp
Code:
#include <iostream>
#include "math_H.h"
using namespace std;
#ifndef randomManipImp
#define randomManipImp
double employeeGrossPay (int hours, double wage)
{
int hoursOver40 = 0;
double grossPay = 0.0;
if (hours > 40) // separate normal time from overtime hours
{
hoursOver40 += hours - 40;
hours = 40;
}
grossPay = (hours * wage) + (hoursOver40 * 1.5 * wage);
return grossPay;
}
double totalGrossPay (double currentTotal, double latestEmployee)
{
return currentTotal + latestEmployee;
}
math_test.cpp
Code:
#include <iostream>
#include <ctime>
#include <iomanip>
#include "math_H.h"
using namespace std;
int main()
{
int hours = 0;
double wage = 0.0;
char more = 'y';
int counter = 0;
double allEmployeesTotal = 0;
do // main loop for entering employee info
{
counter++;
cout << "Enter hours for employee #" << counter << ":\t";
cin >> hours;
cout << "Enter wage for employee:\t$";
cin >> wage;
cout << "Gross Pay for that employee:\t$" << setprecision(2) << fixed
<< employeeGrossPay(hours, wage) << endl;
//update running total for total pay for all employees.
allEmployeesTotal = totalGrossPay(allEmployeesTotal, employeeGrossPay(hours, wage));
cout << "More employees? (y/n)\t";
cin >> more;
more = tolower(more);
} while (more != 'n');
cout << "Total gross pay for all " << counter << " employees:\t$" << setprecision(2) << fixed
<< allEmployeesTotal << "." << endl;
return 0;
}
Bookmarks