[英]Structs and External binary files to store data
Alright so I have an assignment that I have been completely stumped on. 好吧,所以我有一份完全被我遗忘的作业 。 My code thus far is as follows
到目前为止,我的代码如下
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<sstream>
using namespace std;
const int SIZE = 12;
struct Division
{
char divName[SIZE]; // Division name
double sales[4]; // Quarterly sales stored as an array
};
int main()
{
void Intro();
Division CreateCorporateFile();
Division DisplayCorporateSales();
Intro();
CreateCorporateFile();
DisplayCorporateSales();
system("PAUSE");
return 0;
}
void Intro()
{
cout<<"This program will prompt you to enter in quarterly sales for "
"four different\ndivisions of a company.\n\n";
}
Division CreateCorporateFile()
{
Division div;
int x = 0;
//for(int x = 0; x < 4; x++)
do {
int quarter = 1;
cout << "Enter the name of the division: ";
cin >> div.divName;
for(int i = 0; i < 4; i++)
{
cout << "Enter in the sales for quarter "<< quarter <<": ";
cin >> div.sales[i];
if(div.sales[i] > 0)
{
quarter++;
}
else
{
cout << "Sales are not allowed to be negative.\n";
}
}
x++;
} while(x < 4);
return div;
}
Division DisplayCorporateSales()
{
Division test;
Division CreateCorporateFile();
test = CreateCorporateFile();
for(int i = 0; i < 4; i++)
{
cout << "Here are the quarterly sales for " << test.divName
<< ": ";
for(int i = 0, quarter = 1; i < 4; ++i, ++quarter)
{
cout << "Quarter "<< quarter << " sales: $"<< test.sales[i]
<<"\n";
}
}
return test;
}
One of the problems that I am running in to is that it ends up prompting for 8 divisions (should only do it 4 times), and another is that it ends up only displaying the 8th divisions data. 我遇到的问题之一是,它最终会提示输入8个分区(应该只进行4次),另一个问题是,它最终只会显示第8个分区的数据。 So anybody see where I am going wrong?
所以有人看到我要去哪里错了吗? Because I do not, and I have been working on it for hours so I am probably overlooking obvious details.
因为我不这样做,而且我已经工作了几个小时,所以我可能忽略了明显的细节。
The reason you only display data for the final Division
is that CreateCorporateFile
only returns a single Division
instance. 您仅显示最终
Division
数据的原因是CreateCorporateFile
仅返回单个Division
实例。 Each do-while loop overwrites the contents of the previous loop. 每个do-while循环都会覆盖前一个循环的内容。
One solution to this would be to return an array instead 一种解决方案是返回一个数组
std::vector<Division> CreateCorporateFile()
{
std::vector<Division> divArray;
Division div;
int x = 0;
do {
...
divArray.push_back(div);
x++;
} while(x < 4);
return divArray;
}
void DisplayCorporateSales()
{
std::vector<Division> divisions = CreateCorporateFile();
for (size_t i=0; i<divisions.size(); i++)
{
Division div = divisions[i];
cout << "Here are the quarterly sales for " << div.divName
<< ": ";
for(int j = 0, quarter = 1; j < 4; ++j, ++quarter)
{
cout << "Quarter "<< quarter << " sales: $"<< div.sales[j]
<<"\n";
}
}
}
The first problem is caused by you declaring the functions you are going to use inside main rather than before it in global scope: void Intro(); 第一个问题是由于您在main范围内而不是在全局范围内声明要使用的函数而引起的:void Intro(); Division CreateCorporateFile();
除法CreateCorporateFile(); Division DisplayCorporateSales();
Division DisplayCorporateSales();
The second problem is caused by the fact that you have used the variable i
twice for each of your loops. 第二个问题是由于您在每个循环中两次使用了变量
i
而导致的。 You need to use a different variable name (or just use quarter
as a loop variable). 您需要使用其他变量名(或仅使用
quarter
作为循环变量)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.