I need help with the "void NumList::load_from_file(istream& ins)". Data isn't being inputted in the private array.

I am having problems with the "void NumList::load_from_file(istream& ins)" function. My program compiles, and prints to an output file but does not look at all like it should. Goal of this constructor is to sort a file with many number largest to smallest. I used the see_all() function I created and the array does not have the correct integers in 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//main
#include "numlist.cc"
#include <iostream>
#include <cstdlib> 
#include <iomanip>
#include <fstream>

using namespace std;

int main(){
	NumList opp;
	string filename, outfile;
	cout <<"Please enter the file name you'd like to use: ";
	cin >> filename; // gets the filename
	ifstream ins;
	ofstream outs;
	
	
	ins.open(filename.c_str()); //opens the file that user would like to use.
	if (ins.fail()){
		cout << "\Error!";
		ins.close();
	}
	opp.load_from_file(ins);//gets info from file to the array;
	ins.close();

	opp.b_sort();//Sorts the array
	
	int one,two,three;
	
	cout <<"Please enter three numbers that you would like to insert: ";
	cin >> one;
	cin >> two;
	cin >> three;
	
	//inserts the numbers from the user to the array.
	opp.insert(one);
	opp.insert(two);
	opp.insert(three);
	opp.b_sort();//Sorts the array
	
	
	cout << "Please enter the outfile you'd like to use: \n";
	cin >> outfile;
	outs.open(outfile.c_str()); //opens the file that user would like to use.
	if (outs.fail()){
		cout << "\Error!";
		outs.close();
	}
	opp.save_to_file(outs); //prints the sorted array to the output file
	outs.close();
	
	
	return 0;
}


//nulist.cc******************************************************************

#include "numlist.h"
using namespace std;
 
// Constructor
NumList::NumList(){
	used = 0;
}

void NumList::insert(int num){
	if(used <CAPACITY){
		data[used] = num;
		used++;
	}
	else{
		cout<<"Error. List capacity has been reached.\n";
	}
}

void NumList::load_from_file(istream& ins){
// The implementation of this function is left to the student
	int j;
	ins >> j;
	while(!ins.eof()){
		data[used] = j;
		ins >> j;
		used++;
	}
	
}


void NumList::save_to_file(ostream& outs){
// The implementation of this function is left to the student
	for( int i=0; i<used; i++){
		outs << data[i] << endl;
	}
}

void NumList::see_all()const{
	if(used == 0)
	    cout<<"Empty list.\n";
	else
	    for(size_t i = 0; i<used; ++i)
		cout<<data[i]<<endl;
}

int NumList::get_item(size_t index)const{
	if(index < used)
		return data[index];
	else
		return -1;
}
	
void NumList::b_sort(){
    bool done = false;
    int j,i;
    int tmp;
    while(!done){
		for(i=1; i<=CAPACITY; i++){
			done = !true;
			for(j=used-1; j>= 0; --j){
				if(data[j+1] > data[j]){
					done = false;
					tmp = data[j];
					data[j] = data[j+1];
					data[j+1] = tmp;
				}
			}
		}
		done = true;
    }
}

//numlist.h*****************************************************************
#ifndef NUMLIST_H
#define NUMLIST_H
#include <iostream>
#include <cstdlib>
#include <cmath>

class NumList{
    public:
	static const size_t CAPACITY = 100; 
	
	// Default constructor
	NumList();

	// Precondition: none
	// Postcondition: num is placed at the end of the list, 
	//               if there is sufficient Capacity
	void insert(int num);
	size_t size()const {return used;}

	//Precondition: istream is a stream that has already been connected to 
	//              a successfully opened file
	// Postcondition: integers from the file are loaded into the object
	void load_from_file(std::istream& ins);

	// Preconditon: ostream is connected to a successfully open file
	// Postconditon numbers in the object are written to the file
	void save_to_file(std::ostream& outs);
	void b_sort();
	int get_item(size_t index)const;
	void see_all()const;
	
    private:
	int data[CAPACITY];
	size_t used;
};

#endif 
Last edited on
Hey, please edit your post and use code tags for all of your code, to make it readable - http://www.cplusplus.com/articles/jEywvCM9/
Sorry, t'was my first post.
looping on .eof is considered bad, you can google that for more information. Try changing the function to this and see if it helps -

1
2
3
4
5
6
7
8
9
void NumList::load_from_file(istream& ins){
// The implementation of this function is left to the student
	int j;
	while(ins >> j){
		data[used] = j;
		used++;
	}
	
}
Last edited on
It's still giving me only 3 outputs in the output file, and none of which are in the input file.

This is what's in the input file I need to sort.
larger.dat

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
16839
5759
10114
17516
31052
5628
23011
7420
16213
4087
2750
12768
9085
12061
32226
17544
25090
21184
25138
25567
26967
4979
20496
10312
11368
30055
17032
13146
19883
25737
30525
28506
28395
22103
24852
19068
12755
11654
6562
27097
13629
15189
32086
4144
6968
31407
24166
13404
25563
24835
31354
921
10445
24804
7963
19319
1423
31328
10458
1946
14480
29984
18752
3895
18671
8260
16249
7758
15630
13307
28607
13991
11739
12517
1415
5263
17117
22826
3182
13135
25344
8023
11234
7537
9761
9980
29072
1202
21337
13062
22161
24006
30730
7645
27476
31694
25515


My output file only recieves:
1
2
3
134511648
32226
32086


when run. No idea how or why it gets these numbers.
Last edited on
Could you post your input file? You can just edit your comment before this one and put it there.
Last edited on
you need to learn to test.
Now you are doing 4 operations
- reading from file
- sorting
- inserting
- writing to file
if you are going to test the reading, then there is no need to sort or insert, or even write to file (use a debugger to print your variable)

1
2
3
//in b_sort
			for(j=used-1; j>= 0; --j){
				if(data[j+1] > data[j]){
¿is data[used] valid?
Topic archived. No new replies allowed.