我的计算机科学实验室遇到了一些问题。分配的目的是从另一个程序创建的文本文件中读取文本,该文件在一行上显示如下。
ABC DEF GHI JKL MNO PQR 40000 50000 60000 70000 35000 45000 55000 65000 25000 26000 27000 28000 31000 32000 33000 34000 42000 43000 44000 45000 10000 20000 30000 40000
然后分配给两个不同的数组。一维数组和一维数组。一维数组是一个大小为6的字符串数组,它将保存公司的名称(ABC,DEF,GHI,JKL,MNO和PQR),而二维数组是一个int数组,具有6行4列,将保留每个公司在每个季度中获得的利润。因此,为了更好地说明我的意思,这就是表格中的内容。
ABC 40000 50000 60000 70000
DEF 35000 45000 55000 65000
GHI 25000 26000 27000 28000
JKL 31000 32000 33000 34000
MNO 42000 43000 44000 45000
PQR 10000 20000 30000 40000
以下是我遇到问题的程序中的代码块。我删除了与问题无关的数据块,例如数组的显示和搜索索引,在对初始化的数组进行测试之后,我知道它们可以工作。我对C ++和编码仍然还是很陌生,所以我使用了一些相当粗糙的方法来尝试使它起作用。我事先表示歉意。另外,我不是要你为我做这个。我只是想获得一些帮助,以了解如何使它正常工作,尤其是公然错误的部分。
注意:这些功能需要分开分配,并且只能有两个。因此,我无法简单地将它们放在一个函数中。
编辑:忘记添加代码失败了。当程序尝试读取文件并将数据分配给相应的数组时,出现了问题。包含公司名称的一维数组未获取任何信息,而包含利润的二维数组都相同,其值为-9.25596e + 061
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
const int COMPANY_AMOUNT = 6, AMOUNT_OF_QUARTERS = 4;
void ReadData(int, int, int, string&, int&);
int main()
{
int x = 0, // current position of the array
y = 0, // current position of the array
Array; // Used to differentiate which array is going to be worked on
string Company[COMPANY_AMOUNT]; // Array for Company names
string Name1; // Holds the name of the company as a reference variable when sent to the ReadData function.
int Name2; // Holds the money amount for the company quarter as a reference variable when sent to the ReadData function.
double CompanySales[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS]; // Array for Company profits
for (x = 0; x < COMPANY_AMOUNT; x++)
{
Array = 0; // set to 0 and passed to ReadData to let it know that the Company[] array is being worked on.
ReadData(x, y, Array, Name1, Name2);
Name1 = Company[x] ;
}
for (x = 0; x < COMPANY_AMOUNT; x++)
{
for (y = 0; y < AMOUNT_OF_QUARTERS; y++)
{
Array = 1; // set to 1 and passed to ReadData to let it know that CompanySales[][] array is being worked on.
ReadData(x,y,Array, Name1, Name2);
Name2 = CompanySales[x][y];
}
}
}
void ReadData(int x, int y, int Array, string& Name1, int& Name2)
{
// My goal here was to create the array's everytime ReadData is called on and then it would send back whatever was in the [x] or [x][y] spot through the Name1 or Name2 reference variables.
int value = 0, CompanySales[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS], a, b, cash, number;
string value1, Company[COMPANY_AMOUNT], name;
ifstream inputFile;
inputFile.open("C:\\TEMP\\Data.txt");
if (inputFile.fail())
{
cout << "Error opening file.\n";
system("Pause");
exit(PROGRAM_FAILURE)
}
else
{
while (inputFile >> number) // This is the section where I have trouble. It doesn't read properly and gives me awkward outputs later on.
{
for (a = 0; a < COMPANY_AMOUNT; a++)
{
inputFile >> name;
name = Company[a];
}
for (a = 0; a < COMPANY_AMOUNT; a++) // Company_Amount = 6
{
for (b = 0; b < AMOUNT_OF_QUARTERS; b++) // Amount_of_Quarters = 4
{
inputFile >> cash;
cash = CompanySales[a][b];
}
}
inputFile.close();
if (Array == 0)
{
Company[x] = Name1;
}
else if (Array == 1)
{
Company[x][y] = Name2;
}
}
}
}
您的代码比要完成的任务要复杂得多。您的ReadData()
函数试图做太多事情,这会使其余的代码复杂化。摆脱它,然后可以将代码简化为以下内容:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
const int COMPANY_AMOUNT = 6, AMOUNT_OF_QUARTERS = 4;
int main()
{
std::string Company[COMPANY_AMOUNT]; // Array for Company names
double CompanySales[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS]; // Array for Company profits
std::ifstream inputFile("C:\\TEMP\\Data.txt");
if (!inputFile)
{
std::cout << "Error opening file.\n";
system("Pause");
return PROGRAM_FAILURE;
}
for (int x = 0; x < COMPANY_AMOUNT; x++)
{
inputFile >> std::ws >> Company[x];
if (!inputFile)
{
std::cout << "Error reading file.\n";
system("Pause");
return PROGRAM_FAILURE;
}
if (inputFile.eof())
{
std::cout << "Unexpected EOF while reading file.\n";
system("Pause");
return PROGRAM_FAILURE;
}
}
for (int x = 0; x < COMPANY_AMOUNT; x++)
{
for (int y = 0; y < AMOUNT_OF_QUARTERS; y++)
{
inputFile >> std::ws >> CompanySales[x][y];
if (!inputFile)
{
std::cout << "Error reading file.\n";
system("Pause");
return PROGRAM_FAILURE;
}
}
}
inputFile.close();
// use arrays as neded...
return 0;
}
如果您绝对必须具有ReadData()
功能,则需要重新设计它,例如:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
const int COMPANY_AMOUNT = 6, AMOUNT_OF_QUARTERS = 4;
void ReadData(std::string*, double[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS]);
int main()
{
std::string Company[COMPANY_AMOUNT]; // Array for Company names
double CompanySales[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS]; // Array for Company profits
ReadData(Company, CompanySales);
// use arrays as neded...
return 0;
}
void ReadData(std::string* Names, double Sales[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS])
{
std::ifstream inputFile("C:\\TEMP\\Data.txt");
if (!inputFile)
{
std::cout << "Error opening file.\n";
system("Pause");
exit(PROGRAM_FAILURE);
}
for (int x = 0; x < COMPANY_AMOUNT; x++)
{
inputFile >> std::ws >> Names[x];
if (!inputFile)
{
std::cout << "Error reading file.\n";
system("Pause");
exit(PROGRAM_FAILURE);
}
if (inputFile.eof())
{
std::cout << "Unexpected EOF while reading file.\n";
system("Pause");
exit(PROGRAM_FAILURE);
}
}
for (int x = 0; x < COMPANY_AMOUNT; x++)
{
for (int y = 0; y < AMOUNT_OF_QUARTERS; y++)
{
inputFile >> std::ws >> Sales[x][y];
if (!inputFile)
{
std::cout << "Error reading file.\n";
system("Pause");
exit(PROGRAM_FAILURE);
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句