#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <iomanip>
#include <fstream>
usingnamespace std;
constint size = 50;
struct EmpData
{
char empNameDatf[size];
char empNameDatl[size];
char stat[size];
int empIdDat[size];
double empHrsWrk[size];
double empPayDat[size];
double totalPay[size];
};
void dispMenu(int &choice);
void empNamef(struct EmpData *);
int main()
{
//dec vars
string fName, lName, stat;
int choice, index;
double hrsWrk, payRt, empId;
ofstream outFile;
ifstream inFile;
EmpData empArray[10];
//intro
cout << '\n';
cout << "Hello, this program will help you run your payroll and find employee payroll information. \n";
cout << "Please select one of the options from the menu to start: \n";
cout << '\n';
do
{
//call menu
dispMenu(choice);
//switch if program not ended
if (choice != 8)
{
switch(choice)
{
//enter employee name case
case 1: empNamef(&empArray);
cout << "Name: " << empArray.empNameDatf << endl;
break;
//enter employee ID number case
case 2: ;
break;
//enter employee hours case
case 3:
break;
//enter employee pay case
case 4:
break;
//employee report sorted case
case 5:
break;
//total employee pay case
case 6:
break;
//employee search case
case 7:
break;
}
}
}while(choice != 8);
return 0;
}
//disp menu function
void dispMenu(int &choice)
{
cout << "\n";
cout << "Enter employee information Below: \n";
cout << "\n";
cout << " Enter 1: Enter employee Name \n";
cout << " Enter 2: Enter employee ID number \n";
cout << " Enter 3: Enter employee Hours worked \n";
cout << " Enter 4: Enter employee Payrate \n";
cout << '\n';
cout << "Generate a Payroll Report Below: \n";
cout << '\n';
cout << " Enter 5: Complete employee payroll information sorted by ID number \n";
cout << " Enter 6: Calculate Payroll \n";
cout << " Enter 7: Search for employee Payroll Information by ID number \n";
cout << " Enter 8: Quit Program \n";
cout << '\n';
cin >> choice;
cout << '\n';
}
void empNamef(EmpData *s)
{
cout << "Please enter employee #" << "'s First name: \n";
cin.ignore();
cin.getline(s->empNameDatf, size);
cout << '\n';
}
Thank you! But why do I not use the & operator in the call?
I changed this code, but if I add s->empNameDatf[index] in the function definition it wont compile and if I don't it compiles but only reads part of the first element.
actually empNamef( empArray ); means that you pass the address of empArray[ 0 ] to empNamef function, that call is only valid if you want to pass empArray[ 0 ] to the function, for other indexes you will need empNamef( &empArray[ index ] )
I changed this code, but if I add s->empNameDatf[index] in the function definition it wont compile and if I don't it compiles but only reads part of the first element.
no, you don't need that for loop, but instead rethink how you will design the menu and entry of data.
your code flow can be interpreted as follow :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main ()
{
EmpData empArray[ 10 ];
do
// print menu
// enter choice
case 1 :
// read employee name by passing &empArray[ ? ] to it
// print it
case 2 :
// read ID number by passing &empArray[ ? ] to it
// print it
case 3 :
// read work hour by passing &empArray[ ? ] to it
...
}
The problem w/ this is the reading of data is misleading, how would you know what element of the array will you pass to the function ?
A solution to this is to implement again your menu and group the reading of information under 1 case :
int main
{
EmpData empArray[ 10 ];
int index = 0; // <- Note: we now have an index to work w/
do
//-- group the reading of data into 1 case, much like :
//-- 1. Enter Information( name, id, ... ) <- here reading of info is move into "1 case", so you don't mess up with indexes
//---//--//--//
//-- 2. print info's
//-- 3. Calculate Payroll
//-- 4. etc...
//-- 5. exit
cin >> choice;
switch ( choice ) {
case 1:
//-- call function to store the name
empNamef( &empArray[ index ] );
//-- call function to store ID
empIdf( &empArray[ index ] );
//-- read other infos
// increment index so in the next "case 1" the information will be stored on the next element of the array :
index++;
if( index == 10 ) return 0; // if index == the size of the array, exit the program ( to prevent overflows )
break;
case 2 :
//-- print infos
for( int i = 0; i < index; i++ ) {
std::cout << empArray[ i ].name << " " << empArray[ i ].ID << etc...
}
break;
case 3 ...
case 4...
case 5 :
return 0;
}
}