C++ programming

I have a input file (.txt). It has a list of books in the format:
catalogue_number, author_last_name,author_first_name, book_title, genre, availabilty
* The catalogue is 4 characters long
It needs to sort the author_last_name from the input file using array and output as an .txt file.
HOW DO I DO THIS???? I'M VERY CONFUSED.
Very typical girls request...

I'm sorry but no one is gonna do your homework. Start by reading this:

http://www.cplusplus.com/doc/tutorial/files/

And if you need help, we'll be very glad to provide you all the help you need.
I already posted what I did so far earlier.
#include <iostream>
#include <iterator>
#include <string>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;
class SBooks {
private:
string catalogue_number;
string author_first_name;
string author_last_name;
string book_title;
string strGenre;
string strAvail;
public:
friend bool comp(const SBooks& a, const SBooks& b)
{
return a.author_last_name < b.author_last_name ? true : false;
}

friend ostream& operator<<(ostream& os, const SBooks& s)
{
os << setw(6) << s.catalogue_number << setw(15) << s.author_first_name << setw(15) << s.author_last_name << setw(20) << s.book_title << setw(10) << s.strGenre << setw(14) << s.strAvail;
return os;
}
friend istream& operator>>(istream& is, SBooks& s)
{
is >> s.catalogue_number >> s.author_last_name >> s.author_first_name >> s.book_title >> s.strGenre >> s.strAvail;
return is;
}
};

bool comp(const SBooks& a, const SBooks& b);

int main()
{
const string strFile = "input2.txt";
ifstream is(strFile.c_str());
vector<SBooks> vInput;
// This copies SBook objects into a vector from file input.txt
copy(istream_iterator<SBooks>(is), istream_iterator<SBooks>(), back_inserter(vInput));
cout << "Read in records unsorted: \n";
// This prints the read in output to the screen.
copy(vInput.begin(), vInput.end(), ostream_iterator<SBooks>(cout, "\n"));
// This performs the sorting
sort(vInput.begin(), vInput.end(), comp);
// This prints the sorted output to the screen.
cout << "Sorted: \n";
copy(vInput.begin(), vInput.end(), ostream_iterator<SBooks>(cout, "\n"));
return 0;
}
I have to sort with arrays rather than vector and input the .txt and output a .txt with the sort.
Use std::sort on the vector (or array) with a custom comparison function that sorts by last name.
See http://www.cplusplus.com/reference/algorithm/sort/

Edit: didn't see the code.
std::sort works on arrays as well.
Last edited on
I'm don't get how I can do it with arrays.
There reference page I linked shows how to do that...
That is using vectors.
Sorry, you're right. I thought there was an array example, but I guess not.
With an array, you just pass pointers:
std::sort(array,array+length);
How do I pass pointers?
You pass a pointer to the first element of the array and one that points to the element past the last one you want to sort.

1
2
3
const int length=3;
int array[length]={3,2,1};
std::sort(array,array+length);
What if the length is long?
As in "of type long"? It doesn't matter.
So, in int array length={3,2,1}, my array is of characters so can I leave it blank?
You don't have to explictly specify a size for the array, you just have to pass a pointer to the array end to std::sort (you can use sizeof to figure out the array size).
#include <iostream>
#include <iterator>
#include <string>
#include <iomanip>
#include <algorithm>
#include <fstream>
using namespace std;

std::sort(array,array+length)

ifstream instream "input.txt"
ofstream outstream "output.txt"

class Books {
private:
string catalogue_number;
string author_first_name;
string author_last_name;
string book_title;
string strGenre;
string strAvail;

public:
friend bool comp(const Books& a, const Books& b)
{
return a.author_last_name < b.author_last_name ? true : false;
}

friend outstream& operator<<(outstream& os, const Books& s)
{
outstream << setw(6) << s.catalogue_number << setw(15) << s.author_first_name << setw(15) << s.author_last_name << setw(20) << s.book_title << setw(10) << s.strGenre << setw(14) << s.strAvail;
return outstream;
}
friend instream& operator>>(instream& is, Books& s)
{
instream >> s.catalogue_number >> s.author_last_name >> s.author_first_name >> s.book_title >> s.strGenre >> s.strAvail;
return instream;
}
};

bool comp(const Books& a, const Sooks& b);

int main()
{
const string strFile = "input.txt";
intream is(strFile.c_str());
or i=0->sizeof.array - 2 {
for j=i+1->sizeof.array - 1 {
if last_name (i) < last_name (j) {
temp = array (i); array (i) = array (j); array (j) = temp}}}
return 0;
}
Would this work?
Okay, now you're just trolling.
Okay, now you're just trolling.


Yep. Think it is safe to say that, or they are just really slow.
Topic archived. No new replies allowed.