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.
//main
#include "numlist.cc"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
usingnamespace 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"
usingnamespace 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";
elsefor(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];
elsereturn -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:
staticconst 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
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++;
}
}
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)