bananaamy
asked on
Incorrect void function
I have a program that I have been experimenting with to learn C++, and found that when I added a Void function to display a line that I am not displaying one of the values correct. The value is correct when it peforms the calculation, but not when the display line calls the value. I have tried arranging the code several different ways, but I am not having any sucess. Would someone please explain to me what I am missing. My code is attached and comments are included where I am having problems. I prefer comments and suggestion as opposed to you wonderful experts rewriting for it for me. Thxs Much, Annie
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <string>
#include <iomanip>
using namespace std;
class Wages
{
private:
string name;
float hours;
char classification;
float rate;
float wage;
float tax;
char enterChar;
public:
void getName();
float values();
float assignHourlyRate();
void calWage();
void assignTax();
void netPay();
void displayHeadings();
void displayLine();
};
int main ()
{
Wages theWages;
while(true)
{
theWages.getName();
theWages.values();
theWages.assignHourlyRate( );
theWages.calWage();
theWages.assignTax();
theWages.netPay();
theWages.displayHeadings() ;
theWages.displayLine();
}
}
void Wages::getName()
{
cout<<"Please enter the Employees name: ";
cin >> name ;
cout<<setw (33) << right <<"You have entered:"<< name << endl;
cout<<setw (33) << right << "Is this correct? ";
cin >>enterChar ;
if (enterChar == 'N'||enterChar == 'n')
{
cout<<"Please reenter the correct name: ";
cin >> name;
}
}
float Wages::values()
{cout << "Enter "<<name<<"'s " << "total hours worked"": ";
cin >> hours ;
return 0;
}
float Wages::assignHourlyRate()
{
cout << "Enter classification: ";
cin >> classification;
classification = toupper(classification);
while (classification != 'A' && classification != 'B' && classification != 'C' && classification != 'D')
{
cout << "Your choices are A, B, C,or D : ";
cin >> classification;
classification = toupper(classification);
}
switch (classification)
{
case 'A':rate = 10.25;break;
case 'B':rate = 8.5;break;
case 'C':rate = 6.3;break;
case 'D':rate = 4.65;break;
}
return 0;
}
void Wages::calWage()
{
wage = (rate*hours);
if (hours > 40)
{
wage = (wage+((hours-40)* rate/2));
}
}
void Wages::assignTax()
{
if (wage<=200.00)
{tax = (wage *.12);
cout <<"Tax rate:"<<" .12"<< endl;
}
else
if
(wage >200.00 && wage <= 350.00)
{
tax = (wage *.15);
cout <<"Tax rate:"<<" .15"<< endl;
}
else
{
tax = (wage *.18);
cout <<"Tax rate:"<<" .18"<< endl;
}
}
void Wages::netPay()
{ float netPay; //error message if I declared this in private.
netPay = (wage-tax);
cout<< netPay<<endl; //value correct here, but doesn't output in the void below
}
void Wages::displayHeadings()
{
cout << "========================= ========== ========== ===="
<< "=====================\n";
cout<<endl;
cout << setw (12) << left << "Name"
<< setw (4) << right << "Hours Worked"
<< setw (7) << right << "Class"
<< setw (11) << right << "Hrly Wage"
<< setw (9) << right << "Net Pay"
<< setw (6) << right << "Tax"
<< setw (12) << right << "Gross Pay" << endl;
cout << "------------------------- ---------- ---------- ---"
<< "----------------------\n" ;
}
void Wages::displayLine()
{
cout << setw (12) << left << name
<< setw (11) << right << hours
<< setw (8) << right << classification
<< setw (11) << right << rate
<< setw (7) <<"$" << right <<wage
<< setw (6) <<"$" << right <<tax
<< setw (9) <<"$" << right <<netPay ; //This should display a value that was calculated above.
cout << endl;
cout << endl;
}
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <string>
#include <iomanip>
using namespace std;
class Wages
{
private:
string name;
float hours;
char classification;
float rate;
float wage;
float tax;
char enterChar;
public:
void getName();
float values();
float assignHourlyRate();
void calWage();
void assignTax();
void netPay();
void displayHeadings();
void displayLine();
};
int main ()
{
Wages theWages;
while(true)
{
theWages.getName();
theWages.values();
theWages.assignHourlyRate(
theWages.calWage();
theWages.assignTax();
theWages.netPay();
theWages.displayHeadings()
theWages.displayLine();
}
}
void Wages::getName()
{
cout<<"Please enter the Employees name: ";
cin >> name ;
cout<<setw (33) << right <<"You have entered:"<< name << endl;
cout<<setw (33) << right << "Is this correct? ";
cin >>enterChar ;
if (enterChar == 'N'||enterChar == 'n')
{
cout<<"Please reenter the correct name: ";
cin >> name;
}
}
float Wages::values()
{cout << "Enter "<<name<<"'s " << "total hours worked"": ";
cin >> hours ;
return 0;
}
float Wages::assignHourlyRate()
{
cout << "Enter classification: ";
cin >> classification;
classification = toupper(classification);
while (classification != 'A' && classification != 'B' && classification != 'C' && classification != 'D')
{
cout << "Your choices are A, B, C,or D : ";
cin >> classification;
classification = toupper(classification);
}
switch (classification)
{
case 'A':rate = 10.25;break;
case 'B':rate = 8.5;break;
case 'C':rate = 6.3;break;
case 'D':rate = 4.65;break;
}
return 0;
}
void Wages::calWage()
{
wage = (rate*hours);
if (hours > 40)
{
wage = (wage+((hours-40)* rate/2));
}
}
void Wages::assignTax()
{
if (wage<=200.00)
{tax = (wage *.12);
cout <<"Tax rate:"<<" .12"<< endl;
}
else
if
(wage >200.00 && wage <= 350.00)
{
tax = (wage *.15);
cout <<"Tax rate:"<<" .15"<< endl;
}
else
{
tax = (wage *.18);
cout <<"Tax rate:"<<" .18"<< endl;
}
}
void Wages::netPay()
{ float netPay; //error message if I declared this in private.
netPay = (wage-tax);
cout<< netPay<<endl; //value correct here, but doesn't output in the void below
}
void Wages::displayHeadings()
{
cout << "=========================
<< "=====================\n";
cout<<endl;
cout << setw (12) << left << "Name"
<< setw (4) << right << "Hours Worked"
<< setw (7) << right << "Class"
<< setw (11) << right << "Hrly Wage"
<< setw (9) << right << "Net Pay"
<< setw (6) << right << "Tax"
<< setw (12) << right << "Gross Pay" << endl;
cout << "-------------------------
<< "----------------------\n"
}
void Wages::displayLine()
{
cout << setw (12) << left << name
<< setw (11) << right << hours
<< setw (8) << right << classification
<< setw (11) << right << rate
<< setw (7) <<"$" << right <<wage
<< setw (6) <<"$" << right <<tax
<< setw (9) <<"$" << right <<netPay ; //This should display a value that was calculated above.
cout << endl;
cout << endl;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.