read from file txt 10 names and sort alphabetical

hi i need some help
read from file .txt 10 names and sort alphabetical and put into table
im using borland c++

Comments

  • : hi i need some help
    : read from file .txt 10 names and sort alphabetical and put into table
    : im using borland c++
    :
    Really you need to know what ypur instructor is looking for.
    In C you do this by setting up an array of char *s and calling qsort(). In C++ there are several approaches, some of which, involving templated classes, might be beyond your level.

    Basically you've got three problems 1) read in the data, 2) sort it, 3) print it out.

    1) is the hardest to get completely right, because a user could provide a file containing anything, including a line of text longer than the memory capacity of the computer, a blank file, a list of identical names, a file with ASCII control codes embedded in it, etc etc. However you can probably get away with accepting only the input file given. You will need to load the filenames into an array for sorting.

    2) practise using qsort() or whatever sorting method you choose on integers or hand-entered strings, to make sure you know the interface. Then use it for real on your entered data.

    3) is very easy to do in a rudimentary way, which you will need to do to ensure that steps 1) and 2) are correct. You can polish it later to output an html table or whatever it is you want to do. printf() has many options for formatting.

  • This is what i got so far... can you check and fix this?
    i have a file with 10 names(5digit) and want to put in array alphabetical
    thanks

    #include
    #include
    #include

    int maxposition (char v[][5], int from, int until)
    {
    int Met;
    int maxthesi;
    if (until < from) {
    cout << " Error.... "<<from<<" "<<until;
    exit(-1);
    }
    else {
    maxthesi = from;
    for (Met = from+1; Met <= until; Met++) {
    if (v[Met] > v[maxthesi]) maxthesi = Met;
    }
    }
    return maxthesi;
    }



    main() {
    const char N=10;
    ifstream names;
    char countthem(0);
    char k;
    char sv;
    char meg;
    char array[N][5];
    names.open("names.txt");
    if(!names) {
    cout << " error cannot open txt! "<<endl;
    exit(-1);
    }
    names>>array[5];
    while (!names.eof()) {
    countthem++;
    names >> array[countthem];
    }
    names.close();
    for (k=countthem; k>=1; k--) {
    meg = maxposition(array, 0, k);
    sv = array[meg][5];
    array[meg][5] = array[k][5]; array[k][5] = sv;
    }


    cout<<array[meg]<<endl;
    cin.get();
    system("pause");
    }
  • : This is what i got so far... can you check and fix this?
    : i have a file with 10 names(5digit) and want to put in array
    : alphabetical
    : thanks
    :
    : #include
    : #include
    : #include
    :
    : int maxposition (char v[][5], int from, int until)
    : {
    : int Met;
    : int maxthesi;
    : if (until < from) {
    : cout << " Error.... "<<from<<" "<<until;
    : exit(-1);
    : }
    : else {
    : maxthesi = from;
    : for (Met = from+1; Met <= until; Met++) {
    : if (v[Met] > v[maxthesi]) maxthesi = Met;
    : }
    : }
    : return maxthesi;
    : }
    :
    :
    :
    : main() {
    : const char N=10;
    : ifstream names;
    : char countthem(0);
    : char k;
    : char sv;
    : char meg;
    : char array[N][5];
    : names.open("names.txt");
    : if(!names) {
    : cout << " error cannot open txt! "<<endl;
    : exit(-1);
    : }
    : names>>array[5];
    : while (!names.eof()) {
    : countthem++;
    : names >> array[countthem];
    : }
    : names.close();
    :
    countthem should be an integer type, probably an int. character types will work if the count is low, but indicate confusion. You are also off by one, but you correct that in your next section.

    At this point you should do

    for(k=0;k=1; k--) {
    : meg = maxposition(array, 0, k);
    : sv = array[meg][5];
    : array[meg][5] = array[k][5]; array[k][5] = sv;
    : }
    :
    sv should be an array of characters. Here you run into C++'s extremely poor array syntax. What you need to so is

    char sv[1024]; // 1024 just a "big enough" value to hold a string

    strcpy(sv, array[meg]);
    strcpy(array[meg], array[k]);
    strcpy(array[k], sv);

    :
    : cout<<array[meg]<<endl;
    : cin.get();
    : system("pause");
    : }
    :

    put the last bit in a loop, like the first one I showed you, to indicate that th results are what you wnat.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories