数组结构帮助? [英] Array of Struct Help?

查看:75
本文介绍了数组结构帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此对于我的程序,我正在尝试使用结构数组存储来自单独txt的数据列表.文件.我弄清楚了如何在预定的数组大小为100时将每个项目存储到适当的数组中.我创建了一个while循环,以尝试在没有更多数据后阻止将数据读入数组,而是插入一个计数"变量并计算迭代次数,但无济于事.任何人都可以根据下面的代码向我指出循环问题的正确方向(我取出了count变量,因为每当使用它时,它都会弄乱我的输出):


So for my program, I''m trying to use an array of structs to store a list of data from a separate txt. file. I figured out how to store each respective item into the appropriate array when I have a predetermined array size of 100. I created a while loop to try to prevent the data from being read into the arrays after there is no more data, but inserting a "count" variable and counting the number of iterations, but to no avail. Can anyone point me in the right direction of what''s wrong with my loop based on the code below(I took out the count variable because whenever I use it, it messed up my output):


#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;



struct Computer
{
	string Model;
	string Manu;
	int Price;
	string ARRAY1[100];
	string ARRAY2[100];
	int ARRAY3[100];
	
};

void getComputer(Computer&, ifstream&, ofstream&);

int main()
{
	Computer data;

	ifstream iFile;
	ofstream oFile;
	iFile.open("CompSys.txt");
	oFile.open("CompLog.txt");


	getComputer(data, iFile, oFile);
	
	iFile.close();
	oFile.close();
	return 0;


}

void getComputer(Computer& data, ifstream& iFile, ofstream& oFile)//, Computer& alldata)
{

	int Idx;
	
	int counter =100;

	while(iFile)
	{
	for( Idx = 0; Idx < counter; Idx++)
	
	{
	getline(iFile, data.Model, ''\t'');
	data.ARRAY1[Idx] = data.Model;
	
	getline(iFile, data.Manu, ''\t'');
	data.ARRAY2[Idx] = data.Manu;

	iFile >> data.Price;
	data.ARRAY3[Idx] = data.Price;

	}
	data.numItems = Idx;

	}
}




下面是txt. cpp文件读取的内容:




Below is the txt. that the cpp file reads from:

Professional AP500	Compaq	2337
Dimension 4100	Dell	1609
Dimension 8100	Dell	1808
Precision Workstation 620	Dell	2848
AMD Athlon Thunderbird	Explorer Micro	717
Profile 3cx	Gateway	1999
e-Vectra P2024T	Hewlett-Packard	2619
PowerMate VT300	NEC Computers	1160
PCV-RX270DS	Sony	1478
PCV-RX370DS	Sony	1699

推荐答案

您要创建 ^ ](或其他容器).这样,结构就可以保存计算机的单个实例的所有数据,并且只能容纳数据.
请参见 vector :: end() [
You want to create a vector[^] (or some other container) of the struct. This way the struct can hold all the data, and only the data, of a single instance of a Computer.
See vector::end()[^] for an example of how to iterate through all the elements in the container


#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
 
using namespace std;

//This holds the data for a single instance of a computer
struct Computer {
	string Model;
	string Manu;
	int Price;
};

//And this is the type of our collection, a vector of Computers
typedef vector<Computer> ComputerList;
 
void getComputer(Computer&, ifstream&, ofstream&);
 
int main() {
	ComputerList data;
 
	ifstream iFile;
	ofstream oFile;
	iFile.open("CompSys.txt");
	oFile.open("CompLog.txt");

	getComputer(data, iFile, oFile);
	
	iFile.close();
	oFile.close();
	return 0;
}
 
void getComputer(ComputerList& list, ifstream& iFile, ofstream& oFile) {
	int Idx;
	int counter =100;
	while(iFile) {
		for (Idx = 0; Idx < counter; Idx++) {
			Computer data;
			getline(iFile, data.Model, '\t');
			getline(iFile, data.Manu, '\t');
			iFile >> data.Price;
			list.push_back(data); //Add the computer to (the back/end) of the collection
		}
	}
}



这只是我对您的代码所做的更改,我尚未对其进行测试以确保其正常工作.



This is just my changes yo your code, I have not tested it to make sure it works.


除了安德鲁建议的内容:

1.不要将getline()或类似函数与输入流运算符交替使用.由于这些功能的行为不同,因此只会使您的阅读顺序混乱.也许行得通,也许行不通.但如果没有,我保证将很难修复.

我的建议是在不指定分隔符的情况下使用getline()来获取整行,然后将其拆分为单独的标记,看看您是否实际上获得了正确数量的标记(如果没有,则处理错误),以及然后将它们存储在您的数据结构中.

您可以对ifstream进行相同的操作,只是必须注意它是否会吞下换行符(IIRC不会!).如果是这样,则无法测试参数数量,必须希望整个文件格式正确且正确.如果不是,则必须测试它的存在并将其从输入字符串中删除.

2.摆脱掉for循环.如果从解决方案1中建议的变体Andrew开始,则只需删除for循环的开始和结束行.
In addition to what Andrew suggested:

1. don''t mix getline() or similar functions alternating with input stream operators. It will just mess up your reading order since these functions behave differently. Maybe it works, maybe it doesn''t. but if it doesn''t, I promise it will be very tough to fix.

My suggestion is to use getline() without specifying a separator to get the entire line, then split it into individual tokens, see if you actually get the right number of tokens (and deal with the error if you don''t), and then store them in your data structure.

You could do the same with an ifstream, only that you must be careful whether or not it will swallow the newline character (IIRC it doesn''t!). If it does, you cannot test the number of arguments and must hope that the entire file is well-formatted and correct. If it doesn''t you must test for it''s presence and cut it from your input string.

2. Get rid of that for loop. If you start with the variant Andrew suggested in solution 1, then you can just erase the start and end lines of the for loop.


这篇关于数组结构帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