Solved

Incorrect void function

Posted on 2004-09-28
4
385 Views
Last Modified: 2010-04-01
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;      
}      
   
0
Comment
Question by:bananaamy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Assisted Solution

by:efn
efn earned 200 total points
ID: 12176421
The Wages::netPay function calculates net pay and stores it in a local variable also named netPay.  When control leaves that function, that variable disappears.  The netPay put out by the displayLine function is the address of the netPay function.

It would work better to store the calculated value in a member variable, not a local variable, so it will still be around when you want to put it out.  It would also help to give it a name different from the function name.

--efn
0
 
LVL 4

Assisted Solution

by:MacroLand
MacroLand earned 100 total points
ID: 12176681
It is better if u change the name of the variable netPay to netPay1 or else cause it is same as the functions name as efn has written.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 100 total points
ID: 12177430
Change your class to that:

class Wages
{
private:
     string name;
     float hours;
     char classification;
     float rate;
     float wage;
     float tax;
     char enterChar;
     float net;
     
     public:
     void getName();
     float values();
     float assignHourlyRate();
     void calWage();
     void assignTax();
     void netPay();
     void displayHeadings();
     void displayLine();
};

And Wages::netPay to that:

void Wages::netPay()
{    
    net = (wage-tax);    // calculate Wages::net data member
     
     cout<< net <<endl;    //value correct here and later in displayLine
}

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 <<net ;
     cout << endl;
     cout << endl;    
}

Regards, Alex




 
0
 
LVL 9

Accepted Solution

by:
zaghaghi earned 100 total points
ID: 12177452
Hi,
Just change the name of your local variable `netPay' to something like `NetPay' and decalre it as a private member of your class.
u can't declare `netPay' as a private member because u have function name like that,so u get error.
second you can't  use a local variable (float netPay) in other functions (displayLine()),because it is not in other functions' scope,
but u did not get error in function displayLine and other member functions 'cause u have a function named netPay,and cout<<netPay prints out the address of that function,not the value u've calculated before.

good luck.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Starting a function in a c++ application with a TCP command from C# 4 91
VS2015 Redefinition errors 4 102
Need some help with design 17 46
COM server issue 2 23
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question