-4

Please point out where specifically as well as what new specific edits need to be made. I keep getting the same errors and I have no idea what is wrong. I've cecked brackes a million times and am pretty sure I'm doing this right:

  • cpp:36: error: a function-definition is not allowed here before ‘{’ token
  • cpp:44: error: a function-definition is not allowed here before ‘{’ token
  • cpp:58: error: expected initializer before ‘double’
  • cpp:63: error: a function-definition is not allowed here before ‘{’ token
  • cpp:69: error: a function-definition is not allowed here before ‘{’ token

Code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{   
    string item = "";
    ifstream fin;
    double tgross = 0;
    double tnet = 0;
    double hourly;
    double hours;
    double taxrate;
    double net;

    string fileName = "payroll.txt";    
    fin.open("payroll.txt");

    if(!fin.is_open())
    {   
        void instructions() 
        {
            cout << "This payroll program calculates an individual employee pay and";
            cout << "\ncompany totals using data from a data file payroll.txt.\n"; 
            cout << "\n\nA payroll report showing payroll information ";
            cout << " is displayed.\n\n";
        }

        void reportTitle() 
        {
            cout << setprecision(2) << fixed << showpoint << left
                << setw(20) << "Employee" << setw(10) << "Hourly" << setw(10) << "Hours"
                << setw(10) << "Tax" << setw(10) << "Gross" << setw(10) << "Net" << endl;
            cout << setw(20) << "Name" << setw(10) << "Rate" << setw(10) << "Worked"
                << setw(10) << "Rate" << setw(10) << "Amount" << setw(10) << "Amount" << endl;
        }
    }

    while(!fin.eof())
    {
        getline(fin,item,'#');
        fin >> hourly >> hours >> taxrate;

        double calculateGross(double hours, double hourly)
        double calculateNet(double grosspay, double netpercent)
        {
            return grosspay - grosspay*netpercent/100.0;
        }

        void displayEmployeeInfo(const string &, double, double, double, double, double)
        {
            tgross += grosspay;
            tnet += net;
        }
    }

    void totalAmounts (double tgross, double tnet)
    {
        cout << "Totals" << setprecision(2) << fixed << showpoint << right
            << setw(50) << tgross << setw(10) << tnet << endl;
    }

    fin.close();
}
paddy
  • 60,864
  • 6
  • 61
  • 103
existence
  • 1
  • 2
  • 7
    `void instructions() ` you cannot define functions inside other functions. –  Nov 04 '18 at 23:33
  • how do I fix it? I was following the template I was given which had cout statements inside the functions. – existence Nov 04 '18 at 23:39
  • 4
    cout statements inside functions is fine. Functions inside functions is not fine. – melpomene Nov 04 '18 at 23:41
  • 2
    You need to _declare_ and _define_ your functions outside of other functions, but you can _call_ them within functions – Tas Nov 04 '18 at 23:41
  • 1
    Don't follow templates; watch the parking meters. –  Nov 04 '18 at 23:42
  • 1
    While we're at it, [`while(!fin.eof())` is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – melpomene Nov 04 '18 at 23:42
  • Where you going for lambda or functors? – Jake Freeman Nov 04 '18 at 23:49
  • Please specifically point out which parts need to be changed and to what. Which functions? Why are my statements that are cout still producing the same error if there are no function. – existence Nov 05 '18 at 00:02
  • 7
    Start with a small program, make sure it compiles, then gradually add things. At the moment you wrote 80 lines of code having no idea what you are doing and there are many mistakes. – M.M Nov 05 '18 at 00:28
  • 1
    You would be better off deleting the whole thing and starting again, instead of trying to repair from here – M.M Nov 05 '18 at 00:35
  • Is it really that hard for people to be helpful? – existence Nov 05 '18 at 00:41
  • 3
    Is it really that hard to take the advice you've been given? – M.M Nov 05 '18 at 00:53
  • 2
    Your code has multiple basic structure errors. A good book (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) may be a good place to start. – Michael Surette Nov 05 '18 at 00:55
  • Not when people are vague and I'm new. I don't know things. But I guess that's too much to ask. – existence Nov 05 '18 at 01:03
  • 3
    I don't think there's anything vague about saying that functions cannot be defined inside another function -- they must be defined at global scope. Nor is it vague to be encouraged to start from a empty program, and slowly add things to it while testing that it compiles and behaves the way you expect. Combine those two concepts, and you can try writing a small program where you successfully define a single function and call it from `main()`. You can then move forward from there. – paddy Nov 05 '18 at 01:09
  • 3
    @existence You're trying to do too many things at once. Start with something as simple as it has to be so long as it compiles and runs. Then add things to it. That way, if it stops compiling or executing, you'll know that it was the last thing you added that caused the problem. And if you can get it to compile but not run, you can at least run it in a debugger to see how far it gets and where it goes awry. – David Schwartz Nov 05 '18 at 01:29
  • We said what you should do… Teaching the language is beyond the scope of this site. Also, the purpose of the site is not to do other people homework nor a service to write code for other. If you learn the language by yourself, you should read a book or do a tutorial step by step. C++ does not supporte nested function definition. C++ require function declaration before its use. And you probably want to call the function you define. – Phil1970 Nov 05 '18 at 01:39
  • In this example, it's not the case, but if you really need functions within functions, you could define a class with local scope. And within that class you can define functions. – Karl Zeilhofer Nov 06 '18 at 09:22

2 Answers2

0

You have to put your functions before main

void instructions() 
{
    cout << "This payroll program calculates an individual employee pay and";
    cout << "\ncompany totals using data from a data file payroll.txt.\n"; 
    cout << "\n\nA payroll report showing payroll information ";
    cout << " is displayed.\n\n";
}

By the way, for consistency and improve readability, you should either have all your line changes at the beginning or all at the end of line. Otherwise, it make harder to see that, for example, you have 3 line between payroll.txt and A payroll report...

// Other functions here…
// If some functions are dependant on others, those need to be declared before they are used.

int main()
{
      // Some code here…

      // Call your function
      instructions();

      // More code afterwards…

      return 0; 
}

Alternatively, you can only declare your function before main like this:

void instructions();
void reportTitle();
double calculateGross(double hours, double hourly);
double calculateNet(double grosspay, double netpercent);

// For documentation purpose, you should name your arguments.
// Also the body of your function does not appears to do what its name suggest.
void displayEmployeeInfo(const string &, double, double, double, double, double);

// Show probably named displayTotalAmounts
void totalAmounts(double tgross, double tnet);

You need to be aware that you have to pass appropriate arguments when calling a function. For example:

int main() // partial implementation
{
    double tgross = 1.0; // Whatever code you need to have desired value...
    double tnet = 0.90;

    totalAmounts(tgross, tnet);

    return 0;
}

If you use that later option, then you could define your other function here (after main).

This give the basic idea how to structure your program.

Read all other comments to find other problems in your code!

Here a few extra things:

  • You define variable fileName and even initialize it but then afterward you open the file using the string.
  • If you need to modify a variable passed in argument to a function so that the caller see the change, you need to pass it by reference. For example: double &tnet.
  • Usually, it is preferable to avoid using namespace std in production code.
  • It is best to declare variables at their first use.
  • Some variables line net seems to never be initialized.
  • As written, then instruction if(!fin.is_open()) seems suspicious. Assuming no error the file would be open at that point but you probably want to display the header in that case!
  • It is useless to initialize string with = "" as a string has a default constructor that create it empty.
  • I would recommend you to add one space between a keyword like if or while and the opening parenthesis.
  • Also, you should be consistant in your spacing. While do you have a space after totalAmountswhile for other function, it is not the case.
  • And for your naming of variable too. Why are you using camel case for fileName while taxrate is in lowercase. If you use lower case, then you should use an _ to separate words (for ex. tax_rate) as it make easier to read.
  • You should avoid abbreviation when naming variables. total_net (or totalNet) is way easier to understand than tnet.
  • Usually when you have a compiler error, the first problem is near the location reported by the compiler. Fix that error, then check if other errors are real error or a consequence of the first one. It help to compile a single file in that case (for large production projects with hundreds of files).
Phil1970
  • 2,605
  • 2
  • 14
  • 15
0

This code gives a bit less warnings, but you really have to tidy the code up by your self. I also don't want to say, declaring all variables globally is a good solution.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;


string item = "";
ifstream fin;
double tgross = 0;
double tnet = 0;
double hourly;
double hours;
double taxrate;
double net;

void instructions() 
{
    cout << "This payroll program calculates an individual employee pay and";
    cout << "\ncompany totals using data from a data file payroll.txt.\n"; 
    cout << "\n\nA payroll report showing payroll information ";
    cout << " is displayed.\n\n";
}

void reportTitle() 
{
    cout << setprecision(2) << fixed << showpoint << left
        << setw(20) << "Employee" << setw(10) << "Hourly" << setw(10) << "Hours"
        << setw(10) << "Tax" << setw(10) << "Gross" << setw(10) << "Net" << endl;
    cout << setw(20) << "Name" << setw(10) << "Rate" << setw(10) << "Worked"
        << setw(10) << "Rate" << setw(10) << "Amount" << setw(10) << "Amount" << endl;
}

double calculateNet(double grosspay, double netpercent)
{
    return grosspay - grosspay*netpercent/100.0;
}

void displayEmployeeInfo(const string &, double, double, double, double, double)
{
    tgross += grosspay;
    tnet += net;
}

void totalAmounts (double tgross, double tnet)
{
    cout << "Totals" << setprecision(2) << fixed << showpoint << right
        << setw(50) << tgross << setw(10) << tnet << endl;
}

int main()
{   
    string fileName = "payroll.txt";    
    fin.open("payroll.txt");

    if(!fin.is_open())
    {   
        instructions();
        reportTitle();
    }

    while(!fin.eof())
    {
        getline(fin,item,'#');
        fin >> hourly >> hours >> taxrate;

        double calculateGross(double hours, double hourly);
    }

    fin.close();
}
Karl Zeilhofer
  • 420
  • 1
  • 4
  • 10