### structure problem

Write a program that uses a structure to store the following information on a company division:
Division name (East and West)
Quarter (1, 2, 3, 4)
Quarterly Sales
Total corporate sales for each quarter
Total corporate sales for each division
Total yearly corporate sales

** I do not think that I am defining the variables correctly like (east.firstQtr) in the called functions. How and where should I define those kinds of variables?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170`` `````` #include "stdafx.h" #include #include #include #include using namespace std; double setTotSales(double); double setDivSales(double); double setAnnualSales(double); void printStruct(double); struct CorpData { string divisionName; double firstQtr; double secondQtr; double thirdQtr; double fourthQtr; double totalAnnual; double averageQtr; }; int main() { CorpData east; CorpData west; fstream corpsales ("corpsales.dat", ios::out); if (!corpsales) { cout << "Error opening file.\n"; return 0; } else cout << "Enter the following information about the East Division\n"; cout << "Enter the the data for First Quarter :\n"; cin >> east.firstQtr; while (east.firstQtr < 0) { cout << "Sales figures cannot be negative. Please re-enter a postive amount\n"; cin >> east.firstQtr; } cout << "Enter the the data for Second Quarter :\n"; cin >> east.secondQtr; while (east.secondQtr < 0) { cout << "Sales figures cannot be negative. Please re-enter a postive amount\n"; cin >> east.secondQtr; } cout << "Enter the the data for Third Quarter :\n"; cin >> east.thirdQtr; while (east.thirdQtr < 0) { cout << "Sales figures cannot be negative. Please re-enter a postive amount\n"; cin >> east.thirdQtr; } cout << "Enter the the data for Fourth Quarter :\n"; cin >> east.fourthQtr; while (east.fourthQtr < 0) { cout << "Sales figures cannot be negative. Please re-enter a postive amount\n"; cin >> east.fourthQtr; } cout << "Enter the following information about the West Division\n"; cout << endl; cout << "Enter the the data for First Quarter :\n"; cin >> west.firstQtr; while (west.firstQtr < 0) { cout << "Sales figures cannot be negative. Please re-enter a postive amount\n"; cin >> west.firstQtr; } cout << "Enter the the data for Second Quarter :\n"; cin >> west.secondQtr; while (west.secondQtr < 0) { cout << "Sales figures cannot be negative. Please re-enter a postive amount\n"; cin >> west.secondQtr; } cout << "Enter the the data for Third Quarter :\n"; cin >> west.thirdQtr; while (west.thirdQtr < 0) { cout << "Sales figures cannot be negative. Please re-enter a postive amount\n"; cin >> west.thirdQtr; } cout << "Enter the the data for Fourth Quarter :\n"; cin >> west.fourthQtr; while (west.thirdQtr < 0) { cout << "Sales figures cannot be negative. Please re-enter a postive amount\n"; cin >> west.fourthQtr; } corpsales.write(reinterpret_cast(&east), sizeof(east)); corpsales.write(reinterpret_cast(&west), sizeof(west)); corpsales.close(); return 0; } // call getDivSales double setDivSales() { double div1, div2, div3, div4; div1 = east.firstQtr + west.firstQtr; div2 = east.secondQtr + west.secondQtr; div3 = east.thirdQtr + west.thirdQtr; div4 = east.fourth.Qtr + west.fourthQtr; } // call getAnnualSales double setAnnualSales() { double AnnualSalesEast; double AnnualSalesWest; AnnualSalesEast = (east.firstQtr + east.secondQtr + east.thirdQtr + east.fourthQtr); AnnualSalesWest = (west.firstQtr + west.secondQtr + west.thirdQtr + west.fourth.Qtr); } // call getTotalSales double setTotalSales() { double TotalSales; TotalSales = (east.firstQtr = east.secondQtr + east.third.Qtr + east.fourthQtr + west.firstQtr + west.secondQtr + west.thirdQtr + west.fourthQtr); } // call printStruct void printStruct(double) { double AnnualSalesEast, AnnualSalesWest, div1, div2, div3, div4, TotalSales; cout << setw(10) << "Div." << setw(10) << "Qtr1" << setw(10) << "Qtr2" << setw(10) << "Qtr3" << setw(10) << "Qtr4" << setw(10) << "TOTAL"; cout << setw(10) << "East" << setw(10) << east.firstQtr << setw(10) << east.secondQtr << setw(10) << east.thirdQtr << setw(10) << east.fourthQtr << setw(10) << AnnualSalesEast; cout << setw(10) << "West" << setw(10) << west.firstQtr << setw(10) << west.secondQtr << setw(10) << west.thirdQtr << setw(10) << west.fourthQtr << setw(10) << AnnualSalesWest; cout << setw(10) << "TOTAL" << setw(10) << div1 << setw(10) << div2 << setw(10) << div3 << setw(10) << div4 << setw(10) << TotalSales; }``````
first thing i don't understand is, where do u call the all these functions in main, that you have defined.

and second its btr to just keep these functions as member functions of the structure and then work on it, or btr yet make a class.

I did the same program, but using classes. Not a great code,but still can be used for reference, i guess!!!

#include<iostream>
#include<string>

using namespace std;

class compdiv{

public:

compdiv(){}; //default constructor

void set_qtr_sales();
double first_qtr_sales()const{return first_qtr;}
double second_qtr_sales()const{return second_qtr;}
double third_qtr_sales()const{return third_qtr;}
double fourth_qtr_sales()const{return fourth_qtr;}
double get_total_div_sales();

private:

double first_qtr,second_qtr,third_qtr,fourth_qtr;
};

void compdiv::set_qtr_sales()
{
cout<<"Please enter the sales for first quarter: ";
cin>>first_qtr;
cout<<"Please enter sales for second qtr: ";
cin>>second_qtr;
cout<<"Please enter sales for third qtr: ";
cin>>third_qtr;
cout<<"Please enter sales for fourth qtr: ";
cin>>fourth_qtr;
cout<<endl;
}

double compdiv::get_total_div_sales()
{
return first_qtr+second_qtr+third_qtr+fourth_qtr;
}

int main()
{
compdiv east,west;

cout<<"Enter details for division east:\n";
east.set_qtr_sales();
cout<<"Enter details for division west:\n";
west.set_qtr_sales();

cout<<"Total sales for the first quarter: "<<east.first_qtr_sales()+west.first_qtr_sales()<<endl;
cout<<"Total sales for the second quarter: "<<east.second_qtr_sales()+west.second_qtr_sales()<<endl;
cout<<"Total sales for the third quarter: "<<east.third_qtr_sales()+west.third_qtr_sales()<<endl;
cout<<"Total sales for the fourth quarter: "<<east.fourth_qtr_sales()+west.fourth_qtr_sales()<<endl;

cout<<"Total sale for the east divison: "<<east.get_total_div_sales()<<endl;
cout<<"Total sales for the west division: "<<west.get_total_div_sales()<<endl;

cout<<"Total yearly sales: "<<east.get_total_div_sales()+west.get_total_div_sales()<<endl;

keep_window_open();

return 0;

}

I tried calling them the very first thing before i defined the structure. We're not supposed to use a class so I am trying to do it by calling the functions.
no, where do you call the functions in main?
Topic archived. No new replies allowed.