Thread: problem comparing variables from my main program into my subprogram

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Unhappy problem comparing variables from my main program into my subprogram

    Hello,

    I am trying to write up a program that calculates gross salary and other forms of tax to be deducted from the gross to make a net pay.

    My program seems to be working fine except for my function double FEDTAX( double). there is a 12% tax for married people and an 18% tax for single people on the fed tax. I am having trouble comparing the marital status and tax that should be deducted in the fed tax function. Any help will be amazing. I have been trying a bunch of things for the last couple of days, but nonof them seems to be working. here's the program.

    Code:
    #include<iostream>
    #include<string>
    using namespace std;// ln 5
    
    // Declaring subfunctions and other global variables.
    double FEDTAX(double);
    double STATETAX(double);
    double LOCALTAX(double);
    int MARITALSTATUS;// 
    
    int main()
    {
    string FIRSTNAME, LASTNAME;// first and last names of employee.
    char MARITALSTATUS;// Marital status of employee.
    int CENTS;
    double HOURS_WORKED, HOURLY_RATE, GROSS, NET, TAX ;
    // declaring hours worked, hourly rate, gross pay, net pay and tax as long numbers. 
    cout << "Enter  the first and last names of your employee: ";
    cin >> FIRSTNAME >> LASTNAME;
    cout << " Is " <<" " << FIRSTNAME << " " << LASTNAME << " " << "married?( Enter either 1 if married or 0 if not married): "; 
    cin >> MARITALSTATUS;
    cout << "Enter the number of hours" << " " << FIRSTNAME << " " << LASTNAME << " " << "worked this"<< " " << "week: ";
    cin >> HOURS_WORKED;
    cout << "Enter your hourly rate: ";
    cin >> HOURLY_RATE;
    CENTS= HOURS_WORKED*HOURLY_RATE*100;// Calculating pay in cents.
    GROSS= CENTS/100.0;
    cout << FIRSTNAME << " " <<  LASTNAME << endl;
    cout << HOURS_WORKED << "hours worked at $ " << HOURLY_RATE << "/ hour= $ " << GROSS << endl;
    TAX= FEDTAX(GROSS);
    cout << "Federal  Taxes= $ " << TAX << endl; 
    NET= GROSS-TAX;
    TAX= STATETAX(GROSS);
    cout << "State Taxes = $ " << TAX << endl;
    NET= NET-TAX;
    TAX= LOCALTAX(GROSS);
    cout << "Local Taxes =$" << TAX <<  endl;
    NET= NET-TAX;
    cout << "Net Pay =$" << NET << endl;
    
    return 0;
    }
    
    	double FEDTAX( double GROSS)
    	{
    		int CENTS, MARITALSTATUS;
    		
    	   	  switch(MARITALSTATUS)
    			{ case '1':
    			  CENTS= 0.12*GROSS*100;
                              return CENTS/100.0;
       			  break;
    			  case '0':
    			  CENTS= 0.18*GROSS*100;
                              return CENTS/100.0;
    			  break;
    			}
    		
       	 }//End FEDTAX
    
    	
    	
    	double STATETAX (double GROSS)
    	{
    	int CENTS;
    	CENTS= 0.0307*GROSS*100;
    	return CENTS/100.0;
    	}// end STATETAX
    
    	double LOCALTAX(double GROSS)
    	{
    	int CENTS;
    	CENTS= 0.030*GROSS*100;
    	return CENTS/100.0;
    	}// end LOCALTAX

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. Indent your code.
    2. Do not use all capitals in variable/function names - that is supposed to be for macros only.

    To use local variables from one function in another function, you need to pass those as parameters.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    The code has been indented. I don't understand your reply matsp

    Code:
    #include<iostream>
    #include<string>
    using namespace std;// ln 5
    
    // Declaring subfunctions and other global variables.
    double FEDTAX(double);
    double STATETAX(double);
    double LOCALTAX(double);
    int MARITALSTATUS;// 
    
      int main()
     {
         string FIRSTNAME, LASTNAME;// first and last names of employee.
         char MARITALSTATUS;// Marital status of employee.
         int CENTS;
         double HOURS_WORKED, HOURLY_RATE, GROSS, NET, TAX ;
         // declaring hours worked, hourly rate, gross pay, net pay and tax as long numbers. 
    
    cout << "Enter  the first and last names of your employee: ";
    cin >> FIRSTNAME >> LASTNAME;
    
    cout << " Is " <<" " << FIRSTNAME << " " << LASTNAME << " " << "married?( Enter either 1 if married or 0 if not married): "; 
    cin >> MARITALSTATUS;
    
    cout << "Enter the number of hours" << " " << FIRSTNAME << " " << LASTNAME << " " << "worked this"<< " " << "week: ";
    cin >> HOURS_WORKED;
    
    cout << "Enter your hourly rate: ";
    cin >> HOURLY_RATE;
    
    CENTS= HOURS_WORKED*HOURLY_RATE*100;// Calculating pay in cents.
    GROSS= CENTS/100.0;
    
    
    cout << FIRSTNAME << " " <<  LASTNAME << endl;
    cout << HOURS_WORKED << "hours worked at $ " << HOURLY_RATE << "/ hour= $ " << GROSS << endl;
    TAX= FEDTAX(GROSS);
    
    cout << "Federal  Taxes= $ " << TAX << endl; 
    NET= GROSS-TAX;
    TAX= STATETAX(GROSS);
    
    cout << "State Taxes = $ " << TAX << endl;
    NET= NET-TAX;
    TAX= LOCALTAX(GROSS);
    
    cout << "Local Taxes =$" << TAX <<  endl;
    NET= NET-TAX;
    cout << "Net Pay =$" << NET << endl;
    
    return 0;
    }
    
    	double FEDTAX( double GROSS)
    	{
    		int CENTS, MARITALSTATUS;
    		
    	   	  switch(MARITALSTATUS)
    			{ case '1':
    			  CENTS= 0.12*GROSS*100;
                                          return CENTS/100.0;
                                          break;
    
    			  case '0':
    			  CENTS= 0.18*GROSS*100;
                                          return CENTS/100.0;
    			  break;
    			}
    		
       	 }//End FEDTAX
    
    	
    	
    	double STATETAX (double GROSS)
    	{
    	    int CENTS;
    	    CENTS= 0.0307*GROSS*100;
    	    return CENTS/100.0;
    	}// end STATETAX
    
    	double LOCALTAX(double GROSS)
    	{
    	    int CENTS;
    	    CENTS= 0.030*GROSS*100;
    	    return CENTS/100.0;
    	}// end LOCALTAX

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Raschoc
    The code has been indented.
    Very inconsistently. Contrast with:
    Code:
    #include<iostream>
    #include<string>
    using namespace std;// ln 5
    
    // Declaring subfunctions and other global variables.
    double FEDTAX(double);
    double STATETAX(double);
    double LOCALTAX(double);
    int MARITALSTATUS;//
    
    int main()
    {
        string FIRSTNAME, LASTNAME;// first and last names of employee.
        char MARITALSTATUS;// Marital status of employee.
        int CENTS;
        double HOURS_WORKED, HOURLY_RATE, GROSS, NET, TAX ;
        // declaring hours worked, hourly rate, gross pay, net pay and tax as long numbers.
    
        cout << "Enter  the first and last names of your employee: ";
        cin >> FIRSTNAME >> LASTNAME;
    
        cout << " Is " <<" " << FIRSTNAME << " " << LASTNAME << " " << "married?( Enter either 1 if married or 0 if not married): ";
        cin >> MARITALSTATUS;
    
        cout << "Enter the number of hours" << " " << FIRSTNAME << " " << LASTNAME << " " << "worked this"<< " " << "week: ";
        cin >> HOURS_WORKED;
    
        cout << "Enter your hourly rate: ";
        cin >> HOURLY_RATE;
    
        CENTS= HOURS_WORKED*HOURLY_RATE*100;// Calculating pay in cents.
        GROSS= CENTS/100.0;
    
    
        cout << FIRSTNAME << " " <<  LASTNAME << endl;
        cout << HOURS_WORKED << "hours worked at $ " << HOURLY_RATE << "/ hour= $ " << GROSS << endl;
        TAX= FEDTAX(GROSS);
    
        cout << "Federal  Taxes= $ " << TAX << endl;
        NET= GROSS-TAX;
        TAX= STATETAX(GROSS);
    
        cout << "State Taxes = $ " << TAX << endl;
        NET= NET-TAX;
        TAX= LOCALTAX(GROSS);
    
        cout << "Local Taxes =$" << TAX <<  endl;
        NET= NET-TAX;
        cout << "Net Pay =$" << NET << endl;
    
        return 0;
    }
    
    double FEDTAX( double GROSS)
    {
        int CENTS, MARITALSTATUS;
    
        switch (MARITALSTATUS)
        {
        case '1':
            CENTS= 0.12*GROSS*100;
            return CENTS/100.0;
            break;
    
        case '0':
            CENTS= 0.18*GROSS*100;
            return CENTS/100.0;
            break;
        }
    
    }//End FEDTAX
    
    
    
    double STATETAX (double GROSS)
    {
        int CENTS;
        CENTS= 0.0307*GROSS*100;
        return CENTS/100.0;
    }// end STATETAX
    
    double LOCALTAX(double GROSS)
    {
        int CENTS;
        CENTS= 0.030*GROSS*100;
        return CENTS/100.0;
    }// end LOCALTAX
    Once you have proper indentation, you can generally dispense with the "end" comments.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your code certainly doesn't look intended on the pages here - unless your following the indentation style of "all over the place".

    What I mean is that the only way that one function can know the values of a caller's variables is by the caller passing them to the function - like you do with GROSS for your gross salary.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  2. Creating local variables in a program?
    By fl0at in forum C Programming
    Replies: 5
    Last Post: 01-04-2008, 07:34 PM
  3. [NEED HELP] I just can't solve this program problem!
    By frodonet in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 02:44 PM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. Program abort due to memory problem
    By cazil in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2002, 12:55 PM

Tags for this Thread