I am a CS1 student and have had no issues until this point in the class really. I have been working on this for awhile now and cannot figure out how to get this to work!

I am supposed to use this code and modify it to #1 - open a .DAT file (which is just a comma delimited file with about 15 numbers) #2 - import the numbers from the file #3 - Sort the numbers

We are supposed to keep with the existing code using vectors but I just cannot get this to function properly...this is my very first post here so be easy with me lol

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i,int j) { return (i<j); }

struct myclass {
  bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
    ifstream file ( "DATA.DAT" );
    string temp;
    vector<int> myvector;
    vector<int>::iterator it;

        file.open("DATA.DAT");  //open data file
        while(!file.eof())
        {
            getline(file, temp);
            cout << temp << '\n';

            myvector.push_back(temp);
        }
            file.close();

  // using default comparison (operator <):
  sort (myvector.begin(), myvector.begin()+4);

  // using function as comp
  sort (myvector.begin()+4, myvector.end(), myfunction);

  // using object as comp
  sort (myvector.begin(), myvector.end(), myobject);

  // print out content:
  cout << "The file contains:";
  for (it=myvector.begin(); it!=myvector.end(); ++it)
    cout << " " << *it;

  cout << endl;

  return 0;
}

I know that there are a few lines that are probably way off, but I am pulling my hair out because everything I have tried hasn't worked...any help would be greatly appreciated.

It is trying to push a string to a vector of integers. Won't work. What you have to do is read the file as integers, not strings. There is a couple ways to do it.

The easiest (IMO) is to use getline() as you have it now, but add the third parameter which is the deliminator. Since the file you are reading is comma-deliminated the third parameter would be ','. The after it is read convert it to int and push that onto the vector.

Thank you for the assistance! I was able to get it working with your help and the help on another board. Here is the code which is compiling correctly for me!

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;

struct myclass {
  bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
    string temp;
    vector<int> myvector;
    vector<int>::iterator it;

    ifstream file ( "DATA.DAT" , ifstream::in );
        while (file .good())
        {
            getline(file , temp , ',');
            myvector.push_back(atoi(temp.c_str()));
        }
        file .close();

  // using object as comp
  sort (myvector.begin(), myvector.end(), myobject);

  // print out content:
  cout << "The file contains:";
  for (it=myvector.begin(); it!=myvector.end(); ++it)
    cout << " " << *it;

  cout << endl;

  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.