What is wrong with this?

Please see 4th postfor information. (3rd reply)
Last edited on
I compiled the code myself to see where the error was and after indenting, and making it readable and I got an error on line 103.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

//Items
const int items(2);
int inv[items];
string iteminfo[items][2];

//Test variables
int i, ii, iii;

//Reusable functions
void clrsc()
{
	cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
}
void blanks(int a)
{
	for(i;i<=a;i++)cout << "\n";
}

//Files
ofstream ofile;
ifstream itemfile;
size_t semicolon;
size_t pos;
string line, line2, line3;
int linecount(0);
int itemsetup();

int main()
{
	i = itemsetup();

	if (i == 1)
	{
		cout << "Failed to load items. Application shall now exit. ";
		system("PAUSE");
		return 1;
	}

	system("PAUSE");
	return 0;
}


int itemsetup()
{
	itemfile.open("Items.txt");

	//If file is open, read it.
	if (itemfile.is_open())
	{
		//Count lines in the file so we know how many lines to read.
		while(! itemfile.eof())
		{
			getline(itemfile,line);
			linecount++;
		}

		itemfile.seekg(0);

		for(i=1;i<=linecount;i++)
		{
			//Check the line is not excluded. A comma at beginning of line signifies to ignore line.
			getline(itemfile,line);
			semicolon = line.find(",");
			
			if (semicolon != 0)
			{
				//Find which item to enter info for.
				pos = line.find(",");
				line2 = line.substr(pos);
				line.erase(pos);
				stringstream(line) >> ii;
				//
				line2.erase(0,2);
				pos = line2.find(",");
				line = line2.substr(pos);
				line2.erase(pos);
				iteminfo[ii][0] = line2;
				cout << iteminfo[ii][0];
				//
				line.erase(0,2);
				pos = line.find(",");
				line2 = line.substr(pos);
				line.erase(pos);
				iteminfo[ii][1] = line;
				//
				line2.erase(0,2);
				pos = line2.find(",");
				line = line2.substr(pos);
				line2.erase(pos);
				iteminfo[ii][2] = line2;
				//
				line.erase(0,2);
				pos = line.find(",");
				line2 = line.substr(pos);
				line.erase(pos);
				iteminfo[ii][3] = line;
				//
				line2.erase(0,2);
				pos = line2.find(",");
				line = line2.substr(pos);
				line2.erase(pos);
				iteminfo[ii][4] = line2;
			}
		}

		itemfile.close();

	}
	else 
	{
		return 1; //If couldn't open return error message.
	}

	return 0;
}
Last edited on
NVM
Last edited on
I'm going to start again and try and include as much info as possible.

Okay. My program should read data from items.txt and save it into an array named iteminfo. Iteminfo has two dimensions, iteminfo[a] holds the item number, for instance 1. In iteminfo[a][b] b holds the data. For instance: iteminfo[1][0] holds the name of item 1, iteminfo[45][3] holds the description of item 45.

Here is my code in main.cpp, without irrelevant code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

//Items
const int items(2);
int inv[items];
string iteminfo[items][5];
//Test variables
int i, ii, iii;
//Stats
int vcom(0);
int icom(0);
//Files
ofstream ofile;
ifstream itemfile;
size_t semicolon;
size_t pos;
string line, line2, line3, line4, line5;
int linecount(0);
int itemsetup();

int main()
{
i = itemsetup();
if (i == 1){cout << "Failed to load items. Application shall now exit. "; system("PAUSE"); return 1;} 
system("PAUSE"); return 0;}


int itemsetup()
{
itemfile.open("Items.txt");
//If file is open, read it.
if (itemfile.is_open())
	{
		while(!itemfile.eof())
		{
			//Check the line is not excluded.
			getline(itemfile,line);
			semicolon = line.find(";");
			if (semicolon != 0)
			{
				//Find which item to enter info for.
				pos = line.find(",");
				line2 = line.substr(pos);	
				line.erase(pos);
				stringstream(line) >> ii;
				for (i=0;i<=3;i++)
				{
					//
					line = line2;
					line.erase(0,2);
					pos = line.find(",");
					line2 = line.substr(pos);
					line.erase(pos);
					iteminfo[ii][i] = line;
				}
			}
		}
		itemfile.close();
	}
else return 1; //If couldn't open return error message.
//Display all information from file as a test.
for(int i=1;i<=items;i++)
	{
	cout << "Item " << i <<": is " << iteminfo[i][0] << endl;	
	}
return 0;
}


in items.txt i have:
1, fish, gdfg, fgdfg, fgdfg,
2, pasta, klk, sd, gfssferer,


i get an error:

Debug assertion failed.

File:f:\dd...memcopy_s.c

Expression:sizeInBytes >= count


but when I reduce the amount of information to take from items.txt to the first three fields(1, fish, gdfg, and 2, pasta, klk,) I don't get an error and it outputs correctly.

I aplogize for weird formatting, the [i] in iteminfo[i] in my code is recognized as the BB code for italics...
Last edited on
line 58: iteminfo[ii][i] = line;(that could be formatting fault)
loop at line 66 (? Can't understand well the line number...):
1
2
3
4
5
6
7
for(int i=0;i<items;i++)//array indeces start from 0
{
    cout << "Item " << i <<": is ";
    for (int j=0; j<5; j++)
       cout << iteminfo[i][j] << ' ';//display all the contents
    cout << endl;
}

In your file make indeces starting from 0 or at line 58 use iteminfo[ii-1][i];

Why don't you use vectors instead aof array, so will be able to grow them when reading from the file
Last edited on
what do you mean grow them?

also my compiler has been deleted off the computer again so i cannot test that code

Edit: in my code I have iteminfo[ii](i] but the i is used to turn line into italics so it didn't appear

instead of changing my code so items start at 0, I will just add an item 0 to my items file.
Last edited on
Keep in mind that arrays are zero based, meaning the first element is 0, and the last element is arraysize-1.

If you create an array of 10 elements, those element indices will be 0-9. If you try using 1-10, you will get an error accessing the 10th element.

And what Bazzy means by growing is that a vector will automatically resize to accomodate the number of elements in your text file. In your code, your array can only hold 2 items, so if your text file contains more than 2 items, you're going to get another error. A vector will continue to grow in size as you add items to it. If your text file contains 10 items, you will end up with a vector containing 10 items. If your text file contains 50 items, you will end up with a vector containing 50 items. Your code isn't hard coded to contain a certain number of items.
Is it possible to have a 2 dimensional vector though? (I didn't see anything about it in the documentation
Last edited on
closed account (z05DSL3A)
You have a Vector of Vectors... vector< vector<int> >
okay thanks to everyone who help me, I now have the array code working but i'm going to change it to vectors

thanks again
Just adding that in c++ vectors are better containers than using an array.. like someone said, they grow dynamically and have a shed load of functions to make manipulation of the data easy.

But the kicker is that they are really easy to understand and start using them immeadiately. Since i learned about vectors - i never used c style arrays again!
Topic archived. No new replies allowed.