Read numbers from file to Array quick question

Basically write a removeall function, use it to remove items from an array you load from textfile.
Ive tested this code and its working except for the getline which ive commented about in the source below.
If anyone could provide me with the correct way to getlines from a textfile id appreciate it.



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
// ch10_pe5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int RemoveItem (int list[], int size, int ele);

int _tmain(int argc, _TCHAR* argv[])
{
	int list[20],i,element,n;
	string line;
	ifstream myfile ("Ch10_Ex5-7Data.txt");

	n=0;
	i=0;

    if (!myfile)
    {
        cout << "NO INPUT FILE" << endl;
		system("PAUSE");
        return 1;
    }

	 while(!myfile.eof()){
            getline(myfile,line);
			list[i] = atoi(line.c_str());
            cout << line[i] << endl;
            i++;
	 }

  cout<<"\nElements in Array:" << list;


	cout<<"\nEnter element to be removed:";
	cin>>element;
	n=RemoveItem(list,n,element);
	cout<<"\nElements after removing:";
	for(i=0;i<n;i++)
	cout<<list[i];
	system("PAUSE");
}


int RemoveItem(int list[],int size, int ele)
{
	int loc ,i;
	bool found=false;

	for(loc=0;loc<size;loc++)
	{
		found=false;
			if(list[loc]==ele)
			{
				found=true;
			}
		if(found)
		{
			//remove all elements
			for (i=loc;i<size;i++)
					list[i]=list[i+1];
				size=size-1;
		}
	}
	return size;

}




infile:

76
89
150
135
200
12
100
28
178
189
167
134
175
150
87
99
129
149
176
35

Given int list[20] and a file with exactly 20 lines, your loop

1
2
3
4
while(!myfile.eof()) {
     getline(myfile,line);
     list[i] = atoi(line.c_str());
...

executes 21 times and writes past the end of the array.

The correct loop is
1
2
3
while( getline(myfile,line) ) {
     list[i] = atoi(line.c_str());
...


or, since you have a fixed-size array instead of a vector,

1
2
3
while( i < 20 && getline(myfile,line) ) {
     list[i] = atoi(line.c_str());
...

Last edited on
Thanks that seems like a step in the right direction, I still have errors while compiling though..
Debug Assertion Failed!
Expression: string subscript out of range

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
// ch10_pe5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int RemoveItem (int list[], int size, int ele);

int _tmain(int argc, _TCHAR* argv[])
{
	int list[25],i,element,n;
	string line;
	n=0;
	i=0;

	ifstream myfile ("Ch10_Ex5-7Data.txt");
    if (!myfile)
    {
        cout << "NO INPUT FILE" << endl;
		system("PAUSE");
        return 1;
    }

	while( getline(myfile,line) ) {
		 list[i] = atoi(line.c_str());
            i++;
	 }

  cout<<"\nElements in Array:" << list;


	cout<<"\nEnter element to be removed:";
	cin>>element;
	n=RemoveItem(list,n,element);
	cout<<"\nElements after removing:";
	for(i=0;i<n;i++)
	cout<<list[i];
	system("PAUSE");
}


int RemoveItem(int list[],int size, int ele)
{
	int loc ,i;
	bool found=false;

	for(loc=0;loc<size;loc++)
	{
		found=false;
			if(list[loc]==ele)
			{
				found=true;
			}
		if(found)
		{
			//remove all elements
			for (i=loc;i<size;i++)
					list[i]=list[i+1];
				size=size-1;
		}
	}
	return size;

}
If size is 25 and i == 24 then...
1
2
for (i=loc;i<size;i++)
    list[i]=list[i+1]; //24 + 1 = 25, and list[25] is out of range since elements are [0..24] 
Topic archived. No new replies allowed.