In: Computer Science
what am I doing wrong here? thank you!
#include <iostream>
#include <string>
using namespace std;
class DivSales {
private:
int quarterSales[4];
static double totalSales;
public:
void add(int, int, int, int);
int sales(int);
static double getValue()
{
return totalSales;
};
void DivSales::add(int s1, int s2, int s3, int
s4)
{
quarterSales[0] = s1;
quarterSales[1] = s2;
quarterSales[2] = s3;
quarterSales[3] = s4;
totalSales = totalSales + s1 + s2 +
s3 + s4;
}
int DivSales::sales(int n)
{
int value = quarterSales[n];
return value;
}
void error()
{
std::cout << "THe input is
incorrect";
system("pause");
exit(0);
}
double DivSales::totalSales = 0.00;
int main() {
DivSales ds[6];
int i, j;
for (i = 0; i < 6; i++)
{
int s1, s2, s3,
s4;
std::cout
<< "Input quaterly sales" << i + 1 << endl;
std::cout
<< "Input Quarter One Sales";
std::cin
>> s1;
if (s1 <
0)error();
std::cout
<< "Input Quarter Two Sales";
std::cin
>> s2;
if (s2 <
0)error();
std::cout
<< "Input Quarter Three Sales";
std::cin
>> s3;
if (s3 <
0)error();
std::cout
<< "Input Quarter Four Sales";
std::cin
>> s4;
if (s4 <
0)error();
ds[i].add(s1,
s2, s3, s4);
}
std::cout <<
"--------------------------------";
std::cout << "\t" <<
"Qarter One" << "\t" << "Quarter Two" << "\t"
<< "Quarter Three" << "\t" << "Quarter Four"
<< endl;
std::cout <<
"----------------------\n";
for (i = 0; i < 6; i++)
{
std::cout
<< "Div" << i + 1 << "|";
for (j = 0; j
< 4; j++)
std::cout << "\t" <<
ds[i].sales(j);
std::cout
<< endl;
}
std::cout <<
"----------------------------------\n";
std::cout << "\n Total"
<< ds[0].getValue() <<
system("pause");
}
}
Your code has some syntax errors.
Here's the corrected code:
.
#include <iostream>
#include <string>
using namespace std;
class DivSales
{
private:
int quarterSales[4];
static double totalSales;
public:
void add(int, int, int, int);
int sales(int);
static double getValue()
{
return totalSales;
}
};
void DivSales::add(int s1, int s2, int s3, int s4)
{
quarterSales[0] = s1;
quarterSales[1] = s2;
quarterSales[2] = s3;
quarterSales[3] = s4;
totalSales = totalSales + s1 + s2 + s3 + s4;
}
int DivSales::sales(int n)
{
int value = quarterSales[n];
return value;
}
void error()
{
std::cout << "THe input is incorrect";
system("pause");
exit(0);
}
double DivSales::totalSales = 0.00;
int main()
{
DivSales ds[6];
int i, j;
for (i = 0; i < 6; i++)
{
int s1, s2, s3, s4;
std::cout << "\nInput quaterly sales " << i + 1 << endl;
std::cout << "Input Quarter One Sales: ";
std::cin >> s1;
if (s1 < 0)
error();
std::cout << "Input Quarter Two Sales: ";
std::cin >> s2;
if (s2 < 0)
error();
std::cout << "Input Quarter Three Sales: ";
std::cin >> s3;
if (s3 < 0)
error();
std::cout << "Input Quarter Four Sales: ";
std::cin >> s4;
if (s4 < 0)
error();
ds[i].add(s1, s2, s3, s4);
}
std::cout << "--------------------------------\n";
std::cout << "Quarter\tOne\tTwo\tThree\tFour" << endl;
std::cout << "----------------------------------\n";
for (i = 0; i < 6; i++)
{
std::cout << "Div: " << i + 1 << "|";
for (j = 0; j < 4; j++)
std::cout << "\t" << ds[i].sales(j);
std::cout << endl;
}
std::cout << "----------------------------------\n";
std::cout << "\n Total: " << ds[0].getValue() << endl
<< system("pause");
}
.
For testing i have entered 1 2 3 4 only
Output:
.