Hello,
Working with structures and C++. Our teacher in class attempted to show us how to over load an "<<" operator. I've tried to do that here in the program below without much luck. I have commented out some parts of the code while I am trying to debug this program. I am trying to get to get cout to print each part of each structure named. And I am attempting to do that with the overloaded cout statement. When I compile this I get an error of:
lab6 ch7 pc 10.cpp(68): error C2275: 'CorpData' : illegal use of this type as an expression
cpp(16) : see declaration of 'CorpData'
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
struct CorpData
{
string Name;
double firstQ, secondQ, thirdQ, fourthQ, average;
CorpData (string N, double q1,double q2, double q3, double q4)
{
Name=N;
firstQ=q1;
secondQ=q2;
thirdQ=q3;
fourthQ=q4;
};
};
void PrintCorpData (const CorpData &Corp)
{
const int x=1000000;
cout.imbue(std::locale(""));
/*cout<<Corp.Name<<" Division"<<endl;
cout<<"Quarter One Sales:$ "<<Corp.firstQ <<endl;
cout<<"Quarter Two Sales:$ "<<Corp.secondQ <<endl;
cout<<"Thrid Quarter Sales:$ "<<Corp.thirdQ <<endl;
cout<<"Forth Quarter Sales:$ "<<Corp.fourthQ << endl;*/
double total= Corp.firstQ + Corp.secondQ + Corp.thirdQ + Corp.fourthQ;
cout<< "Total Sales:$ "<< fixed <<setprecision(2)<<total <<endl;
cout<<"Avg sales:$ " << total/4 << endl;
cout <<endl;
}
ostream &operator<<(ostream &lhs, CorpData rhs)
{
lhs << rhs.Name << " "
<< rhs.firstQ << " "
<< rhs.secondQ << " "
<< rhs.thirdQ << " "
<< rhs.fourthQ << " ";
return lhs;
}
int main()
{
CorpData North ("North", 100000,1100200,102000,10201230);
CorpData South ("South", 14562200,145521120,98751120,19778525430);
CorpData East ("East", 987456,4223651120,55222120,11112230);
CorpData West ("West", 95636660,1458455120,11545456420,1545454545430);
cout << CorpData;
//PrintCorpData(North);
//PrintCorpData(South);
//PrintCorpData(East);
//PrintCorpData(West);
int key;
cout << "Press any key to continue"<< endl;
cin >> key;
return 0;
}