“Write a C++ program (use multi-dimensional arrays) that uses an array structures (size 8) to store the following information on a company’s 8 divisions:

Division name (such as East, West, North, South, Northeast, Northwest, Southeast, and Southwest)

First Quarter sales
Second quarter sales
Third quarter sales
Fourth Quarter Sales
Fourth Quarter sales
Total annual sales
Average quarterly sales

Each array element should contain all information about one of the eight divisions (East, West, North, etc.). The user should be asked for the four quarters’ sales figures for each division. (You are allowed to store user input data in an input file, if you wish). Each division’s total and average sales should be calculated and stored in the appropriate member field. Once all data are filled, print a report to a file displaying all information for all 8 divisions.

NB. Print your report in descending order of Average quarterly sales.
Do not allow the user to input negative values for any of the sales figures.”
----------------------------------------------------------------------------------

#include <iomanip>
#include <iostream>
#include <fstream>

using namespace std;

struct Comp;
float first;
float second;
float third;
float val;
float avg;

string const N = "North Division";
string const S = "South Division";
string const E = "East Division";
string const W = "West Division";
string const NW = "North West Division";
string const NE = "North East Division";
string const SW = " South West Division";
string const SE = " South East Division";


int main()
{
	ofsttream outFile;
	outFile.open ("outsales.dat")
	Comp div[8]

	div [0].name= N;
	div [1].name= S;
	div [2].name= E;
	div [3].name= W;
	div [4].name= NW;
	div [5].name= NE;
	div [6].name= SW;
	div [7].name = SE;

	outFile<<setw(10)<< "Division"<<setw(15)<<"First Quarter"<<setw(15)<<"Second Quarter"<<setw(15)<<"Third Quarter"<<setw(15)<<"Fourth Quarter"<<setw(15)<<"Total"<<setw(15)<<"Average"<<endl;
	
	for (int j=0; j<8; j++)
	{ outFile<<setw(15)<<div[j].name;
	cout<< "Please enter First Qrt Sales"<<div[j].name<<endl;
	cin>>div[j].fir;
	if (div[j].fir<1)
		{
			cout<< "You can only enter postive numbers!!"<<endl;
		}
	outFile<<fixed<<showpoint<<setprecision(2)<<setw(15)<<div[j].fir<<endl;

	cout<< "Please enter Second Qrt Sales"<<div[j].name<<endl;
	cin>>div[j].sec;
	if (div[j].sec<1)
		{
			cout<< "You can only enter postive numbers!!"<<endl;
		}
	outFile<<fixed<<showpoint<<setprecision(2)<<setw(15)<<div[j].sec<<endl;

	cout<< "Please enter Third Qrt Sales"<<div[j].name<<endl;
	cin>>div[j].thir;
	if (div[j].thir<1)
		{
			cout<< "You can only enter postive numbers!!"<<endl;
			break;
		}
	outFile<<fixed<<showpoint<<setprecision(2)<<setw(15)<<div[j].thir<<endl;
	
	cout<< "Please enter Fourth Qrt Sales"<<div[j].name<<endl;
	cin>>div[j].four;
	if (div[j].four<1)
		{
			cout<< "You can only enter postive numbers!!"<<endl;
			break;
		}
	outFile<<fixed<<showpoint<<setprecision(2)<<setw(15)<<div[j].four<<endl;

	div [j].total= (div[j].four + div[j].thir + div[j].sec + div[j].fir);
	outFile<<setw(15)<<div[j].total;

	div[j].avg = (div[j].total)/4;
	outFile<<setw(15)<<div[j].avg<<endl;
	}
	void BubbleSort (Comp div[], int size)
	{
		bool QrtSal= true;
		while (QrtSal)
		{
			QrtSal=false;
			for (int j=0; j<size + 1; --j)
				if (avg (div[j]<avg(div[j-1))
				{ Comp qrt = div [j-1]; div [j-1]=div[j]; div [j-1]= Comp;
				QrtSal=true;
			}
		}
	}



	return 0;
}

----------------------------------------------------------------------------------
Good day: I have a lot of things going on here...i'm lost...I'm getting compiler errors such as:

1. error C2065: 'ofsttream' : undeclared identifier
2. error C2275: 'Comp' : illegal use of this type as an expression
3. error C2228: left of '.name' must have class/struct/union
4. error C2275: 'Comp' : illegal use of this type as an expression
5. error C2036: 'Comp []' : unknown size
There are too many errors for me to list. But can you see what I'm doing wrong?

Thank you and have a good day.
5.

Recommended Answers

All 12 Replies

Since my professor say I should "break" out of an "for" loop....what is my alternative?

Since my professor say I SHOULD NOT "break" out of an "for" loop....what is my alternative?

What additional declaration should i give to Comp...but it's an array, or how else could I use 'Comp'.

First, look carefully at the compiler errors, and correct spelling/typing mistakes (I see at least one!)

Second, where is your struct definition..? (Perhaps you need to read up on how to create a struct)

Ok, thx bench, i'll repost my code inna few...imma do sone reading up some more on the struct.

How about something like this:

#include <iomanip>
#include <iostream>
#include <fstream>
#include <cmath>


using namespace std;

 void TotalAverage(int i, int j);
 void giveDivision(int i);
 void Enter_Fourth_Quarter_Score();
 int array[Divisions][Quarters];
 void sort(int Arrayavg[8]);
 int printResult();
 void deleteresult();

	double EastTotal= 0, WestTotal= 0,NorthTotal= 0, SouthTotal= 0,
    NortEastTotal= 0, NorthWestTotal= 0, SouthEastTotal= 0,
    SouthWestTotal= 0;
	double EastAverage = 0, WestAverage = 0, NorthAverage = 0, SouthAverage = 0,
    NortEastAverage = 0, NorthWestAverage = 0, SouthEastAverage = 0,
    SouthWestAverage = 0;
	double East,  West, North, South, NortEast, NorthWest, SouthEast,  SouthWest;

const string N = "North Division";
const string S = "South Division";
const string E = "East Division";
const string W = "West Division";
const string NW = "North West Division";
const string NE = "North East Division";
const string SW = " South West Division";
const string SE = " South East Division";



int main()
{

	char Month[20];
	int Day, Year, i, j;
	ofstream outFile;
	outFile.open ("outsales.dat")

	
	Divisions div[8]
	Quarters[8]

	div [0].name= N;
	div [1].name= S;
	div [2].name= E;
	div [3].name= W;
	div [4].name= NW;
	div [5].name= NE;
	div [6].name= SW;
	div [7].name = SE;

	outFile<<setw(10)<<"Division"<<setw(15)<<"First Quarter"<<setw(15)<<"Second Quarter"<<setw(15)<<"Third Quarter"<<setw(15)<<"Fourth Quarter"<<setw(15)<<"Total"<<setw(15)<<"Average"<<endl;
	
	for (int j=0; j<8; j++)
      cout << " Enter Fourth Quarter Score for Division NortEast"
   		<< '\n';
      cin >> NortEast;
   }while (NortEast < 0);


   
   //the following will ask the user to input Fourth Quarter sales
   //ensure that the value entered is a positive value
   do{
      cout << " Enter Fourth Quarter Score for Division NorthWest"
   		<< '\n';
      cin >> NorthWest;
   }while (NorthWest < 0);



   //the following will ask the user to input Fourth Quarter sales
   //ensure that the value entered is a positive value
   do{
      cout << " Enter Fourth Quarter Score for Division SouthEast"
   		<< '\n';
      cin >> SouthEast;
   }while (SouthEast < 0);



   //the following will ask the user to input Fourth Quarter sales
   //ensure that the value entered is a positive value
   do{
      cout << " Enter Fourth Quarter Score for Division SouthWest"
   		<< '\n';
      cin >> SouthWest;
   }while (SouthWest < 0);





     	cout<< East <<  ' ' << West <<  ' ' <<North <<  ' '
         <<South<<  ' ' <<NortEast <<  ' ' <<NorthWest <<  ' '
         <<SouthEast <<  ' ' << SouthWest;

}







void TotalAverage(int i, int j){
//............................................



    for(i=0; i<Divisions; i++){
       for(j=0; j<4; j++){
          if(i == 0){
       	    EastTotal += array[i][j];}
          if(i == 1){
       	    WestTotal += array[i][j];}

          if(i == 2){
       	    NorthTotal += array[i][j];}

          if(i == 3){
       	    SouthTotal += array[i][j];}

          if(i == 4){
       	    NortEastTotal += array[i][j];}

          if(i == 5){
       	    NorthWestTotal += array[i][j];}

          if(i == 6){
       	    SouthEastTotal += array[i][j];
              cout << '\n' <<"total SouthEastTotal " << SouthEastTotal<< '\n';}
          if(i == 7){
       	    SouthWestTotal += array[i][j];

             cout << '\n' <<"total SouthWestTotal " << SouthWestTotal<< '\n';  }



       } //end inner for loop
    }//end first for loop

    EastAverage =  EastTotal /4;
    WestAverage =  WestTotal /4;
    NorthAverage =  NorthTotal  /4;
    SouthAverage =   SouthTotal  /4;
    NortEastAverage =  NortEastTotal  /4;
    NorthWestAverage = NorthWestTotal   /4;
    SouthEastAverage = SouthEastTotal /4;
    SouthWestAverage = SouthWestTotal /4;

   int 	Arrayavg[8];

for(i = 0; i < 8; i++){
	if(i == 0){Arrayavg[i] = EastAverage;}
   if(i == 1){Arrayavg[i] = WestAverage;}
   if(i == 2){Arrayavg[i] = NorthAverage;}
   if(i == 3){Arrayavg[i] = SouthAverage;}
   if(i == 4){Arrayavg[i] = NortEastAverage;}
   if(i == 5){Arrayavg[i] = NorthWestAverage;}
   if(i == 6){Arrayavg[i] = SouthEastAverage;}
   if(i == 7){Arrayavg[i] = SouthWestAverage;}

}

  cout << '\n' << "==========================================================";
cout << '\n' << "Divisions       Total           Average";
cout << '\n' << " East           "<<EastTotal<< setw(15) <<    EastAverage;
cout << '\n' << " West           "<<WestTotal<< setw(15) <<   WestAverage;
cout << '\n' << " North          "<<NorthTotal<<setw(15) <<    NorthAverage;
cout << '\n' << " South          "<<SouthTotal<<setw(15) <<    SouthAverage;
cout << '\n' << " Northeast      "<<NortEastTotal<<setw(15) <<    NortEastAverage;
cout << '\n' << " Northwest      "<<NorthWestTotal<<setw(15) <<    NorthWestAverage;
cout << '\n' << " SouthEast      "<<SouthEastTotal<<setw(15) <<    SouthEastAverage;
cout << '\n' << " SouthWest      "<<SouthWestTotal<<setw(15) <<    SouthWestAverage;
cout << '\n' << '\n';

cout << "the average in decending order" << '\n';
   sort(Arrayavg) ;
}//end function



void Bubblesort(int Arrayavg[8]){
//	Arrayavg[8] = {1, 2, 3, 4, 5, 6, 7, 8};
   int i, hold;

   for(int pass =0; pass < 8 - 1; pass++)
      for(i=0; i < 8 - 1; i++)
         if (Arrayavg[i] < Arrayavg[ i +1]){
         	hold = Arrayavg[i];
            Arrayavg[i] = Arrayavg[i + 1];
            Arrayavg[i + 1] = hold;
         }

   for( i= 0; i < 8; i++)
   cout << Arrayavg[i] << ' ';
}//end function







   cout << "Division  " <<  ' ' << "Total" <<  ' ' <<"Average" <<'\n';
   cout << "East      " <<  ' ' << EastTotal <<  ' ' <<EastAverage <<'\n';
   cout << "West      " <<  ' ' << WestTotal <<  ' ' <<WestAverage<<'\n';
   cout << "North     " <<  ' ' << NorthTotal <<  ' ' <<NorthAverage<<'\n';
   cout << "South     " <<  ' ' << SouthTotal <<  ' ' <<SouthAverage <<'\n';
   cout << "NortEast  " <<  ' ' << NortEastTotal <<  ' ' <<NortEastAverage <<'\n';
   cout << "NorthWest " <<  ' ' << NorthWestTotal <<  ' ' <<NorthWestAverage <<'\n';
   cout << "SouthEast " <<  ' ' << SouthEastTotal <<  ' ' <<SouthEastAverage <<'\n';
   cout << "SouthWest " <<  ' ' << SouthWestTotal <<  ' ' <<SouthWestAverage <<'\n';


  return 0;
}

