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.