我和一位朋友在我们的教授指派给我们的节目挑战方面遇到了困难。我们所拥有的是int main()。导师是否一直指着我们,但不会解释什么是错的。所以,如果你能解释并向我们展示我们做错了什么,那么我们将非常感激。
以下是问题: 公司有六个部门,每个部门负责销售到不同的地理位置。设计一个Divsales类,用于保存部门的销售数据,具有以下成员:
编写一个程序,创建一个包含六个DivSales对象的数组。该程序应要求用户输入每个部门四个季度的销售额。输入数据后,程序应显示一个表格,显示每个季度的部门销售额。然后,该程序应显示该年度的公司总销售额。
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
class DivSales
{
private:
double quarter [4];
static double DivCorp;
public:
double display(int);
void qutrSale();
}
void DivSales::qutrSale()
{
int number;
for (number = 0; number < 4; number++)
{
cout << "The sales for the quarter"<< number+1<< ": ";
cin>> quarter [number];
DivCorp += quarter[number];
while (quarter[number] < 0)
{
cout<< "Error, please enter a positive sale amount: $";
cin >> quarter [number];
}
}
}
double DivSales::display(int input)
{
return quarter [input];
}
int main ()
{
int count;
DivSales div[5];
for (count=0; count<5; count++)
{
div [count].qutrSale();
}
return 0;
}
有些错误是
错误1错误C2628:'DivSales'后跟'void'是非法的(你忘了';'?)h:\ school \ cosc 1337.01 \ chapter 11 program challenge 7 \ chapter 11 program challenge 7 \ corporate sales.cpp 23
错误2错误C2556:'DivSales DivSales :: qutrSale(void)':重载函数的区别仅在于来自'void DivSales :: qutrSale(void)'的返回类型h:\ school \ cosc 1337.01 \ chapter 11计划挑战7 \第11章计划挑战7 \ corporate sales.cpp 24
错误3错误C2371:'DivSales :: qutrSale':重新定义;不同的基本类型h:\ school \ cosc 1337.01 \ chapter 11 program challenge 7 \ chapter 11 program challenge 7 \ corporate sales.cpp 24
错误4错误C2264:'DivSales :: qutrSale':函数定义或声明中的错误;功能不叫h:\ school \ cosc 1337.01 \ chapter 11 program challenge 7 \ chapter 11 program challenge 7 \ corporate sales.cpp 54
答案 0 :(得分:3)
即使我们知道它应该做什么,该代码也无法编译。无论如何我为编译和运行做了一些微小的改动,我希望你可以检查一下这个帮助:
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
class DivSales
{
private:
double quarter [4];
static double DivCorp;
public:
double display(int);
void qutrSale();
};
double DivSales::DivCorp = 0;
void DivSales::qutrSale()
{
int number;
for (number = 0; number < 4; number++)
{
cout << "The sales for the quarter"<< number+1<< ": ";
cin>> quarter [number];
DivCorp += quarter[number];
while (quarter[number] < 0)
{
cout<< "Error, please enter a positive sale amount: $";
cin >> quarter [number];
}
}
}
double DivSales::display(int input)
{
return quarter [input];
}
int main ()
{
int count;
DivSales div[5];
for (count=0; count<5; count++)
{
div [count].qutrSale();
}
return 0;
}
试运行
$ g++ -o test test.c
$ ./test
The sales for the quarter1: 1
The sales for the quarter2: 1
The sales for the quarter3: 1
The sales for the quarter4: 1
The sales for the quarter1: 2
The sales for the quarter2: 2
The sales for the quarter3: 2
The sales for the quarter4: 2
The sales for the quarter1: 3
The sales for the quarter2: 3
The sales for the quarter3: 3
The sales for the quarter4: 3
The sales for the quarter1: 4
The sales for the quarter2: 4
The sales for the quarter3: 4
The sales for the quarter4: 4
The sales for the quarter1: 5
The sales for the quarter2: 5
The sales for the quarter3: 5
The sales for the quarter4: 5
$