hi need help finding error. salary avg. is accidently = 0.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: hi need help finding error. salary avg. is accidently = 0.

  1. #1
    Join Date
    May 2007
    Posts
    5

    hi need help finding error. salary avg. is accidently = 0.

    hi i am doing a program using data files and functions for my assignment and i am having trouble. i debugged it and there are no errors when i build it. however, everything prints correctly when compiled except for the average salary. it prints to zero. can someone please help find my error? thanks!

    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;
    void Heading(void) ;
    void print_end (double avg_sal);
    float sal_count (float total_sal, float salary, float counter =0);
    double calc_sal (float, float, double& );
    void main (void)
    {
    //*****Declare Variables*****//
    ifstream fin;
    ofstream fout;
    int hours=0 ;
    float pay_rate=0, salary=0, total_sal=0, counter=0;
    double tax = 0, avg_sal;
    string dataf_name, first_name, last_name, name, dataf_out;
    Heading();
    sal_count (total_sal, salary, counter =0);
    calc_sal (counter, total_sal, avg_sal=0);
    print_end (avg_sal);


    //**********Input***********//
    cout<< "Enter input file name:";
    getline(cin,dataf_name);
    cout<< "Enter output file name:";
    getline(cin,dataf_out);
    //cin>>dataf_out;
    fin.open (dataf_name.c_str());
    if (fin.fail())
    {
    cout<< "Bad file name!";
    }
    else
    {
    fout.open("out.txt");
    cout<< left << fixed << setprecision(2);
    cout<< left << fixed<<showpoint;
    fout<< left << fixed << setprecision(2);
    fout<< left << fixed<<showpoint;
    cout<<endl;

    fout<<"Shafia Paglar Assignment #6"<<endl;
    fout<<"This program will calculate and report user's tax liabilities."<<endl;

    fout<<"======================================================================="<<endl;
    fout<<setw(15)<<"Name"<<setw(15)<<"Hours Worked"<<setw(15)<<"Pay Rate"<<setw(15)<<"Salary"<<setw(15)<<"Tax"<<endl;

    fout<<"======================================================================"<<endl;

    Heading();

    cout<<"======================================================================="<<endl;
    cout<<setw(15)<<"Name"<<setw(15)<<"Hours Worked"<<setw(15)<<"Pay Rate"<<setw(15)<<"Salary"<<setw(15)<<"Tax"<<endl;

    cout<<"======================================================================"<<endl;
    fin>>first_name;
    while (fin) // while (!fp.eof))
    {
    fin>>last_name;
    fin>>hours;
    fin>>pay_rate;
    salary = pay_rate * hours;
    if (salary < 3500)
    {
    tax = 0;
    } else if (salary < 8000)
    {
    tax = ((salary - 3500)*.06)+ 105;
    } else if (salary < 20000)
    {
    tax = ((salary - 8000)*.11)+ 375;
    } else if (salary < 34000)
    {
    tax = ((salary - 20000)*.17)+ 1695;
    } else if (salary < 52000)
    {
    tax = ((salary - 34000)*.24)+ 4075;
    } else if (salary > 52000)
    {
    tax = ((salary - 52000)*.32)+ 8395;
    }
    name=first_name+" "+last_name;


    if (hours < 0 && pay_rate>0)
    {

    fout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<" Invalid hours!!!" << endl;

    cout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<" Invalid hours!!!" << endl;
    }
    else if (pay_rate < 0 && hours > 0)
    {

    fout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<" Invalid pay rate!!!" << endl;

    cout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<" Invalid pay rate!!!" << endl;
    }
    else if (pay_rate>0 && hours > 0)
    {
    float sal_count (float total_sal, float salary, float counter =0);

    fout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<salary<<setw(15)<<tax <<endl;

    cout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<salary<<setw(15)<<tax <<endl;
    }





    fin>>first_name;//reset
    }//end of while
    if (counter > 0)
    {
    calc_sal (counter, total_sal, avg_sal);
    fout<<"\tAverage Gross Salary for the company is: $"<<avg_sal<<endl;

    print_end(avg_sal);
    }
    /**********output********/
    else
    {
    fout<<"no input"<<endl;
    cout<<"no input"<<endl;
    }
    }
    }

    void Heading(void)
    {
    cout<<"Shafia Paglar Assignment #6"<<endl;
    cout<<"This program will calculate and report user's tax liabilities."<<endl;
    }
    float sal_count (float total_sal, float salary, float counter =0)
    {
    total_sal = total_sal + salary;
    counter++;

    }
    double calc_sal (float counter, float total_sal, double &avg_sal)

    {

    avg_sal= total_sal/counter;
    return (avg_sal);
    }
    void print_end (double avg_sal)
    {

    cout<<"\tAverage Gross Salary for the company is: $"<<avg_sal<<endl;

    }

  2. #2
    Join Date
    Mar 2007
    Posts
    28
    Hi, lucky85. I'm a beginner when it comes to C++, but I think I can help. Now I am not sure what compiler you used for the code, but mine came up with a redefinition and return error. I use Visual studio 6.0. I believe the redefinition error is compiler dependent so I won't go into any detail on that, but it looks like your problem is that your not returning anything in your sal_count function:

    Code:
    float sal_count (float total_sal, float salary, float counter =0)
    {
    total_sal = total_sal + salary;
    counter++;
    
    }
    And the return value from this function will effect your answer in your average salary calculation:
    Code:
    double calc_sal (float counter, float total_sal, double &avg_sal)
    
    {
    
    avg_sal= total_sal/counter;
    return (avg_sal);
    }

    If everything else is working, then the only thing you should have to add is a return value for your float sal_count function and make sure to pass total_sal in by reference. If you don't, there will be no change to total_sal in the main part of the program. : (I have fixes labeled in red)

    Code:
    float sal_count (float &total_sal, float salary, float &counter =0)
    {
    total_sal = total_sal + salary;
    counter++;
    
    return (total_sal); 
    }
    Another thing to note:
    Although it is not a big deal when it comes to memory usage, I would change your counter to an integer number as it will never have a decimal. This way instead of 6 bytes being allocated, there will be 4 bytes allocated.

    Edit:
    Your counter also must be passed in by reference anywhere it is supposed to increase in order to change. If you still have problems feel free to ask me. :)
    Last edited by Luvanime; 05-07-2007 at 05:02 PM.

  3. #3
    Join Date
    May 2007
    Posts
    5
    thanks! thats what i thought too but when i make the change to it it still returns the average salary as 0. i dont know what else i can do?

  4. #4
    Join Date
    Mar 2007
    Posts
    28
    Don't forget to fix the prototype as well:
    Code:
    using namespace std;
    
    
    void Heading(void) ;
    void print_end (double avg_sal);
    
    float sal_count (float &total_sal, float salary, float &counter =0);
    double calc_sal (float, float, double& );
    
    
    void main (void)
    {
    Ok I think I see your biggest problem:
    Code:
    //*****Declare Variables*****//
    ifstream fin;
    ofstream fout;
    int hours=0 ;
    float pay_rate=0, salary=0, total_sal=0, counter=0;
    double tax = 0, avg_sal;
    string dataf_name, first_name, last_name, name, dataf_out;
    
    Heading();
    sal_count (total_sal, salary, counter =0);
    calc_sal (counter, total_sal, avg_sal=0);
    print_end (avg_sal);
    See the ones you have for your functions? These are not declarations. They are function calls. These actually call your functions and execute them. The declarations for your functions are actually your function prototypes you have before void main(void).

    Now I see you have all of these function calls after your file read in as well accept for your sal_count function call. You see, throughout your whole program, this function is being called only once and being called before any salary data has been calculated (using the data you read from the file). Therefore, what is being passed to your sal_count function is your salary data object initialized to 0. This will lead to 0 for you total_sal, and in turn lead to 0/1 (which is 0) in your avg_sal. As a result, your average salary will always be 0.

    How do we fix this problem? Well it is actually quite simple. First, copy your function call for sal_count. Next, delete all the function calls you have under Declare Variables. Lastly, paste the sal_count function call under your salary calculation:
    Code:
          salary = pay_rate * hours;
    and voila! Problem solved! :D

    Or it should be solved. If it still doesn't work, ask me again. I may still be over looking some errors. I apologize if I did, but your code is a little hard to read. ;)
    Last edited by Luvanime; 05-07-2007 at 07:21 PM.

  5. #5
    Join Date
    May 2007
    Posts
    5
    hi! now it says fatal error, entry point must be defined. any idea of what this may mean? im using vs 2005 so it may be allittle different. thanks

  6. #6
    Join Date
    Mar 2007
    Posts
    28
    That's odd. Can you post your modified code and the text file your reading from?
    I will try to modify and test it and see if I can get it to work.

    Or show me where the error points to.
    Last edited by Luvanime; 05-07-2007 at 11:32 PM.

  7. #7
    Join Date
    May 2007
    Posts
    5
    ok now i got the error to leave but its still reading 0 for avg sal. here is my revised code thanks

    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;
    void Heading(void) ;
    void print_end (double &avg_sal);
    float sal_count (float &total_sal, float salary, float counter =0);
    double calc_sal (float, float, double );
    void main (void)
    {
    //*****Declare Variables*****//
    ifstream fin;
    ofstream fout;
    int hours=0 ;
    float pay_rate=0, salary=0, total_sal=0, counter=0;
    double tax = 0, avg_sal;
    string dataf_name, first_name, last_name, name, dataf_out;

    //**********Input***********//
    cout<< "Enter input file name:";
    getline(cin,dataf_name);
    cout<< "Enter output file name:";
    getline(cin,dataf_out);
    //cin>>dataf_out;
    fin.open (dataf_name.c_str());
    if (fin.fail())
    {
    cout<< "Bad file name!";
    }
    else
    {
    fout.open("out.txt");
    cout<< left << fixed << setprecision(2);
    cout<< left << fixed<<showpoint;
    fout<< left << fixed << setprecision(2);
    fout<< left << fixed<<showpoint;
    cout<<endl;

    fout<<"Shafia Paglar Assignment #7<<endl;
    fout<<"This program will calculate and report user's tax liabilities."<<endl;

    fout<<"======================================================================="<<endl;
    fout<<setw(15)<<"Name"<<setw(15)<<"Hours Worked"<<setw(15)<<"Pay Rate"<<setw(15)<<"Salary"<<setw(15)<<"Tax"<<endl;

    fout<<"======================================================================"<<endl;

    Heading();

    cout<<"======================================================================="<<endl;
    cout<<setw(15)<<"Name"<<setw(15)<<"Hours Worked"<<setw(15)<<"Pay Rate"<<setw(15)<<"Salary"<<setw(15)<<"Tax"<<endl;

    cout<<"======================================================================"<<endl;
    fin>>first_name;
    while (fin) // while (!fp.eof))
    {
    fin>>last_name;
    fin>>hours;
    fin>>pay_rate;
    salary = pay_rate * hours;
    sal_count (&total_sal, salary, counter );

    if (salary < 3500)
    {
    tax = 0;
    } else if (salary < 8000)
    {
    tax = ((salary - 3500)*.06)+ 105;
    } else if (salary < 20000)
    {
    tax = ((salary - 8000)*.11)+ 375;
    } else if (salary < 34000)
    {
    tax = ((salary - 20000)*.17)+ 1695;
    } else if (salary < 52000)
    {
    tax = ((salary - 34000)*.24)+ 4075;
    } else if (salary > 52000)
    {
    tax = ((salary - 52000)*.32)+ 8395;
    }
    name=first_name+" "+last_name;


    if (hours < 0 && pay_rate>0)
    {

    fout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<" Invalid hours!!!" << endl;

    cout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<" Invalid hours!!!" << endl;
    }
    else if (pay_rate < 0 && hours > 0)
    {

    fout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<" Invalid pay rate!!!" << endl;

    cout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<" Invalid pay rate!!!" << endl;
    }
    else if (pay_rate>0 && hours > 0)
    {


    fout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<salary<<setw(15)<<tax <<endl;

    cout<<setw(15)<<name<<setw(15)<<hours<<setw(15)<<pay_rate<<setw(15)<<salary<<setw(15)<<tax <<endl;
    }





    fin>>first_name;//reset
    }//end of while
    if (counter > 0)
    {

    calc_sal (counter, total_sal, avg_sal);
    fout<<"\tAverage Gross Salary for the company is: $"<<avg_sal<<endl;

    print_end(avg_sal);
    }
    /**********output********/
    else
    {
    fout<<"no input"<<endl;
    cout<<"no input"<<endl;
    }
    }
    }

    void Heading(void)
    {
    cout<<"Shafia Paglar Assignment #7<<endl;
    cout<<"This program will calculate and report user's tax liabilities."<<endl;
    }
    float sal_count (float &total_sal, float salary, float counter =0)
    {
    total_sal = total_sal + salary;
    counter++;
    return (total_sal);

    }
    double calc_sal (float counter, float total_sal, double &avg_sal)

    {

    avg_sal= total_sal/counter;
    return (avg_sal);
    }
    void print_end (double avg_sal)
    {

    cout<<"\tAverage Gross Salary for the company is: $"<<avg_sal<<endl;

    }

  8. #8
    Join Date
    Mar 2007
    Posts
    28
    One thing that I see right away is that your missing your ampersand after the double in your calc_sal prototype ( the one before main ). You must have that or there will be no change to your avg_sal.

    Let me compile it really quickly and see if I can't spot anything else.

    The only other thing I can think of to try is to assign the calc_sal to the avg_sal in your if statement towards the end of main:

    avg_sal = calc_sal (float counter, float total_sal, double &avg_sal);



    I'm getting quite a few errors on my compiler and I'm not sure where some of them are coming from. I'm afraid I don't have enough time to really go through your code. I do apologize, but I am having a bit of trouble reading it. Perhaps one of the moderators can help.


    Here are some tips for coding though that I think may make it easier for you to catch and correct errors (It helps me keep my code in check):

    (1) When dealing with files, read all the input first before writing output.

    (2) Check to make sure everything read in correctly by looping them in cout statements. (This would require your variables be arrays.)

    (3) After each addition of a new function, test that function by writing a snippet of code that prints its results to the screen. This will limit the amount of errors when the code is finished.

    (4) Close input files when they are no longer needed.

    (5) The last thing I would do is write all the necessary data to my output file.

    (6) Then close the file.

    (7) At the end, compile, check, and there will be little error.

    Plus adding a this function works comment to each function you test will help you keep track of ones that you have and have not tested.

    By no means scratch your code. I'm sure its gonna be something that I'm not catching, but this may help you in the future. :)

    Heres a file lab I coded a while ago (I hope this helps. I have another lab I could show you that deals with files, but it's a lot more complicated. If you want to see it though, I'll post it)):
    Code:
    //********************************************************************
    //
    // Lab #6 - Word Count Program
    //
    // A program to count the number of words in a text file
    //
    // Date Created: 03-15-07
    //
    // Date Modified: 03-16-07
    //
    //********************************************************************
    
    #include <cstdlib>
    #include <fstream>
    #include <iostream>
    #include <string>
    
    #define cls system ("cls")
    #define pause system ("pause")
    
    using namespace std;
    
    // 
    // function prototypes
    //
    string getFileName ();
    
    int wordCount (ifstream &txt);
    
    
    // start of main
    int main ()
    {
    	int totalWords; 
    
    
    	cout << "Word Count Program launched..." << "\n\n";
    	
    	pause;
    
    	cls;
    
    
    	ifstream txtFile; // handle for the text file
    
    	string fileName = getFileName (); // assigned function call to variable
    	
    
    	txtFile.open (fileName.c_str ()); // opening file
    
    
    	if (!txtFile.is_open ()) // tests if file opening failed
    	{
    		cerr << "Could not open file: " << fileName << "\n\n";
    
    		exit (1);// exits program
    	}
    
    	cls;
    
    	totalWords = wordCount ( txtFile ); // assigned function call to variable
    
    
    
    	//
    	// print fileName and totalWords of text file to screen
    	//
    	cout << "FQN of File:" << '\n' << fileName << "\n\n";
    
    	cout << "Word Total of Text File = " << totalWords << "\n\n";
    	
    
    	txtFile.close(); // closes the text file
    
    	return 0;
    }// end of main 
    
    
    //********************************************************************
    //
    // Function Name: getFileName
    //
    // Purpose: To get the FQN (Fully Qualified Name) of a text file 
    //         from the user
    //
    // Input Parameters: none 
    //
    // Output Parameters: none
    //
    // Return Value: fN is assigned as the value for fileName in main
    //
    //********************************************************************
    
    string getFileName ()
    {
    	string fN;
    
    	cout << "Enter the fully qualified name of the text file." <<
    		   " (i.e. must include entire path" << "\n\n" << 
    		   "of file): "; 
    
    		   cin >> ws;
    
    	getline (cin, fN);
    
    	cout << "\n\n";
    
    	return (fN);
    }// getFileName
    
    
    //********************************************************************
    //
    // Function Name: wordCount
    //
    // Purpose: To count the number of words in a text file
    //         (note: "word" refers to characters separated by 
    //          whitespace)
    //
    // Input Parameters: txt- a file handle pointing (pass by reference) 
    //                  to the (input) text file
    //
    // Output Parameters: none
    //
    // Return Value: wC is assigned as the value for totalWords in main
    //
    //********************************************************************
    
    int wordCount (ifstream &txt)
    {
    	int wC = 0;
    
    	char ch,
    		 word = 'O';
    
    	while ( txt.get(ch) )
    	{
    		if (ch == ' ' || ch == '\n' || ch == '\t')
    		{
    			word = 'O';
    		}
    		else if (word == 'O')
    		{
    			 wC++;
    			 
    			 word = 'I';
    		}
    	}// end of loop
    
    	return (wC);
    }// wordCount
    Last edited by Luvanime; 05-08-2007 at 01:04 AM.

  9. #9
    Join Date
    May 2007
    Location
    Dell Rapids, SD
    Posts
    4
    Your counter is not being incremented meaning it is always going to be 0. Try incrementing the counter in the while loop instead of in the sal_count function.

    Code:
      
    while (fin.good()) {
    	fin>>first_name;
    	fin>>last_name;			
    	fin>>hours;
    	if (hours <= 0) {
    		cout << "Invalid hours\n";
    		return;
    	}
    	fin >> pay_rate;
    	if (pay_rate <= 0) {
    		cout << "Invalid pay rate\n";
    		return;
    	}
    	counter++;
    	
    	//... code omitted
    }
    You should always verify your inputs as you bring them in, not when ready to output. Then when you do your calculations, you always know you have valid values.

    Hope this helps.

  10. #10
    Join Date
    May 2007
    Posts
    5
    thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center