Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Assigning a value to a variable

Posted on 2004-09-29
11
Medium Priority
?
366 Views
Last Modified: 2010-04-01
I posted a question earlier that ask what I was doing wrong in my programming. In short, several experts explained to me that I  can't name a function and a variable with the same syntax. Okay, I changed that. However, I am still not recieving the correct value. (This time I only posted fraction of the code that I have been experimenting with -if you need to see it all to understand pls advise)
Problem:
I have a funtion that is solving for a value. Then another funtion that calls that value to display it.  Why does it not display it? I tried to assign a value of  0 to the variable but that doesnt work. What should I be doing?

void Wages::netPay()                ///This my function that  calculates     do I need to aassign a value of 0 to this? If so where do I assign this?
      
{      float grossPay;
   
    grossPay = (wage-tax);
      
      cout<< grossPay<<endl;
}
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 <<grossPay ; /////This is where it should dispaly.However, My answer is 0
      
      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
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 12178184
You assing the value to a local variable. When Wages::netPay ends the value is lost.
Do you wanted to assign the value (wage-tax) to a member variable?

void Wages::netPay()
{
     // float grossPay This local will make the member variable invisible.
     grossPay = (wage-tax);
     cout<< grossPay<<endl;
}
0
 
LVL 13

Expert Comment

by:SteH
ID: 12178186
Can you please post the definition of class Wages if this was not the solution.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12178201
Hi,

You've defined float grossPay in ::netPay as a local variable. Since you output grossPay in the line
          << setw (9) <<"$" << right <<grossPay ; /////This is where it should dispaly.However, My answer is 0

I assume that grossPay is also defined as a class member. In this case, the local variable has a higher priority.

In other words, the class variable is never modified.
Simply remove the float grossPay declaration in netPay().

In order to make the local/instance distiction clearer, you can also use "this" explicitly:
void Wages::netPay()                    
{    
   
    this -> grossPay = (wage-tax);
     cout<< (this -> grossPay)<<endl;
}

But that's not necessary.

0
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!

 

Author Comment

by:bananaamy
ID: 12178303

#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;
      float grossPay ;
      
      
      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 grossPay;
   
    grossPay = (wage-tax);
      
      cout<< grossPay<<endl;
}
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 <<grossPay ;
      cout << endl;
      cout << endl;      
}      
   
0
 
LVL 13

Assisted Solution

by:SteH
SteH earned 800 total points
ID: 12178319
So the first post was the solution:

void Wages::netPay()
     
{     float grossPay;
   
    grossPay = (wage-tax);
     
     cout<< grossPay<<endl;
}

should change to

void Wages::netPay()
{     float grossPay;
    this->grossPay = (wage-tax); // this makes the local variable unsued
     cout<< grossPay<<endl;
}
or better:
void Wages::netPay()
{
    grossPay = (wage-tax); // this-> is not needed anymore
     cout<< grossPay<<endl;
}
0
 
LVL 9

Accepted Solution

by:
zaghaghi earned 800 total points
ID: 12178422
just delete the `grossPay' local variable in netPay function, 'cuse in c++ local variable hide the global (like member variables), so if you define a local variable with the same name of a member variable or global variable you have tell the c++ which one to use:
1-if you have a member variable use `this' class pointer to access that member

2-if you have a global variable you can use the  global scope operator '::' to access the global variable

3-if you just type the name of that variable then compiler use the local one.

good luck.
0
 

Author Comment

by:bananaamy
ID: 12178490
Removing     float grossPay;  corrected my problem. Thank-you, so that statement redefine the vari ble within the funtion causing other funtions not to see it?
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 12178518
no they can see the member variable, but u have stroed the result of your calculation in the local varibale not in the member varibale, so the member variable has the initial value not the calculated value.
0
 
LVL 9

Assisted Solution

by:jhshukla
jhshukla earned 200 total points
ID: 12179399
whenever you enter { }, you are entering in a new "scope". if you declare a variable and the parent scope has a variable by the same name, the parent's variable gets hidden by the local variable. note that this parent-child relationship is for nested {} and not for function calls.

classes are different case. if the compiler cannot find a variable declared locally, then it searches the members and adds this->. all this is hidden from the programmer.
0
 
LVL 4

Assisted Solution

by:pankajtiwary
pankajtiwary earned 200 total points
ID: 12184739
Refering the function below:

void Wages::netPay()
     
{     float grossPay;
   
    grossPay = (wage-tax);
     
     cout<< grossPay<<endl;
}

We already have one variable named grossPay declared in the class as a private member. Then why are you defining one more with the same name. You need to understand this.

As soon as you define a new variable in a scope, any reference made inside that scope will work for the newly defined variable and not the old one. Here as soon as you calculate grossPay = ( wage - tax ), it calculates for the gross pay defined just in the funation and not the old one. You need to change it as:

void Wages::netPay()
     
{    // float grossPay;
   
    grossPay = (wage-tax);  // Here it will refer to the class variable gross pay since no other reference with the name
     
     cout<< grossPay<<endl;
}

This should work fine. Hope this helps.
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12186279
just FYI. if you have a static member for a class, and you declare a local variable by the same name then you can access the static variable by className::varName.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

670 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