> Also I have no clue as to why mode code gets so indented when I post it
Your code editor is using real tabs for indentation
Switch it to using say 4 spaces for each indentation level, then it will look better posted here
This...
Code:
while(ch1 != EOF)
{
if(ch1 == '\n')
{
nLineCount++;
index6 = 0;
cout << lineData << endl;
}
lineData[nLineCount][index6] = ch1;
ch1 = inData.get( );
index6++;
lineData[nLineCount][index6] = '\0'; //!! you need this!!
}
So the aim of this is to get lineData looking something like
lineData[0] = "1, 2, zz"
lineData[1] = "1, 9, ccc"
Though it would seem to me to be a lot easier to read a whole line using this,
Code:
inData.getline( lineData[nLineCount] );
nLineCount++;
Rather than reading each character at a time.
Here's a short example showing how to sort such data based on the text field at the end of the line
Code:
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
// define a type for each line of text
// it saves more complicated casts later on
typedef char line[50];
// find the 2nd comma in a string
const char *find2ndComma ( const char *p ) {
for ( int i = 0 ; i < 2 && p != NULL ; i++ ) {
p = strchr( p, ',' );
if ( p != NULL ) p++;
}
return p;
}
// compare two strings from the lineData array
// starting at the 2nd comma in each one
int compare ( const void *a, const void *b ) {
const line *pa = (const line*)a;
const line *pb = (const line*)b;
const char *comma1 = find2ndComma((const char*)pa);
const char *comma2 = find2ndComma((const char*)pb);
return strcmp( comma1, comma2 );
}
int main ( ) {
line lineData[6] = {
"1, 2, zz",
"1, 9, ccc",
"2, 3, lll",
"2, 13, aaaaa",
"3, 3, mmm",
"3, 11, oo"
};
qsort( lineData, 6, sizeof(line), compare );
for ( int i = 0 ; i < 6 ; i++ ) {
cout << lineData[i] << endl;
}
return 0;
}