Thread: Can't Find Bug in basic MP3 Sorter

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    4

    Can't Find Bug in basic MP3 Sorter

    I'm writing this for a school project (yes I know it's messy, it's basicly fill in the blanks in the code the prof gave us). I have everything working but getting option 8 to work. It's supposed to reorder the index so it's in alphabetical order by title.

    here's the code, maybe you could help me? I can't figure out why 8 isn't working.

    Code:
    #include <iostream.h>
    #include <iomanip.h>    // For setw()
    #include <string.h>     // For strcmp()
    
    
    const int MAX_Songs = 3;   // Maximum number of MP3s
    int index = 0;
    
    struct MP_Three
    {
        char     Title [15];
        char     Artist[15];
        char     Genre[15];   
    };
    
    int main()
    {
        MP_Three Collection[MAX_Songs]; // collection (Array) of MP3s
        char     CurrentTitle [15];
        char     CurrentArtist[15];
        char     CurrentGenre[15];
    	bool operator<(const MP_Three& a) {
           //comparison operator...this allows you to sort MP_Three objects just like ints
               return (strcmp(this->Title,a.Title) > 0);
        }
    	int length = 0;        // Number of entries
        MP_Three CurrentSong;             // Current record being entered
        int op;
       
        
        //Menu
       
        while(op != 9)
        {
            cout << "*** choose an operation ***"<<endl;
            cout << "1 Add a MP3 Collection"<<endl;
            cout << "2 Search by Title"<<endl;
            cout << "3 Search by Artist"<<endl;
            cout << "4 View Entries" << endl;
            cout << "5 Edit Entries" << endl;
            cout << "6 Search by Genre" << endl;
            cout << "7 Delete MP3" << endl;
            cout << "8 Sort Entries (Not Completed)" << endl;
    		cout << "9 Quit"<< endl<<endl;
            cin>>op;
            cin.ignore(1,'\n');
            
            //User Selections
            
           
            if (op == 1)  //Add an Mp3
            {
                index = 0;
                while (index < MAX_Songs)
                {
                cout << "Enter the Title of MP3: ";
                cin >> Collection[index].Title;
           
                cout << "Enter the Artist: ";
                cin >> Collection[index].Artist;
           
                cout << "Enter the Genre: ";
                cin >> Collection[index].Genre;
               
                index++;
               }
    
           
            }
            if (op == 2)  //Search By Title
    
            {
               
           
               
                cout << "Enter Title: "<<endl;
                cin >> CurrentTitle;
                int found = 0;       
                int k;
                for (k = 0; k < MAX_Songs; k++)
                {
                    if (strcmp(CurrentTitle, Collection[k].Title) == 0)
                    {  
         
                    index = k;
                    cout<<endl<<CurrentTitle<<" found at index "<< index<<endl<<endl;
                    cout<<"Title: "<<Collection[index].Title<<endl;
                    cout<<"Artist: "<<Collection[index].Artist<<endl;
                    cout<<"Genre: "<<Collection[index].Genre<<endl;
                    found = 1;
                    }
       
                }
            if (found == 0)
                    cout<<endl<< "MP3 record not found"<<endl<<endl;
               
            }
            if (op == 3)  //SEARCH BY ARTIST
            {
                    cout << "Enter Artist"<<endl;
                cin >> CurrentArtist;
    
                int found = 0;       
                int k;
                for (k = 0; k < MAX_Songs; k++)
               
                    if (strcmp(CurrentArtist, Collection[k].Artist) == 0)
                    {  
         
                    index = k;
                    cout<<endl<<CurrentArtist<<" found at index "<< index<<endl<<endl;
    
                    cout<<"Title: "<<Collection[index].Title<<endl;
                    cout<<"Artist: "<<Collection[index].Artist<<endl;
                    cout<<"Genre: "<<Collection[index].Genre<<endl;
                    found = 1;
                    }
               
               
            }
           
           
            if (op == 4) //View Entries
            {
                int i;
            for (i = 0; i < MAX_Songs; i++)
                {
                cout << "Title: " << Collection[i].Title << endl;
               
                cout << "Artist: " << Collection[i].Artist << endl;
       
                cout << "Genre " <<Collection[i].Genre << endl << endl;
                }
            }
           
            if (op == 5)  //Edit Mp3 Info
    
            {
    
               
                cout << "Enter Title"<<endl;
                cin >> CurrentTitle;
                int found = 0;       
                int k;
                for (k = 0; k < MAX_Songs; k++)
                {
                    if (strcmp(CurrentTitle, Collection[k].Title) == 0)
                    {  
         
                    index = k;
                    cout<<endl<<CurrentTitle<<" found at index "<< index<<endl<<endl;
                    cout<<"Title: "<<Collection[index].Title<<endl;
                    cout<<"Artist: "<<Collection[index].Artist<<endl;
                    cout<<"Genre: "<<Collection[index].Genre<<endl;
                    found = 1;
                    }
    
                    }   
                if (found == 0)
                {
                    cout<<endl<< "MP3 record not found"<<endl<<endl;
                }
       
                cout << "Enter the new song Title of the MP3 ";
                cin >> Collection[index].Title;
           
                cout << "Enter the new Artist ";
                cin >> Collection[index].Artist;
           
                cout << "Enter the new Genre ";
                cin >> Collection[index].Genre;
    
           
            }
            if (op == 6) //SEARCH BY GENRE
            {
                cout << "Enter Genre: "<<endl;
                cin >> CurrentGenre;
    
                int found = 0;       
                int k;
                for (k = 0; k < MAX_Songs; k++)
               
                    if (strcmp(CurrentGenre, Collection[k].Genre) == 0)
                    {  
         
                    index = k;
                    cout<<endl<<CurrentGenre<<" found at index "<< index<<endl<<endl;
    
                    cout<<"Title: "<<Collection[index].Title<<endl;
                    cout<<"Artist: "<<Collection[index].Artist<<endl;
                    cout<<"Genre: "<<Collection[index].Genre<<endl;
                    found = 1;
                    }
           
            }
            if (op ==7) //DELETE MP3
    
            {
                cout << "Enter Title"<<endl;
                cin >> CurrentTitle;
                int found = 0;       
                int k;
                for (k = 0; k < MAX_Songs; k++)
                {
                    if (strcmp(CurrentTitle, Collection[k].Title) == 0)
                    {  
         
                    index = k;
                    cout<<endl<<CurrentTitle<<" found at index "<< index<<endl<<endl;
                    cout<<Collection[index].Title<<endl;
                    cout<<Collection[index].Artist<<endl;
                    cout<<Collection[index].Genre<<endl;
                    found = 1;
                    }
    
                    }   
                if (found == 0)
                {
                    cout<<endl<< "MP3 record not found"<<endl<<endl;
                }
    			
    			cout << "Enter New Information to Delete Old MP3"<<endl;
                cout << "Enter New Title: ";
                cin >> Collection[index].Title;
           
                cout << "Enter new Artist: ";
                cin >> Collection[index].Artist;
           
                cout << "Enter new Genre: ";
                cin >> Collection[index].Genre;
    
           
            }
    		if(opp ==8) 
    			{
    				sort_array(Collection);
    				cout<< "Mp3s Sorted";
    			}
    		{
    			
    
           
        }
        return 0;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This shouldn't be called C++ code, it's just a lot of C.
    You want to make it C++? Then use std::string and build some class around all that mess. Also use std::vector for your array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It looks like option 8 calls a function called sort_array. Where is that function defined or declared? Also, you use a variable called opp for option 8 but no such variable is declared in the posted code.

    >> This shouldn't be called C++ code, it's just a lot of C.
    It uses C++ objects like cout and cin. It might not use paradigms and features of C++ that can make it better, but that just means the OP is still learning.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Daved View Post
    >> This shouldn't be called C++ code, it's just a lot of C.
    It uses C++ objects like cout and cin. It might not use paradigms and features of C++ that can make it better, but that just means the OP is still learning.
    Yes, it does... but the rest is just C, so I'm offering suggestions on what to do to improve it to better C++!

    Code:
    	bool operator<(const MP_Three& a) {
           //comparison operator...this allows you to sort MP_Three objects just like ints
               return (strcmp(this->Title,a.Title) > 0);
        }
    My eyes also spot a nested function! That is undefined. Move it outside the function.
    Btw, you should be including iostream, not iostream.h.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm offering suggestions on what to do to improve it to better C++
    I agree with your suggestions, but they were added as an edit after I posted. Also, given the headers the OP is using, I would imagine there are quite a few things that are being taught that aren't good practice.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But wait, there's more!
    Code:
    bool operator<(const MP_Three& a) {
    A global operator < does not take one argument.
    Code:
    if(opp ==8)
    opp is undeclared - this code doesn't even compile!
    Code:
    sort_array(Collection);
    Undefined function.

    The code uses poor indentation, and apparently a } has gone missing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    in class we have to use to use Visual Studio 6.0 : P The teacher wrote the basic structure of the code, i would have used case statements but he wants us to use ifs

    the headers need .h to compile in VS 6.0 (i.e. iostream.h)

    i tried getting the sort command in the C++ STL to work, to sort the index entries alphabeticly he gave us some sample code to do it but it's bubble sorting : P

    I was trying to do it a bit more cleanly.

    i fixed op == 8 fyi

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In case you can, I suggest you upgrade your compiler. Try Visual Studio 2008 Express (it's free), if you can.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    i'd like to! they have 6.0 in the lab and that's what he requires it to be written in, i'm only taking this class since it's required for my major. I'll probably need to reteach myself over break anyways

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can always complain that they should upgrade. As an argument, you can argue that Visual Studio 6 is very old and a very poor compiler + not so standards compliant.
    But hey, C++ isn't so boring. You just need to get yourself a decent class and a good teacher.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    it's not so much that i think C++ is boring, it's fun actually, it's just frustrating that things that work in most other compilers dont in this one, I wrote it in case statements so it works, but i'm just having trouble writing the code the way the instructor is making us write it for this project

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by trickeastcs View Post
    ...it's just frustrating that things that work in most other compilers dont in this one...
    Welcome to the world of Visual C++ 6.
    Anyway, if you have questions about any code, just let us know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The standard headers like <iostream> work in VC++ 6.0. If you have trouble using them feel free to post the error you get. If the instructor requires you to use <iostream.h> instead of <iostream>, feel free to be very skeptical of anything the instructor says.

    >> i tried getting the sort command in the C++ STL to work
    Again, that can work, feel free to post your attempt if you need help with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to find a bug
    By serge in forum C++ Programming
    Replies: 17
    Last Post: 05-09-2008, 04:06 PM
  2. Can't find the bug
    By caduardo21 in forum C Programming
    Replies: 15
    Last Post: 05-17-2005, 10:24 AM
  3. Annoying bug I can't find or fix!
    By homeyg in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2004, 12:13 AM
  4. Time for another round of: FIND THAT BUG!
    By Geolingo in forum C++ Programming
    Replies: 0
    Last Post: 10-29-2003, 05:29 AM
  5. Can you find the bug? 'Cause we can't
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 11-29-2001, 04:28 PM