need help the program is showing zero in output section need help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: need help the program is showing zero in output section need help

  1. #1
    Join Date
    Nov 2013
    Posts
    49

    Question need help the program is showing zero in output section need help

    need help the program is showing zero in output section need help cant figure how what im doing wrong
    Code:
    #include <iostream>
    #include <cstdio>
    #include <fstream>
    #include <string>
    #include <iomanip>
    using namespace std;
    const int SZ = 55;
    void tellUser();
    int readData( string [], string [], string [], double [], double []);
    void bubbleSort( string [], string [], string [], double [], double [], int);
    void findregularhours(double[], int[], int);
    void findregularpay(int[], double[], double[], int);
    void findovertimehours(double[], double[], int);
    void findovertimepay(double[], double[], double[], int);
    void findgrosspay(double[], double[], double[], int);
    int outputScr(double[], double[], int[]);
    int main()    //begin main
    {
          string firstname[SZ], lastname[SZ];                       
          string empids[SZ];                                        
          double  hoursworked[SZ], overtimehours[SZ];               
          int regularhours[SZ];                                    
          double hourlyrate[SZ], regularpay[SZ], overtimepay[SZ], grosspay[SZ]; 
          int n, numemp;                                                    
          ofstream outputFile;                                     
          bool swapmade = false;                                    
          bool screenonly = false;                               
          tellUser();                                               
          //readData(firstn, lastn, empID, hrs, rate);             
          bubbleSort(firstname, lastname, empids, hoursworked, hourlyrate, numemp);   
          findregularhours(hoursworked, regularhours, n);           
          findovertimehours(hoursworked, overtimehours, n);        
          findovertimepay(overtimehours, hourlyrate, overtimepay, n);  
          findgrosspay(regularpay, overtimepay, grosspay, n);       
          outputScr(hoursworked, hourlyrate, regularhours);         
          return 0;
    }  //end main
    void tellUser()       // tellUser Function
    {
       cout <<"\nThis program reads a file called employees.txt,\n";
       cout <<"and it calculates the regular pay, overtime pay\n";
       cout <<"and grosspay and total for grosspay for each employee and\n";
       cout <<"sorts the from last name and output is written to the screen. \n\n"; 
    }  //end tellUser Function
    int readData(string firstn[], string lastn[], string empID[], double hrs[], double rate[])  
    {  
         int numemp;
         ifstream inputFile;
         int i = 0;  
         // open file and read inputs from employees.txt
         inputFile.open("employees2.txt");              
         if (inputFile.fail())                            
         { 
            cout << "Error opening file employees.txt \n\n";
            cout << "end of program\n\n";
         }
         else
         {
            while ((inputFile >> hrs[i]) && (i < SZ))    
            {
               inputFile >> rate[i];
               inputFile >> empID[i];
               inputFile >> firstn[i];
               inputFile >> lastn[i];
               i++;
            } //end while
            cout << "There were " << i << " employess\n\n";
             numemp = i;
            inputFile.close();
            //************* close input file ****************//
         }
    } //end of readData Function
    void bubbleSort(string firstn[ ],string lastn[ ],string empID[ ],double hrs[ ],double rate[ ],int numemp)     
    {
           //string firstn[SZ], last[SZ], empID[SZ];
           //double hrs[SZ], rate[SZ];
           int regularhours[SZ];                            
           int  i, lastpos;                                 
           bool screenonly = false;                          
           bool swapmade = false;                            
           ofstream outputFile;
           lastpos = numemp;
           {
                lastpos--;
                swapmade = false;
                for ( i = 0; i < lastpos; i++)
                {
                    if (lastn[i] > lastn[i+1])   
                    { // swap all items here
                        swap(firstn[i], firstn[i+1]);
                        swap(lastn[i], lastn[i+1]);
                        swap(empID[i], empID[i+1]);
                        swap(hrs[i], hrs[i+1]);
                        swap(rate[i],rate[i+1]);
                        //swap(findregularpay[i], findregularpay[i+1]);
                        //swap(findovertimepay[i], findovertimepay[i+1]);
                        //swap(findgrosspay[i], findgrosspay[i+1]);
                        swapmade = true;
                    }
                } // end of for loop
           } while(swapmade); // end of do loop
    } //end of bubbleSort Function
    void findregularhours(double hoursworked[], int regularhours[], int n)
    {
       for(int i=0; i<n; i++)
       { 
           if(hoursworked[i]>40)    
                regularhours[i]=40;  
           else  
                regularhours[i]=hoursworked[i]; 
       }  //end for loop  
    }  //regularhours()
    void findregularpay( int regularhours[], double regularpay [], double hourlyrate[], int n)
    {
       for(int i=0; i<n; i++)
       {
          regularpay[i] = regularhours[i]*hourlyrate[i];
       }  //end for loop
    }   //regularpay
    void findovertimehours(double hoursworked[], double overtimehours[], int n)
    {
       for(int i=0; i<n; i++)
       {
           if(hoursworked[i]>40) 
                overtimehours[i]=hoursworked[i]-40; 
           else 
                overtimehours[i]=0;  
       }  //end for loop
    } // findsovertimehours
    void findovertimepay(double overtimehours[], double hourlyrate[], double overtimepay[], int n)
    {
        for(int i=0; i<n; i++)
        {
             overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;  
        } //end for loop
    } // findovertimepay
    
    void findgrosspay(double regularpay[], double overtimepay[], double grosspay[], int n)
    {
       for(int i=0; i<n; i++)
       {
           grosspay[i]=regularpay[i]+overtimepay[i];  
       }  // end for loop
    } //findgrosspay
    
    int outputScr(double hoursworked[], double hourlyrate[], int regularhours[])
    { 
          string firstn[SZ], lastn[SZ];
          string empID[SZ];
          double hrs[SZ];
          double rate[SZ]; 
          double grosspay[SZ], regularpay[SZ], overtimepay[SZ];
          
          int  i, numemp, lastpos;
          bool screenonly = false;
          ofstream outputFile; 
          cout << "Payroll being written to file payroll.txt\n\n"; 
          outputFile.open("payroll.txt"); // output file
          numemp = readData(firstn, lastn, empID, hrs, rate);
          if (outputFile.fail())
          {
               screenonly = true;
               cout <<" output file did not open\n";
               cout <<" output file will only be sent to the screen\n";
          }
          cout <<"First          Last       Employee     Hours        Rate      Regular    Overtime    Gross\n";  
          cout <<"Name           Name        Number      Worked      of Pay       Pay        Pay        Pay\n";
          cout <<"===========================================================================================\n";
          outputFile <<"First          Last        Employee     Hours       Rate      Regular    Overtime    Gross\n";
          outputFile <<"Name           Name         Number      Worked     of Pay      Pay        Pay        Pay\n";
          outputFile <<"=========================================================================================\n";
          for (i = 0; i < numemp; i++)  //screen only
          {
              cout << setw(15) << fixed << left <<  firstn[i] << setw(12) << lastn[i];
              cout << setw(11) << empID[i] << " " << setw(12) << fixed << setprecision(2) << hrs[i] << " ";
              cout << setw(10) << rate[i] << " " << setw(10) << fixed << setprecision(2) << regularpay[i] << " ";
              cout << setw(11)  << overtimepay[i] << " " << setw(12) << fixed << setprecision(2) << grosspay[i] << " " << endl; 
          
              if (!screenonly)  
              {
                    outputFile << setw(13) << fixed << left << firstn[i] << " ";
                    outputFile << setw(9) << fixed << lastn[i] << " ";
                    outputFile << setw(9) << fixed << right << empID[i] << " "; 
                    outputFile << setw(9) << fixed << setprecision(2) << hrs[i] << " ";
                    outputFile << setw(12) << fixed << setprecision(2) << rate[i] << " ";
                    outputFile << setw(10) << fixed << setprecision(2) << regularpay[i] << " ";
                    outputFile << setw(11) << fixed << setprecision(2) << overtimepay[i] << " ";
                    outputFile << setw(10) << fixed << setprecision(2) << grosspay[i] << " " << endl;
              }
          }
    
         cout <<"============================================================================================\n";
         cout <<"\t\t\tTotal Gross Pay\t\t\t\t\t             "<< fixed << setprecision(2) << findgrosspay << " \n";
         outputFile <<"============================================================================================\n";
         outputFile <<"\t\t\tTotal Gross Pay\t\t\t\t\t             "<< fixed << setprecision(2) << findgrosspay << " \n";
          if (!screenonly)
          { 
             outputFile.close();    //outputFile close.
             cout << "input file closed\n\n";}
    } // end outputScr Function
    the output:
    Code:
    First          Last       Employee     Hours         Rate      Regular    Overtime    Gross
    Name           Name        Number      Worked      of Pay       Pay        Pay        Pay
    ===========================================================================================
    Jane           Adams       A1234       40.00        10.00      0.00       0.00        0.00
    Mary           Lincoln     L8765       50.00        10.00      0.00       0.00        0.00
    Martha         Washington  W7654       25.50        10.85      0.00       0.00        0.00
    John           Adams       A9876       52.00        15.75      0.00       0.00        0.00
    George         Washington  W1235       45.00        25.00      0.00       0.00        0.00
    Abraham        Lincoln     L9087       40.25        55.00      0.00       0.00        0.00
    William        Tell        T9876       30.00        9.75       0.00       0.00        0.00

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: need help the program is showing zero in output section need help

    You should use the debugger. That's what it's for. Why is this line in your outputScr function.

    double grosspay[SZ], regularpay[SZ], overtimepay[SZ];

  3. #3
    Join Date
    Nov 2013
    Posts
    49

    Re: need help the program is showing zero in output section need help

    if i take out this line double grosspay[SZ], regularpay[SZ], overtimepay[SZ]; the program runs and displays all 1

  4. #4
    Join Date
    Nov 2013
    Posts
    49

    Re: need help the program is showing zero in output section need help

    can you help i tried every method to calculate but it wont how, or can you fix the regular pay and i can fiz the overtime pay and grosspay with your help in regular pay

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: need help the program is showing zero in output section need help

    Quote Originally Posted by Krrish1 View Post
    if i take out this line double grosspay[SZ], regularpay[SZ], overtimepay[SZ]; the program runs and displays all 1
    And now it displays all zeros. What you should do is use the arrays that you populated with data.

  6. #6
    Join Date
    Nov 2013
    Posts
    49

    Re: need help the program is showing zero in output section need help

    so it should be regularpay = rate[SZ] * hoursworked[SZ]

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: need help the program is showing zero in output section need help

    Quote Originally Posted by Krrish1 View Post
    so it should be regularpay = rate[SZ] * hoursworked[SZ]
    This is the third thread you've started asking the same question, and yet you're still not following the advice you got the first two times. Stop creating duplicate threads, and pay attention to the advice you've already been given.

    forums.codeguru.com/showthread.php?540953-need-help-with-my-source-code-but-it-wont-complie-and-giving-me-errors
    Last edited by VictorN; November 27th, 2013 at 04:48 AM. Reason: Link was edited

  8. #8
    Join Date
    Nov 2013
    Posts
    49

    Re: need help the program is showing zero in output section need help

    i just need help with regularpay

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: need help the program is showing zero in output section need help

    Quote Originally Posted by Krrish1 View Post
    i just need help with regularpay
    You need to understand variable scope and passing variables to functions, just as was explained repeatedly in your first thread.

  10. #10
    Join Date
    Nov 2013
    Posts
    49

    Re: need help the program is showing zero in output section need help

    i cant use variable scope in the program

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: need help the program is showing zero in output section need help

    Quote Originally Posted by Krrish1 View Post
    i cant use variable scope in the program
    It isn't something you use, it's a concept in how the language works. I feel like I've told you to read up on it a dozen times already. Your lack of understanding of it has been the central theme in all three of your threads.

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    Re: need help the program is showing zero in output section need help

    Quote Originally Posted by Krrish1 View Post
    i just need help with regularpay
    No, you just want us to fix your program for you so you don't have to take the trouble to learn how to write properly c++ programs. As GCDEF has stated in post #7, you have not followed or understood the help and advice provided in your previous posts.

    Short of providing you with the correct code, which we are not going to do, there is really not much additional help and guidance we can give until you understand what has already been said.

    Your first problem is that you are reading the data using readData() in outputScr() rather than in main(). You're done this because you aren't passing parameters correctly between functions as we have said previously. Forget about sorting, calculations etc and have your main program as

    tell(...)
    readData(....)
    outputScr(...)

    when you have got this simple code to work, then add code for the calculations, sorting etc.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #13
    Join Date
    Nov 2013
    Posts
    49

    Question Re: need help the program is showing zero in output section need help

    Now I got it instead of the read data function to to read in Main, I'm directly reading it in output screen. Now I got that's y it could read from same function. Thanks for the help. I'll correct my code and post it

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center