-1

I am trying to measure savings as an example for a school project and I really don't know what I did wrong of course adding the if statement inside the function will not work since it can't call itself

    #include <iostream>
    using namespace std;
    int main(){
 struct worker{
    int salary;
    float taxperc;
    int bills;
    int home_needs;
    int bonus;
    bool measure;
    void saving(){
        float tax = (salary/100)*taxperc;
        cout << salary - tax -bills-home_needs  << endl;
    }
    if (measure)
        saving();
};
worker me = {50000, 2.5, 150, 3000, 0, true};
}
Xyz
  • 123
  • 1
  • 4
  • [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Feb 23 '17 at 20:06

1 Answers1

0

Not sure what you are trying to achieve, but you cannot have an if at the struct scope level. Directly in struct { ... } you can only have:

  • member values (fields)
  • member functions

and not a regular code, statements, etc. When would you actually expect such code to be executed?

Your options are:

  • put if (measure) { saving(); } into the another member function
  • invoke saving() from outside the struct, in your main function, e.g. me.saving()

P.S. I strongly suggest you work on indentation of your code too, for readability sake.

CygnusX1
  • 20,968
  • 5
  • 65
  • 109