I'm getting brain freeze....any help is apprecited and also to tidy-up the program...

>>“Write a C++ program (use multi-dimensional arrays) that uses an array structures

Where have you declared the structure ? and where did you declare the array? You need a structure something like this:

struct company
{
    std::string company_name;
    long Quarter_sales[4];
    long Total_annual_sales;
    long Average_quarterly_salesl
};

lines 45 and 46: Divisions and Quarters should be combined into one structure something similar to what I posted above.

lines 148-155: delete those variables. The calculations should be stored in the structure members not in simple individual int objects.

lines 193-199: That should be sorting the array of the structures similar to what I posted above. Sorting an array that only contains the averages will not give you what you want because it doesn't keep the other information in the same order. Putting everything in a structure similar to what I posted previously and sorting that array solves that problem very easily.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <iostream>
#include<cmath>
#include<cstring>

using namespace std;

float Efir,Esec, Ethir,Efour;
float Sfir,Ssec, Sthir,Sfour;
float Nfir,Nsec, Nthir,Nfour;
float Wfir, Wsec, Wthir,Wfour; 
float NEfir,NEsec,NEthir,NEfour;
float SEfir,SEsec,SEthir,SEfour;
float NWfir,NWsec,NWthir,NWfour;
float SWfir,SWsec,SWthir, SWfour;

