gpt4 book ai didi

C++如何从基本文本文件中读取文本并将其存储到两个不同的数组中

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:42:29 25 4
gpt4 key购买 nike

我的计算机科学实验室出现了一些问题。此作业的目标是从另一个程序创建的文本文件中读取文本,该程序在一行中显示如下。

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),二维数组是一个具有 6 行和 4 列的 int 数组将持有每个公司每个季度的利润。因此,为了更好地展示我的意思,这里是它在表格中的样子。

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

下面是我的程序中遇到问题的代码块。我删除了与问题无关的 block ,例如数组的显示和我知道在使用初始化数组测试后可以工作的搜索索引。我对 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);
}
}
}
}

关于C++如何从基本文本文件中读取文本并将其存储到两个不同的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26434867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com