- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的计算机科学实验室出现了一些问题。此作业的目标是从另一个程序创建的文本文件中读取文本,该程序在一行中显示如下。
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/
我有 table 像这样 -------------------------------------------- id size title priority
我的应用在不同的 Activity (4 个 Activity )中仅包含横幅广告。所以我的疑问是, 我可以对所有横幅广告使用一个广告单元 ID 吗? 或者 每个 Activity 使用不同的广告单元
我有任意(但统一)数字列表的任意列表。 (它们是 n 空间中 bin 的边界坐标,我想绘制其角,但这并不重要。)我想生成所有可能组合的列表。所以:[[1,2], [3,4],[5,6]] 产生 [[1
我刚刚在学校开始学习 Java,正在尝试自定义控件和图形。我目前正在研究图案锁,一开始一切都很好,但突然间它绘制不正确。我确实更改了一些代码,但是当我看到错误时,我立即将其更改回来(撤消,ftw),但
在获取 Distinct 的 Count 时,我在使用 Group By With Rollup 时遇到了一个小问题。 问题是 Rollup 摘要只是所有分组中 Distinct 值的总数,而不是所有
这不起作用: select count(distinct colA, colB) from mytable 我知道我可以通过双选来简单地解决这个问题。 select count(*) from (
这个问题在这里已经有了答案: JavaScript regex whitespace characters (5 个回答) 2年前关闭。 你能解释一下为什么我会得到 false比较 text ===
这个问题已经有答案了: 奥 git _a (56 个回答) 已关闭 9 年前。 我被要求用 Javascript 编写一个函数 sortByFoo 来正确响应此测试: // Does not cras
所以,我不得不说,SQL 是迄今为止我作为开发人员最薄弱的一面。也许我想要完成的事情很简单。我有这样的东西(这不是真正的模型,但为了使其易于理解而不浪费太多时间解释它,我想出了一个完全模仿我必须使用的
这个问题在这里已经有了答案: How does the "this" keyword work? (22 个回答) 3年前关闭。 简而言之:为什么在使用 Objects 时,直接调用的函数和通过引用传
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: what is the difference between (.) dot operator and (-
我真的不明白这里发生了什么但是: 当我这样做时: colorIndex += len - stopPos; for(int m = 0; m < len - stopPos; m++) { c
思考 MySQL 中的 Group By 函数的最佳方式是什么? 我正在编写一个 MySQL 查询,通过 ODBC 连接在 Excel 的数据透视表中提取数据,以便用户可以轻松访问数据。 例如,我有:
我想要的SQL是这样的: SELECT week_no, type, SELECT count(distinct user_id) FROM group WHERE pts > 0 FROM bas
商店表: +--+-------+--------+ |id|name |date | +--+-------+--------+ |1 |x |Ma
对于 chrome 和 ff,当涉及到可怕的 ie 时,这个脚本工作完美。有问题 function getY(oElement) { var curtop = 0; if (oElem
我现在无法提供代码,因为我目前正在脑海中研究这个想法并在互联网上四处乱逛。 我了解了进程间通信和使用共享内存在进程之间共享数据(特别是结构)。 但是,在对保存在不同 .c 文件中的程序使用 fork(
我想在用户集合中使用不同的功能。在 mongo shell 中,我可以像下面这样使用: db.users.distinct("name"); 其中名称是用于区分的集合字段。 同样我想要,在 C
List nastava_izvjestaj = new List(); var data_context = new DataEvidencijaDataContext();
我的 Rails 应用程序中有 Ransack 搜索和 Foundation,本地 css 渲染正常,而生产中的同一个应用程序有一个怪癖: 应用程序中的其他内容完全相同。 我在 Chrome 和 Sa
我是一名优秀的程序员,十分优秀!