-5

I am new to C++, and am baffled by how the compiler seems to spit out my attempts at using arrays of structs.

In the program below, the compiler says that Salesman cannot be resolved as type. And that it was not declared in scopes. Also, compiler claims struct Salesman does not have some members such as sales or comms, when it clearly does.

    #include <iostream>
    #include <iomanip>
    #include <sstream>
    using namespace std;

    double all_sales;
    double all_coms;
    Salesman salesmen[100];
    final int MAX_MONTH = 2;

    void calc_comp_total(int emp){
        for(int i=1;i<=emp;i++){
            all_sales+=salesmen[i].ytosales;
            all_coms+=salesmen[i].ytocom;

        }
    }

    struct Salesman{

        double sales[MAX_MONTH] ={ };
        double comms[MAX_MONTH] ={ };
        string name;
        double ytosales =0;
        double ytocom =0;

    }
    void calc_personal_totals(Salesman s){
        for(int i=0;i<2;i++)
        {
            s.ytosales += s.sales[i];
            s.ytocom += s.comms[i];
        }
    }

    void store_sales(double sal, double com, int m, Salesman s, string nam){

        s.sales[m] = sal;
        s.comms[m] = com;
        s.name=nam;
    }
    double calculate_commission(double sales){

        if (sales>=5000 && sales<10000)
            return sales*0.1;
        else if(sales>=10000&&sales<18000)
            return sales*0.15;
        else if(sales>=18000)
            return sales*0.22;
        else if(sales<5000)
    return  0;
    }

    main(){

        bool again = true;
        int empl = 0;


        while(again){


            string n;
            double in_sales;

            cout<<"Enter employee name. n for next";
            cin>>n ;
            if(n=="n")
                bool again = false;
            else{


            for(int i=1; i<MAX_MONTH; i++){
                cout<<"Enter month"<< i << " sales";
                cin>>in_sales;

                store_sales( in_sales, calculate_commission(in_sales), i-1,                    salesmen[empl],n);
        }

            calc_personal_totals(salesmen[empl]);
            empl++;

            }
                calc_comp_total(empl);
                cout<<"\n Sales input is complete"

                }

                for(int i=0;i<empl;i++){
                    cout<<"\n "<< salesmen[i].name<<": "<<endl;
                    for(int j=0;j<MAX_MONTH;j++){
                            cout<<"\n $"<<salesmen[i].sales[j]<<" sales"<<" $"
                        <<salesmen[i].comms[j]<<" commission"<<endl;
                }

            cout<<"\n Yearly totals: $"<<salesmen[i].ytosales<<" sales,"<<"         $"<<salesmen[i].ytocom<<
                    "commissions"<<endl;
        }

        cout<<"Company totals: $"<<all_sales<<" sales"<< " $ "        <<all_coms<<endl;

    }

Amy I putting the structure declaration i nthe wrong place? Should I have moved some things into the main statement? Coming from a Java background, I'm encountering many problems I am not used to. Thank you in advance for the help.

antman
  • 3
  • 2

1 Answers1

1

In your array declaration and definition of calc_comp_total, Salesman has not yet been defined, nor declared.

The simple fix is to move the definition of Salesman above those constructs. However, you might be better off defining it in a header file, then including that in your implementation file.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
  • I get the gist of what you mean, but I'm not familiar with header files yet. But I have a quick question. In C++, does it matter the order of declarations outside of the main() method? So what you're telling me is that because Salesman structure is defined below the calc_comp_total method, it doesn't know what salesman is? I remember from Java that order does not matter. Also, I wonder. What happens if you want to declare two structs(or functions), and each struct contains reference to each others' type of structs? Then which one gets to be above the other? Thank you so much! – antman Jul 23 '15 at 22:58
  • The order matters in C++. If you have circular references you need to use forward declaration. I'd recommend picking up a good [introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – TartanLlama Jul 24 '15 at 07:15