hey guys i need to sort out some names in a txt file which is in the following format:
Code:
Peter Bladon 598272651
Andrew Sundhu 857695241
Sean Hawley 931003752
Martin Testrow 212860499
Louise Curtis 927185887
Alexander Oddie 157020783
John Colman 478545487
William Richardson 689461958
Reece Jenkins 872692036
Brian Wright 689846491
so far this is what i have done:
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
using namespace std;
int a[1000];
int x;
int k=0;
int y;
int z;
char *p[1000];
int h;
char filetosave[20];
char filetoopen[20];
char forename[1000];
char surname[1000];
double number[1000];
const int maximumwords = 100;
int main(){
//------------------------------------------
//Open a file
cout << "Please enter the file to open: " << endl;
cin >> filetoopen;
ifstream in(filetoopen);
//Error checking
if(!in){
cerr << "Failed to open input file " << filetoopen << endl;
exit(1);
}
//Read file till new line
int h = 0;
while(in){
if(h >= maximumwords) break;
if(in >> forename[h]&& in >> surname[h] >> number[h]) h++,k++;
} //end if
//Formalities
in.clear();
in.close();
cout << "The unsorted array is:\n" << endl;
for(y=0;y<k;y++){
cout << "Element " << y << ": " << forename[y] << endl;
} //end for
// ---------------------
for(int i=0;i<k;i++){
//*p[x]=&a[x];
p[i]=forename+i;
//cout << "p[x]=a+x=" << p[x] << endl;
} //end for
for(int g=0;g<k-1;g++){
for(int i=0;i<k-1;i++){
char *t;
if(*p[i]>*p[i+1]){
t=*(p+i);
*(p+i)=*(p+i+1);
*(p+i+1)=t;
} //end if
} //end for
} //end for
cout << "\nThe sorted array is:\n" << endl;
for(y=0;y<k;y++){
cout << "Element" << y << ": " << *p[y] << endl;
} //end for
system("PAUSE");
} //end main
I can get it to sort numbers out but not characters. When running this code it outputs nothing.
Wondering if someone can point me in the right direction please on how to fix this.
PS. Im a newb at C++ its part of a module im doing at uni:S