float WestTotal= Wfir + Wsec + Wthir + Wfour;
float EastTotal= Efir + Esec + Ethir + Efour;
float NorthTotal= Nfir + Nsec + Nthir + Nfour;
float SouthTotal= Sfir + Ssec + Sthir + Sfour;
float NorthEastTotal= NEfir + NEsec + NEthir + NEfour;
float NorthWestTotal= NWfir + NWsec + NWthir+ NWfour;
float SouthEastTotal = SEfir + SEsec + SEthir + SEfour;
float SouthWestTotal= SWfir + SWsec+  SWthir + SWfour; 

float EastAverage= (Efir + Esec + Ethir + Efour)/4;
float WestAverage= (Wfir + Wsec + Wthir + Wfour)/4;
float NorthAverage=(Nfir + Nsec + Nthir + Nfour)/4;
float SouthAverage= (Sfir + Ssec + Sthir + Sfour)/4;
float NorthEastAverage=(NEfir+NEsec + NEthir+NEfour)/4; 
float NorthWestAverage=(NWfir+NWsec+NWthir+NWfour)/4;
float SouthEastAverage= (SEfir+SEsec+SEthir+SEfour)/4;
float SouthWestAverage= (SWfir+SWsec+SWthir+SWfour)/4;

int main()
{
cout<<"Enter Four Quarter Scores for Division East, separated by space: "<<endl;
cin>>Efir>>Esec>>Ethir>>Efour;
EastTotal= Efir + Esec + Ethir + Efour;
EastAverage= (Efir + Esec + Ethir + Efour)/4;
if (Efour, Esec, Ethir, Efour<0)
cout<<" No negative numbers allowed !!"<<endl;
else
{
	cout<<EastTotal<<" The average is: "<<EastAverage<<endl;
}
cout<<"Enter Four Quarter Scores for Division North, separated by space: "<<endl;
cin>>Nfir>>Nsec>>Nthir>>Nfour;
NorthTotal= Nfir + Nsec + Nthir + Nfour;
NorthAverage=(Nfir + Nsec + Nthir + Nfour)/4;
if (Nfir, Nsec, Nthir, Nfour<0)
cout<<"No negative numbers allowed !!"<<endl;
else
{
	cout<<NorthTotal<<" The average is: "<<NorthAverage<<endl;
}
cout<<"Enter Four Quarter Scores for Division West, separated by space: "<<endl;
cin>>Wfir>>Wsec>>Wthir>>Wfour;
WestTotal= Wfir + Wsec + Wthir + Wfour;
WestAverage= (Wfir + Wsec + Wthir + Wfour)/4;
if (Wfour, Wsec, Wthir, Wfour<0)
cout<<"No negative numbers allowed !!"<<endl;
else
{
	cout<<WestTotal<<" The average is: "<<WestAverage<<endl;
}
cout<<"Enter Four Quarter Scores for Division South, separated by space: "<<endl;
cin>>Sfir>>Ssec>>Sthir>>Sfour;
SouthTotal= Sfir + Ssec + Sthir + Sfour;
SouthAverage= (Sfir + Ssec + Sthir + Sfour)/4;
if (Sfour, Ssec, Sthir, Sfour<0)
cout<<"No negative numbers allowed !!"<<endl;
else
{
cout<<SouthTotal<<"The average is: "<<SouthAverage<<endl;
}
cout<<"Enter Four Quarter Scores for Division South East, separated by space: "<<endl;
cin>>SEfir>>SEsec>>SEthir>>SEfour;
SouthEastTotal= SEfir + SEsec + SEthir + SEfour;
if (SEfour, SEsec, SEthir, SEfour<0)
cout<<"No negative numbers allowed !!"<<endl;
else
{
	cout<<SouthEastTotal<<" The average is: "<<SouthEastAverage<<endl;
}
cout<<"Enter Four Quarter Scores for Division North East, separated by space: "<<endl;
cin>>NEfir>>NEsec>>NEthir>>NEfour;
NorthEastTotal= NEfir + NEsec + NEthir + NEfour;
NorthEastAverage=(NEfir+NEsec + NEthir+NEfour)/4; 
if (NEfour, NEsec, NEthir, NEfour<0)
cout<<"No negative numbers allowed !!"<<endl;
else
{
	cout<<NorthEastTotal<<" The average is: "<<NorthEastAverage<<endl;
}
cout<<"Enter Four Quarter Scores for Division South East, separated by space: "<<endl;
cin>>SEfir>>SEsec>>SEthir>>SEfour;
SouthEastTotal= SEfir + SEsec + SEthir + SEfour;
if (SEfour, SEsec, SEthir, SEfour<0)
cout<<"No negative numbers allowed !!"<<endl;
else
{
	cout<<SouthEastTotal<<" The average is: "<<SouthEastAverage<<endl;
}
cout<<"Enter Four Quarter Scores for Division North West, separated by space: "<<endl;
cin>>NWfir>>NWsec>>NWthir>>NWfour;
NorthWestTotal= NWfir + NWsec + NWthir + NWfour;
NorthWestAverage=(NWfir+NWsec+NWthir+NWfour)/4;
if (NWfour, NWsec, NWthir, NWfour<0)
cout<<"No negative numbers allowed !!"<<endl;
else
{
	cout<<NorthWestTotal<<" The average is: "<<NorthWestAverage<<endl;
}



	return 0;
}

