THESE ARE MY CODES AND WORKS WELL, HOWEVER CAN YOU HELP ME WITH THIS : Include t
ID: 3578778 • Letter: T
Question
THESE ARE MY CODES AND WORKS WELL, HOWEVER CAN YOU HELP ME WITH THIS :
Include the preprocessor #ifndef..endif as a guard.(divide this program)
THANKS U.
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
class DivSales
{
public:
string region;
int quarters [4];
double yearlySale;
double allregtot;
DivSales()
{
region = "";
yearlySale = 0;
allregtot = 0;
for(int i=0; i<4; i++)
{
quarters[i]=0;
}
}
~DivSales()
{
}
void getValues()
{
cout<<" Enter Region: ";
cin>>region;
for(int i = 0; i<4;i++)
{
cout<<" Quarter "<<i+1<< ": ";
cin>> quarters[i];
yearlySale = yearlySale + quarters[i];
}
allregtot = allregtot + yearlySale;
}
string getRegion()
{
return region;
}
double getquatsale(int index)
{
return quarters[index];
}
double getyearlysale()
{
return yearlySale;
}
double gettotsale()
{
return allregtot;
}
};
int main()
{
DivSales obj[4];
int allregquat [4];
int tot=0;
for (int i =0; i<4; i++)
{
obj[i].getValues();
}
cout<<" ----------------------- ";
for(int i=0; i<4; i++)
{
cout<<" "<<obj[i].getRegion();
for(int j=0; j<4; j++)
{
cout<<" Quarter "<<j+1<< ": "<<
obj[i].quarters[j];
}
cout<<" Region's total: "<<obj[i].yearlySale;
}
for(int i =0; i<4; i++)
{
tot= tot+ obj[i].gettotsale();
}
cout<<" All Regions Total: "<< tot;
for(int i=0; i<4; i++)
{
allregquat[i]=0;
}
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
allregquat[i]=allregquat[i]+ obj[j].quarters[i];
}
}
cout<<" Extra work: ";
for(int i=0; i<4; i++)
{
cout<<" ALL regiongs Quarter" <<"1: "<<
allregquat[i];
}
system("pause");
return 0 ;
}
Explanation / Answer
I have divided this code in two different files code.cpp and code.h . Important things to note here, to compile do g++ code.cpp code.h -o a.out . code.h is included in code.cpp
code.h
#ifndef CODE_H
#define CODE_H
#include <iostream>
using namespace std;
class DivSales{
public:
string region;
int quarters [4];
double yearlySale;
double allregtot;
DivSales();
~DivSales();
void getValues();
string getRegion();
double getquatsale(int);
double getyearlysale();
double gettotsale();
};
DivSales::DivSales(){
region = "";
yearlySale = 0;
allregtot = 0;
for(int i=0; i<4; i++){ quarters[i]=0; }
}
DivSales::~DivSales(){
}
void DivSales::getValues(){
cout<<" Enter Region: ";
cin>>region;
for(int i = 0; i<4;i++)
{
cout<<" Quarter "<<i+1<< ": ";
cin>> quarters[i];
yearlySale = yearlySale + quarters[i];
}
allregtot = allregtot + yearlySale;
}
string DivSales::getRegion(){
return region;
}
double DivSales::getquatsale(int index){
return quarters[index];
}
double DivSales::getyearlysale(){
return yearlySale;
}
double DivSales::gettotsale(){
return allregtot;
}
#endif
code.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include "code.h"
using namespace std;
int main(){
DivSales obj[4];
int allregquat [4];
int tot=0;
for (int i =0; i<4; i++)
{
obj[i].getValues();
}
cout<<" ----------------------- ";
for(int i=0; i<4; i++)
{
cout<<" "<<obj[i].getRegion();
for(int j=0; j<4; j++)
{
cout<<" Quarter "<<j+1<< ": "<<
obj[i].quarters[j];
}
cout<<" Region's total: "<<obj[i].yearlySale;
}
for(int i =0; i<4; i++)
{
tot= tot+ obj[i].gettotsale();
}
cout<<" All Regions Total: "<< tot;
for(int i=0; i<4; i++)
{
allregquat[i]=0;
}
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
allregquat[i]=allregquat[i]+ obj[j].quarters[i];
}
}
cout<<" Extra work: ";
for(int i=0; i<4; i++)
{
cout<<" ALL regiongs Quarter" <<"1: "<<
allregquat[i];
}
system("pause");
return 0 ;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.