input file and an array

I have the next set of data:
id sold left
619847GBE 641 998
418712IMB 107 867
227451GEM 789 181
981836KEA 747 171
986516IGU 303 71
501024BMU 895 743

and I'm trying to read it into 1 array to later sort it but I can't figure out how to make it into that one array so far I declared the class

1
2
3
4
5
6
7
8
9
const int inventory = 100
class Item
{
public:
	string id;
	int left, sold;
	char a[inventory];
	
};


I'm not sure if I declared the array right.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <fstream>
#include <string>
using namespace std;
int main ()
{
     string sInput;
     fstream file;
     file.open ("Text.txt");
     file >> sInput;
     file.close ();
     return 0;
}


sInput is a string (close to the same thing as a char array) that holds all the text in the file "Text.txt" (use what ever file you want though.) You can use it like an array like this:

 
if (sInput [10] == 'g') {do something}
I made an error in the class definition I wasn't suppose to make it a class object but make the class objects go into an array of 100 I started doing this so far in main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
	ifstream file;
	file.open("inventory.txt");
	
	Item a[ivt];
	
	for ( int i = 0; i < ivt; i++)
	{
		file >> a[i].id;
		file >> a[i].sold;
		file >> a[i].left;
	}

	for (int i = 0; i < ivt; i++)
	{
		cout << a[i].id;
	}

	
}


ivt being the const inventory just made it shorter

I was trying to see if my file input went in but no dice when I tried printing it to the screen I'm not sure what I did wrong there

I tested out each of the three objects id gives me nothing, sold and left give me -85899 and so on

I'm thinking that I have to use the getline thing but I have no idea how it works
Last edited on
Topic archived. No new replies allowed.