Thread: bubble sort not working with struct...

  1. #1
    Registered User
    Join Date
    Oct 2004

    bubble sort not working with struct...

    I need to sort the records by inv_num...I tried a regular bubble sort but it gives me and error, then I found an example on here that was using keys and key pointers, which is what I think I am supposed to do, so that is in comments on here....I just want it to sort and need help! Thanks!!!

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    struct info                             // Declares the struct
           char inv_num [16];
           char prod_name [21];
           char prod_code [4];
           int  oh_qty;
           int  bo_qty;
           int  oo_qty;
           float inv_eval;
           float avg_cost;
           float sell_price;
    struct key
            char invnbr [16];
            int recnbr;
     key *keyptr = new key[16];
     key tempkey;
    class inventory
            fstream file;
     inventory() : file("inventory.dat", ios::out | ios::in | ios::binary)
            int n, j, k;
            bool exchangeMade;
            char temp;
            k = 0;
            exchangeMade = true;
            file.seekg(0, ios::end);
            n = file.tellg()/sizeof(info);
            file.seekg(0, ios::beg);
      while ((k < n - 1) && exchangeMade)
                  char temp[16];
                  exchangeMade = false;
                  for (j = 0; j < n - k; ++j)
                      if (strcmp(info.inv_num[j], info.inv_num[j+1]) > 0)
                           temp = info.inv_num[j];
                           info.inv_num[j] = info.inv_num[j + 1];
                           info.inv_num[j + 1] = temp;
                           exchangeMade = true;
    /*     while ((k < n - 1) && exchangeMade)
                  exchangeMade = false;
                  for (j = 0; j < n - k; ++j)
                      if (strcmp(keyptr[j].invnbr, keyptr[j + 1].invnbr) > 0)
                           tempkey = keyptr[j];
                           keyptr[j] = keyptr[j + 1];
                           keyptr[j + 1] = tempkey;
                           exchangeMade = true;
            file.close();                           // Deconstructor
    int main()
            inventory go;
            info x;
            string valid;
            string neg;
            int num_recs;
            return 0;
    Thank you soooo much!!!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Nowhere in your code provided do you appear to actually read any information from the file. How do expect to magically sort something if you haven't read it into memory where you can operate upon it?

    You make references to the info struct in your inventory class' constructor code however it cannot be used this way. You need to create an instance of your info struct by way of use of a variable of that struct.

    Bubble sorts, at least all that I have seen, are usually implemented as double for loops, i.e for an int array something along the lines of:

    void bsort( int *array, int size)
        for( int i = size-1; i > 0; --i )
            for( int j = 0; j < i; ++j )
                if( array[j] > array[j+1] )
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
    Maybe that can help you with your implementation. You are going to need to read all your file information into an array of info structs and then sort them. In place of simply comparing two ints in an int array, you will be comparing the inv_num character array (via strcmp?) members of the info array.

    Personally I would just overload the less-than (<) operator for my info struct, load everything from the file into a vector<info> STL container, and then just call the templated sort function defined in the <algorithm> header.
    Last edited by hk_mp5kpdw; 10-12-2004 at 12:56 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM