need help with my source code but it wont complie and giving me errors
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: need help with my source code but it wont complie and giving me errors

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    49

    Question need help with my source code but it wont complie and giving me errors

    i tring to write a employee payroll code with functions and read data from the txt. file and also need help with bubble sort that sorts and displays starting from employees last name and calculations.

    //txt file//
    Hours Pay rate empID first name last name
    40.0 10.00 A1234 Jane Adams
    50.0 10.00 L8765 Mary Lincoln
    25.5 10.85 W7654 Martha Washington
    52.0 15.75 A9876 John Adams
    45.0 25.00 W1235 George Washington
    40.25 55.00 L9087 Abraham Lincoln
    30.0 9.75 T9876 William Tell
    42.5 12.50 M7654 Missy Muffett
    30.0 10.00 P8765 Peter Piper

    and here is my code
    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 []);
    int bubbleSort();
    int outputScr();
    int regular();
    int overtime();
    int grossPay();
    int main()
    {
          string firstname[SZ], lastname[SZ];
          string empids[SZ];
          double hours[SZ];
          double rates[SZ];
          ofstream outputFile;
          bool swapmade = false;
          bool screenonly = false;
          char yesno;
          int  i, numemp;   
          numemp = readData(firstname, lastname, empids, hours, rates);
          for (i = 0; i < numemp; i++)
          {
              cout << firstname[i] << lastname[i] << empids[i] <<  hours[i]
                   << rates[i] << endl; 
          }
          bubbleSort();
          outputScr();
          return 0;
    }  //end main
    /* bubbleSort()
     * sorts employees with theor last name snd displays in 
     * screen and txt file
     */
    int bubbleSort()
    {
           cout << "sorting golfer\n";
           lastpos = numemp;
           do
           {
             lastpos--;
             swapmade = false;
             for ( i = 0; i < lastpos; i++)
             {
                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]);
                swapmade - true;
             }
           } while(swapmade);
    }
    /* outputScr()
     * displays the employee payroll function to the screen and txt file
     */
    int outputScr()
    { 
          cout <<"First   Last   Employee   Hours    Rate   Regular  Overtime   Gross\n";
          cout <<"Name    Name   Number     Worked  of Pay    Pay     Pay        Pay\n";
          cout <<"===================================================================\n";
                    
          for ( i=0; i < numemp; i++)
          {
              cout<< setw(7) <<  firstn[i] << setw(13) << lastn[i];
              cout<< setw(12) << empID[i] << " " << setw(10) << hrs[i] << " ";
              cout<< setw(11) << rate[i] << " " << setw(11) << regular <<  " ";
              cout<< setw(11) << overtime << " " << setw(11) << grossPay << " \n";
              if (!screenonly)
              {
                  outputFile << setw(7) << left << firstn[i] << " ";
                  outputFile << setw(7) << left << lastn[i] << " ";
                  outputFile << setw(4) << fixed << right << empID << " "; 
                  outputFile << setw(4) << fixed << right << hrs[i] << " ";
                  outputFile << setw(4) << fixed << right << rate[i] << " \n";
              }
          }
          if (!screenonly)
          { 
             outputFile.close(); cout << "Output file closed\n\n";}
    } // end else
    /* rellUser(0
     * tells about the program to the user
     */
    void tellUser()
    {
       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.\n";
       cout<<"output is written to the screen. \n\n"; //tell user what program does
    }
    /* regular()
     * calculates employees regular pay
     */
    int regular()
    {
      double  hours, rate;
      if(hours <= 40)
          grossPay = hours * rate;
    }
    /* overtime()
     * calculates employees overtime pay
     */
    int  overtime()
    {
      double hours, rate;
     // const int emp_hrs = 40;
     // const double emp_over = 1.5;
      if (hours >= 40) 
         overtime = (hours - 40) * rate * 1.5;
    }
    /* grossPay()
     * calculates employees regular + overtime pay
     */
    int  grossPay(double &)
    {
      double  hours, rate; 
      if (hours <= 40)
         grossPay = (hours * rate);
      else
         grossPay = ((hours - 40) * rate * 1.5);
    }
    /**************************************************
     * readData
     * firstname , lastname, empID, hours, rate of pay
     */
    int readData(string firstn[], string lastn[], string empID[], double hrs[], double rate[])
    { 
         int numemp;
         ifstream inputFile;
         int i = 0;
         tellUser();  
         // open file and read inputs from employees.txt
         inputFile.open("employees.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 ****************//
            cout << "input file closed\n\n";
            cout << "Payroll being written to file payroll.txt\n"; //output function
            outputFile.open("payroll.txt"); // output file
            if (outputFile.fail())
            {
               screenonly = true;
               cout <<" output file did not open\n";
               cout <<" output file will only be sent to the screen\n";
            }
        }
    }
    here are the errors the are giving
    Code:
    payroll.cpp: In function ‘int bubbleSort()’:
    payroll.cpp:52:8: error: ‘lastpos’ was not declared in this scope
    payroll.cpp:52:18: error: ‘numemp’ was not declared in this scope
    payroll.cpp:56:10: error: ‘swapmade’ was not declared in this scope
    payroll.cpp:57:16: error: ‘i’ was not declared in this scope
    payroll.cpp:59:18: error: ‘firstn’ was not declared in this scope
    payroll.cpp:60:18: error: ‘lastn’ was not declared in this scope
    payroll.cpp:61:18: error: ‘empID’ was not declared in this scope
    payroll.cpp:62:18: error: ‘hrs’ was not declared in this scope
    payroll.cpp:63:18: error: ‘rate’ was not declared in this scope
    payroll.cpp:66:16: error: ‘swapmade’ was not declared in this scope
    payroll.cpp: In function ‘int outputScr()’:
    payroll.cpp:77:13: error: ‘i’ was not declared in this scope
    payroll.cpp:77:22: error: ‘numemp’ was not declared in this scope
    payroll.cpp:79:30: error: ‘firstn’ was not declared in this scope
    payroll.cpp:79:55: error: ‘lastn’ was not declared in this scope
    payroll.cpp:80:30: error: ‘empID’ was not declared in this scope
    payroll.cpp:80:61: error: ‘hrs’ was not declared in this scope
    payroll.cpp:81:30: error: ‘rate’ was not declared in this scope
    payroll.cpp:83:16: error: ‘screenonly’ was not declared in this scope
    payroll.cpp:85:15: error: ‘outputFile’ was not declared in this scope
    payroll.cpp:92:12: error: ‘screenonly’ was not declared in this scope
    payroll.cpp:94:10: error: ‘outputFile’ was not declared in this scope
    payroll.cpp: In function ‘int regular()’:
    payroll.cpp:114:26: error: assignment of function ‘int grossPay()’
    payroll.cpp:114:26: error: cannot convert ‘double’ to ‘int()’ in assignment
    payroll.cpp: In function ‘int overtime()’:
    payroll.cpp:125:39: error: assignment of function ‘int overtime()’
    payroll.cpp:125:39: error: cannot convert ‘double’ to ‘int()’ in assignment
    payroll.cpp: In function ‘int grossPay(double&)’:
    payroll.cpp:134:30: error: overloaded function with no contextual type information
    payroll.cpp:136:43: error: overloaded function with no contextual type information
    payroll.cpp: In function ‘int readData(std::string*, std::string*, std::string*, double*, double*)’:
    payroll.cpp:171:9: error: ‘outputFile’ was not declared in this scope
    payroll.cpp:174:12: error: ‘screenonly’ was not declared in this scope

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,276

    Re: need help with my source code but it wont complie and giving me errors

    Well, error "'something' was not declared in this scope" is very clear: all variables must be declared before using them, so do it!
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2013
    Posts
    49

    Re: need help with my source code but it wont complie and giving me errors

    Can you tell which one because I check everything and it seems to be in order and I'm just a beginner in C++

  4. #4
    Join Date
    Nov 2013
    Posts
    49

    Re: need help with my source code but it wont complie and giving me errors

    So I have do declare the same variable both places: one in int main function and the another one at each function where the variable is used

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

    Re: need help with my source code but it wont complie and giving me errors

    Quote Originally Posted by Krrish1 View Post
    So I have do declare the same variable both places: one in int main function and the another one at each function where the variable is used
    Yes/no/maybe, depends what you're trying to do. In the case of the first error at least, lastpos isn't declared anywhere. Keep in mind that variables declared in different scopes are different variables, even if they have the same name. I'd suggest reading up on scope and how it works.

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,265

    Re: need help with my source code but it wont complie and giving me errors

    Quote Originally Posted by Krrish1 View Post
    So I have do declare the same variable both places: one in int main function and the another one at each function where the variable is used
    A variable must be within scope before it can be used. See
    http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx

    Simplistically, a variable is within scope in the block of code following the line in which it is defined in the block. As functions (including main) are separate 'blocks' a variable defined in one function is not accessible to another function. If variable var1 is defined in main() and also defined in func1(), the variable var1 in main() is a different variable to var1 in func1(). An exception to this are global variables which are usually defined at the start of a program outside of a block and are available to all functions.
    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.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,276

    Re: need help with my source code but it wont complie and giving me errors

    Yes, variables declared locally withing some function cannot be visible from outside this function.
    Read about Scope in MSDN.
    Victor Nijegorodov

  8. #8
    Join Date
    Nov 2013
    Posts
    49

    Question Re: need help with my source code but it wont complie and giving me errors

    here is the code i have corrected and updated:
    BUT also need help with bubble sort that sorts and diaplsys starting from emoloyees last name and calculations.

    //you can get the text file from above post


    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 []);
    int bubbleSort();
    int outputScr();
    int regular();
    int overtime();
    int grossPay();
    int main()
    {
          string firstname[SZ], lastname[SZ];
          string empids[SZ];
          double hours[SZ];
          double rates[SZ];
          ofstream outputFile;
          bool swapmade = false;
          bool screenonly = false;
          char yesno;
          int  i, numemp, lastpos;   
          numemp = readData(firstname, lastname, empids, hours, rates);
          for (i = 0; i < numemp; i++)
          {
              cout << firstname[i] << lastname[i] << empids[i] <<  hours[i]
                   << rates[i] << endl; 
          }
          bubbleSort();
          outputScr();
          return 0;
    }  //end main
    /* bubbleSort()
     * sorts employees with theor last name snd displays in 
     * screen and txt file
     */
    int bubbleSort()
    {
           string firstn[SZ], lastn[SZ];
           string empID[SZ];
           double hrs[SZ];
           double rate[SZ];
           int  i, numemp, lastpos;
           bool screenonly = false;
           ofstream outputFile;
           bool swapmade = false;
           cout << "sorting golfer\n";
           lastpos = numemp;
           do
           {
             lastpos--;
             swapmade = false;
             for ( i = 0; i < lastpos; i++)
             {
                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]);
                swapmade - true;
             }
           } while(swapmade);
    }
    /* outputScr()
     * displays the employee payroll function to the screen and txt file
     */
    int outputScr()
    {  
          string firstn[SZ], lastn[SZ];
          string empID[SZ];
          double hrs[SZ];
          double rate[SZ];
          int  i, numemp, lastpos;
          bool screenonly = false;
          ofstream outputFile; 
          cout << "Payroll being written to file payroll.txt\n"; //output function
          outputFile.open("payroll.txt"); // output file
          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";
                    
          for ( i=0; i < numemp; i++)
          {
              cout<< setw(7) <<  firstn[i] << setw(13) << lastn[i];
              cout<< setw(12) << empID[i] << " " << setw(10) << hrs[i] << " ";
              cout<< setw(11) << rate[i] << " " << setw(11) << regular <<  " ";
              cout<< setw(11) << overtime << " " << setw(11) << grossPay << " \n";
              if (!screenonly)
              {
                  outputFile << setw(7) << left << firstn[i] << " ";
                  outputFile << setw(7) << left << lastn[i] << " ";
                  outputFile << setw(4) << fixed << right << empID << " "; 
                  outputFile << setw(4) << fixed << right << hrs[i] << " ";
                  outputFile << setw(4) << fixed << right << rate[i] << " \n";
              }
          }
          if (!screenonly)
          { 
             outputFile.close(); cout << "Output file closed\n\n";}
    } // end else
    /* rellUser(0
     * tells about the program to the user
     */
    void tellUser()
    {
       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.\n";
       cout<<"output is written to the screen. \n\n"; //tell user what program does
    }
    /* regular()
     * calculates employees regular pay
     */
    int regular()
    {
      double  hours, rate;
      if(hours <= 40)
          grossPay = hours * rate;
    }
    /* overtime()
     * calculates employees overtime pay
     */
    int  overtime()
    {
      double hours, rate;
     // const int emp_hrs = 40;
     // const double emp_over = 1.5;
      if (hours >= 40) 
         overtime = (hours - 40) * rate * 1.5;
    }
    /* grossPay()
     * calculates employees regular + overtime pay
     */
    int  grossPay(double &)
    {
      double  hours, rate; 
      if (hours <= 40)
         grossPay = (hours * rate);
      else
         grossPay = ((hours - 40) * rate * 1.5);
    }
    /**************************************************
     * readData
     * firstname , lastname, empID, hours, rate of pay
     */
    int readData(string firstn[], string lastn[], string empID[], double hrs[], double rate[])
    { 
         int numemp;
         ifstream inputFile;
         int i = 0;
         tellUser();  
         // open file and read inputs from employees.txt
         inputFile.open("employees.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 ****************//
            cout << "input file closed\n\n";
        }
    }
    NOW THE THE ERROR IS:
    Code:
    payroll.cpp: In function ‘int regular()’:
    payroll.cpp:137:26: error: assignment of function ‘int grossPay()’
    payroll.cpp:137:26: error: cannot convert ‘double’ to ‘int()’ in assignment
    payroll.cpp: In function ‘int overtime()’:
    payroll.cpp:148:39: error: assignment of function ‘int overtime()’
    payroll.cpp:148:39: error: cannot convert ‘double’ to ‘int()’ in assignment
    payroll.cpp: In function ‘int grossPay(double&)’:
    payroll.cpp:157:30: error: overloaded function with no contextual type information
    payroll.cpp:159:43: error: overloaded function with no contextual type information

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,276

    Re: need help with my source code but it wont complie and giving me errors

    Quote Originally Posted by Krrish1 View Post
    [B]here is the code i have corrected and updated:
    Code:
    /* regular()
     * calculates employees regular pay
     */
    int regular()
    {
      double  hours, rate;
      if(hours <= 40)
          grossPay = hours * rate;
    }
    /* overtime()
     * calculates employees overtime pay
     */
    int  overtime()
    {
      double hours, rate;
     // const int emp_hrs = 40;
     // const double emp_over = 1.5;
      if (hours >= 40) 
         overtime = (hours - 40) * rate * 1.5;
    }
    /* grossPay()
     * calculates employees regular + overtime pay
     */
    int  grossPay(double &)
    {
      double  hours, rate; 
      if (hours <= 40)
         grossPay = (hours * rate);
      else
         grossPay = ((hours - 40) * rate * 1.5);
    }
    What could this assignemt in int regular() function mean:
    Code:
         grossPay = hours * rate;

    The same question - about
    Code:
        overtime = (hours - 40) * rate * 1.5;
    in int overtime () function and
    Code:
         grossPay = ((hours - 40) * rate * 1.5);
    in int grossPay(double &) function.
    Where is the double & parameter you were going to pass in grossPay() function?
    Last edited by VictorN; November 12th, 2013 at 12:41 PM. Reason: typos
    Victor Nijegorodov

  10. #10
    Join Date
    Nov 2013
    Posts
    49

    Question Re: need help with my source code but it wont complie and giving me errors

    Could you correct and show the correct format of the int regular function code
    So I can fix the grossPay and overtime

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,276

    Re: need help with my source code but it wont complie and giving me errors

    Quote Originally Posted by Krrish1 View Post
    Could you correct and show the correct format of the int regular function code
    So I can fix the grossPay and overtime
    your function
    Code:
    int regular()
    {
      double  hours, rate;
      if(hours <= 40)
          grossPay = hours * rate;
    }
    does not make any sense, so I cannot "correct" it.
    You declare two double variables but do not initialize them. Therefore neither any comparison with 40 nor the multiplication the two uninitialized variables makes sense.
    Besides, you do not return any value from this function.

    I guess you should begin to learn the basics of C++: how to implement and use functions in it.
    Victor Nijegorodov

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

    Re: need help with my source code but it wont complie and giving me errors

    I get different errors, but these lines aren't valid in C++.
    Code:
    int  grossPay(double &)
    {
      double  hours, rate; 
      if (hours <= 40)
         grossPay = (hours * rate);
      else
         grossPay = ((hours - 40) * rate * 1.5);
    }
    grossPay would need to be a variable if you're assigning a value to it, and the function needs to return a value. I've used languages where you return function values like that, but C++ doesn't work that way.

    There are a lot of things wrong with just that function.

    1) You're passing in an argument you can't get to because it isn't named.
    2) hours and rate are unitialized. You have no idea what values they contain. Could be anything.
    3) You're returning an int, but the value could contain a fractional component.
    4) As mentioned, grossPay isn't defined.

    In general, you still don't seem to grasp the concept of scope. For example, everywhere you have a statement such as "double hrs[SZ];", you're defining a new variable called hrs, that is completely unrelated to do with the previous definitions. They're not the same variable, even if they have the same name.

    C++ doesn't lend itself to guessing. Go back and reread the book or whatever you're learning from. Don't try to write a whole program and compile it all at once. Write a few lines. Compile and test them. When you're sure they're working, move on.

  13. #13
    Join Date
    Nov 2013
    Posts
    49

    Question Re: need help with my source code but it wont complie and giving me errors

    int regular()
    {
    double hours, rate; // THE DATA FOR HOURS AND RATE SHOULD BE READ FROM TXT SHOWN BELOW
    if(hours <= 40)
    grossPay = hours * rate;
    }

    //txt file//
    Hours Pay rate empID first name last name
    40.0 10.00 A1234 Jane Adams
    50.0 10.00 L8765 Mary Lincoln
    25.5 10.85 W7654 Martha Washington
    52.0 15.75 A9876 John Adams
    45.0 25.00 W1235 George Washington
    40.25 55.00 L9087 Abraham Lincoln
    30.0 9.75 T9876 William Tell
    42.5 12.50 M7654 Missy Muffett
    30.0 10.00 P8765 Peter Piper

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

    Re: need help with my source code but it wont complie and giving me errors

    Quote Originally Posted by Krrish1 View Post
    int regular()
    {
    double hours, rate; // THE DATA FOR HOURS AND RATE SHOULD BE READ FROM TXT SHOWN BELOW
    if(hours <= 40)
    grossPay = hours * rate;
    }

    //txt file//
    Hours Pay rate empID first name last name
    40.0 10.00 A1234 Jane Adams
    50.0 10.00 L8765 Mary Lincoln
    25.5 10.85 W7654 Martha Washington
    52.0 15.75 A9876 John Adams
    45.0 25.00 W1235 George Washington
    40.25 55.00 L9087 Abraham Lincoln
    30.0 9.75 T9876 William Tell
    42.5 12.50 M7654 Missy Muffett
    30.0 10.00 P8765 Peter Piper
    They are in main, but as has been mentioned several times, the hours in main and the hours in regular are separate, unrelated variables. You're being told the problem, but you're not taking any action to correct it. Your code is beyond correcting. It needs a rewrite, and nobody here, with one possible exception, is going to do that for you. Start with a couple of lines. Use the debugger to makes sure it works, then move on to the next few lines.

  15. #15
    Join Date
    Nov 2013
    Posts
    49

    Re: need help with my source code but it wont complie and giving me errors

    so how can we use the same variable in the calculations

Page 1 of 2 12 LastLast

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center