Sorting/Displaying

This is a discussion on Sorting/Displaying within the C++ Programming forums, part of the General Programming Boards category; I'm a huge noob at programming.. but I'm suppose to be making a program that creates an array of structures... ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    25

    Sorting/Displaying

    I'm a huge noob at programming.. but I'm suppose to be making a program that creates an array of structures... each structure contains the id, first name, last name, major, and gpa of a student... and my program is suppose to be able to sort by last name and display, sort by id and display, search for a specific structure by last name, and search for a specific structure by id...

    this is what I have so far.... but I don't really know where to go from here.. I should probably work on finding a way to sort the structures by name and id.. but I don't really know where to start..

    can anyone help?




    Code:
        int x=0;
        while(x =! 5)    
        {
                
        cout << "Enter a number from the list to choose an option";
        cout << "1. List the students sorted by last name";
        cout << "2. List the students sorted by student id";
        cout << "3. Search the students by last name";
        cout << "4. Search the students by student id";
        cout << "5. Quit the program";    
        cin >> x;
        if (x=1)
                ;
        else if (x=2)
                ;
        else if (x=3)
                ;
        else if (x=4)
                ;
                   
        }
        exit(1);

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Note that = and == are not the same thing.

    What do you know about sorting?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    25
    oh yea I'll fix that real fast -_-''

    almost nothing lol..

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should also note that != and =! are not the same thing either.

    You should look up sort in your C++ book then, or your favorite on-line C++ resource.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    25
    oh yea -_-''

    ok I'll do that (:

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    25
    bleh.. ok I know a little about sorting now....

    I know how to sort by numbers of a normal int array..

    but I need to sort by the char's of the last name that is included in a structure.. the structure being dynamically created........

    this I don't have a clue how to approach.....

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The operator < is defined for std::strings just like for ints.

    Every built-in container (such as vector or list) knows its own size. If you're doing your own memory management with a home-brew dynamic array, then you know the size of the array. So the dynamic-ness can't possibly matter.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    You can overload operators You need for sorting (for example <) and then sort it like an int array.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    25
    Ok.. this is what I have so far and I'm wondering if everything is right so far?
    and I don't know what to pass through the functions.. I'm thinking a pointer pointing at where my array of structures begins.. but what would my pointer be?


    Code:
    #include <iostream>
    #include <fstream>
    
    
    
    
    using namespace std;
    
    
    
    
    
    
    void choice1();
    void choice2();
    void choice3();
    void choice4();
    
    
    
    int main(int argL, char *argv[1])
    {
        int studentnumber=0, x=0;
        struct My_struct
            {
               char id[4];
               char fname[30];
               char lname[30];
               char major[10];
               float gpa;
            };
            
            
            
        ifstream infile;
        infile.open(argv[1]);
        infile >> studentnumber;  
        
        
        
        My_struct* my_array = new My_struct[studentnumber];
        
    
    
        
        
        
        while(x != 5)    
        {
                
        cout << "Enter a number from the list to choose an option";
        cout << "1. List the students sorted by last name";
        cout << "2. List the students sorted by student id";
        cout << "3. Search the students by last name";
        cout << "4. Search the students by student id";
        cout << "5. Quit the program";    
        
        if (x==1)
        choice1();
        else if (x==2)
        choice2();
        else if (x==3)
        choice3();
        else if (x==4)
        choice4();
        }
        exit(1);
        
        
        
    }
        
        
        
        void choice1()
        {
        
        }
        void choice2()
        {
             
        }
        void choice3()
        {
             
        }
        void choice4()
        {
             
        }

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    You might want to choose beter names that choice1 .. choice4. You shouldn't have to rename those functions if you later wanted to change which number did what thing.

    Something like ListByID for choice2 perhaps? You'll think of something for the rest.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to define your structure outside of any function. It's char *argv[]; I don't know whether the compiler will ignore the 1 or not, but the 1 is surely wrong (since you're not expecting 0-character arguments). You never read in x. But you did create the array pointer correctly, so well done there.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    I think You should pass pointer and array size through functions and struct shouldn't be inside main.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    25
    I'm a noob lol...
    how do I pass the pointer through the function?
    I already declared my struct outside any function..

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you refer to the code above, you most certainly did not declare your struct outside any function.

    The input to a function is given inside the ().

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    25
    no I'm not referring to the above code..

    in any case I don't have time to fix all the errors I have in this program lol.. its due in 35 minutes..... but thanks for the help anyways

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21