Functions

I dont understand how to make functions or how to get them to work? Please help

these are the directions

Function Name: display( )
Arguments: None
Return Value: None
Prototype: void display(void);
Action: Display name and lab number

Function Name: code_not_valid( )
Arguments: pay code (a char)
Return Value: true, if bad code; false, if good code
Prototype: bool code_not_valid(char);
Action: Use do/while loop in main to enter pay code
Test T/F value returned by function to see if another iteration is necessary

this is what i got so far

#include <iostream>
using namespace std;
// Begin main Function Definition
void code_not_valid(char);
void display(void);

int main()
{

cout<<" Programmed by Devon Swanton"<<endl;
cout<<" Lab 5 A"<<endl;
cout<<"____________________________"<<endl;
}
bool code_not_valid()
{
const double min_wage = 7.40,y=1;
string employeenum;
double hrsworked, payrate, totalpay,overtimepay, overtimehours, payroll, employees;
char wagecode, again;
do
{
cout<<"What is the employee number?"<<endl;
cin>> employeenum;
cout<<"Wage code M/O/T?"<<endl;
cin>> wagecode;
cout<<"How many hours worked"<<endl;
cin>> hrsworked;
} while (wagecode == (M||O||T||m||o||t);
return wagecode;
}
switch (wagecode)
{

case 'M || m': cout<<"You entered M."<<endl;
if (hrsworked>40)
{

overtimehours= hrsworked - 40;
payrate= min_wage;
overtimepay= overtimehours * payrate * 1.5;
totalpay= 40 * payrate + overtimepay;
cout<<"Employee #:"<< employeenum <<endl;
cout<<"Pay Rate:"<< payrate <<endl;
cout<<"Hours worked:"<< hrsworked <<endl;
cout<<"Total Pay:"<< hours*payrate <<endl;
}
else
{
payrate=min_wage;
totalpay=hrsworked*payrate;
cout<<"Employee #:"<< employeenum <<endl;
cout<<"Pay Rate:"<< payrate <<endl;
cout<<"Hours worked:"<< hrsworked <<endl;
cout<<"Total Pay:"<< hours*payrate <<endl;
}
break;
case 'O || o': cout<<"You entered O."<<endl;
if (hrsworked>40)
{

overtimehours= hrsworked - 40;
payrate= min_wage + 1.00;
overtimepay= overtimehours * payrate * 1.5;
totalpay= 40 * payrate + overtimepay;
cout<<"Employee #:"<< employeenum <<endl;
cout<<"Pay Rate:"<< payrate <<endl;
cout<<"Hours worked:"<< hrsworked <<endl;
cout<<"Total Pay:"<< totalpay <<endl;
}
else
{
payrate=min_wage + 1.00;
totalpay=hrsworked*payrate;
cout<<"Employee #:"<< employeenum <<endl;
cout<<"Pay Rate:"<< payrate <<endl;
cout<<"Hours worked:"<< hrsworked <<endl;
cout<<"Total Pay:"<< totalpay <<endl;
}
break;
case 'T || t': cout<<"You entered T."<<endl;
if (hrsworked>40)
{

overtimehours= hrsworked - 40;
payrate= min_wage + 2.00;
overtimepay= overtimehours * payrate * 1.5;
totalpay= 40 * payrate + overtimepay;
cout<<"Employee #:"<< employeenum <<endl;
cout<<"Pay Rate:"<< payrate <<endl;
cout<<"Hours worked:"<< hrsworked <<endl;
cout<<"Total Pay:"<< totalpay <<endl;
}
else
{
payrate=min_wage + 2.00;
totalpay=hrsworked*payrate;
cout<<"Employee #:"<< employeenum <<endl;
cout<<"Pay Rate:"<< payrate <<endl;
cout<<"Hours worked:"<< hrsworked <<endl;
cout<<"Total Pay:"<< totalpay <<endl;
}
break;
default: cout<<"You did not enter M/O/T?"<<endl;
}
cout<<"Do you want to do another employee process (Y/N)?"<<endl;
cin>> again;
}while (again == 'Y'|| again == 'y');

employees=1*y;
payroll=totalpay+totalpay+totalpay+totalpay;
cout<<"Number of employees processed" <<employees<<endl;
cout<<"Total payroll?" << payroll<<endl;
system("pause");
return 0;
}
you make functions like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

void function(); //Function prototype

int main()
{
 function(); //Calling our other function in main
}

void function()
{
 cout << "im in another function" << endl;
}
Last edited on
your code_not_valid() function is returning a different type then what you declared above main, and it takes a different argument list.

void code_not_valid(char);

bool code_not_valid()//should be code_not_valid(c);

you also have this function nested within the main function.

Think of functions as something external to the main. For example, the police serve a function by protecting people who are incapable of protecting themselves. If you have a problem, you call the police:

1
2
3
bool Crime;
if(Crime)
   CallPolice(Crime);


in this case CallPolice(...) is a function that does something about crime.

if you were to put this into a semi working program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

void CallPolice(bool Crime); //function declaration

int main()
{
   bool Crime = true;
   if(Crime)
     CallPolice(Crime);

  char c;
  cin>>c;

return 0;
};


//function definition
void CallPolice(bool Crime)
{
    cout<<"Theres a crime!"<<endl;
};


notice that the definition exists OUTSIDE of the main function.
Last edited on
Topic archived. No new replies allowed.