So pretty much, the assignment is as above, except that for the fact that i'm required to use arrays.

OMG! what did you do in lines 10 thru 35? Delete all that crap and use arrays of structures as your assignment told you to do and as I explained in my previous post.

Thanks...I didn't post it as the assignment, i simply did it as an alternative to see if we had accomplished the same thing...but i'll post the code with the arrays structures soon. Where in this program could i use functions?

#include<iostream>
#include<fstream>
#include<cstring>
#include<iomanip>
#include<cmath>

using namespace std;

struct Divsn
{
	float first, second, third, fourth, total, avg;
	string name;
};


int main()
{
	const string N= "North";
	const string W= "West";
	const string S="South";
	const string E="East";
	const string NW="North West";
	const string NE="North East";
	const string SW="South West";
	const string SE="South East";

 ofstream outFile;
 outFile.open("Company.dat");

 Divsn div[8];

 div[0].name=N;
 div[1].name=W;
 div[2].name=S;
 div[3].name=E;
 div[4].name=NW;
 div[5].name=NE;
 div[6].name=SW;
 div[7].name=SE;

 outFile<<"Division"<<setw(10)<<"1ST Quarter"<<setw(15)<<"2ND Quarter"<<setw(15)<<"3RD Quarter"<<setw(15)<<"4TH Quarter"<<setw(15)<<"Total"<<setw(15)<<"Average"<<endl;
 for (int j=0; j<8; j++)
 {
	 outFile<<setw(10)<<div[j].name;

	 cout<<"Please enter the the first quarter sales"<<div[j].name<<endl;
	 cin>>div[j].first;
	 while (div[j].first<0)
	 {
		 cout<<"No negative values please"<<endl;
		 cin>>div[j].first;
	 }
	 outFile<<fixed<<showpoint<<setprecision(2)<<setw(15)<<div[j].first;
	 
	 cout<<"Please enter the the second quarter sales"<<div[j].name<<endl;
	 cin>>div[j].second;
	 while (div[j].second<0)
	 {
		 cout<<"No negative values please"<<endl;
		 cin>>div[j].second;
	 }
	 outFile<<fixed<<showpoint<<setprecision(2)<<setw(15)<<div[j].second;

	 cout<<"Please enter the the third quarter sales"<<div[j].name<<endl;
	 cin>>div[j].third;
	 while (div[j].third<0)
	 {
		 cout<<"No negative values please"<<endl;
		 cin>>div[j].third;
	 }

	 outFile<<fixed<<showpoint<<setprecision(2)<<setw(15)<<div[j].third;
	 cout<<"Please enter the the fourth quarter sales"<<div[j].name<<endl;
	 cin>>div[j].fourth;
	 while (div[j].fourth<0)
	 {
		 cout<<"No negative values please"<<endl;
	 }
	 outFile<<fixed<<showpoint<<setprecision(2)<<setw(15)<<div[j].fourth<<endl;
	 div[j].total=(div[j].first+div[j].second+div[j].third+div[j].fourth);
	 outFile<<setw(15)<<div[j].total;

	 div[j].avg= (div[j].total)/4.0;
		 outFile<<setw(15)<<div[j].avg<<endl;
		 outFile<<endl;
 }
 outFile.close();

	return 0;
}

This is the code i came up with:
1. How can i use this same code, but use functions?
2. I get errors when i try to compile it
3. How can I print my report in descending order of Average quarterly sales.
Thank you and have a good day.

Member Avatar for iamthwee

>How can i use this same code, but use functions?


Functions are most commonly used for parts of the code which get repeated. Decide which parts would benefit from functions and put them there.

>How can I print my report in descending order of Average quarterly sales.

Basically create a sort routine but sort by the attribute in the structure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.