Thread: sorting structured array problem

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    sorting structured array problem

    Hey guys, I am developing a program that receives song input(artist, title, rating) from the user and inserts that into a structured array. The user will then be prompted to sort the array by artist, title or rating. It complies but when i run this is what happens.

    How many songs will be entered?2
    Enter the information below for songs:
    Artist:Title:james
    Rating(0-5) :1
    Enter the information below for songs:
    Artist:Title: peter
    Rating(0-5) :2
    Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)ilter,(q)uit:
    Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)ilter,(q)uit:
    It skips 'artist' input and just puts in title. Then it shows the 'select the operation to perform' print twice.

    here is the main of the program

    Code:
    int main(void)
    {
            int i;
            int j = 0;
            int rating_filter;
            char oper;
            song oneSong[99];
            int songCount;
    
            printf("How many songs will be entered?");
            scanf("%d", &songCount);
    
            for(i = 0; i <= songCount - 1; i++)
            {
                    printf("Enter the information below for songs:\n");
    
                    printf("Artist:\n");
                    fgets(oneSong[i].artist, 40, stdin);
    
                    printf("Title:\n");
    
                    fgets(oneSong[i].title, 60, stdin);
    
                    printf("Rating(0-5):");
    
                    scanf("%d", &oneSong[i].rating);
            }
    
    while(13 == 13)
    {
            printf("Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)ilter,(q)uit");
            scanf("%c", &oper);
    
            if(oper == 't')
            {
                    printf("Songs by title:\n");
                    song_by_title(oneSong, songCount);
                    for(j = 0; j <= songCount; j++)
                    {
                    printf("<%s--%s--%d>\n",oneSong[j].title, oneSong[j].artist, oneSong[j].rating);
                    }
            }
            else if(oper == 'a')
            {
                    printf("Song by artists:\n");
                    song_by_artist(oneSong, songCount);
                    for(j = 0; j <= songCount; j++)
                    {
                    printf("<%s--%s--%d>\n",oneSong[j].title,oneSong[j].artist,oneSong[j].rating);
                    }
            }
            else if(oper == 'r')
            {
                    printf("Songs by ratings:\n");
                    song_by_artist(oneSong, songCount);
                    for(j = 0; j <= songCount; j++)
                    {
                    printf("<%s--%s--%d>\n",oneSong[i].title, oneSong[i].artist,oneSong[i].rating);
                    }
            }
            else if(oper == 'f')
            {
                    printf("Enter the rating to show:\n");
                    scanf("%d", &rating_filter);
                    song_by_title(oneSong, songCount);
                    for(j = 0; j <= songCount; j++)
                    {
                    printf("<%s--%s--%d>\n",oneSong[i].title, oneSong[i].artist,oneSong[i].rating);
                    }
            }
            else if(oper == 'q')
            {
                    printf("Thank you for using!\n");
                    break;
            }
            j = 0;
    }
    return 0;
    }
    help would be greatly appreciated!! Thanks in advanced

    -Spencer
    Last edited by matrix0978; 11-11-2009 at 05:07 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Before the scanf() for the char, you need to pull off the newlines that were left behind in the keyboard (input) buffer:

    Code:
    //will pull off one newline, each time.
    someVariable = getchar();
    
    //will pull off all char's up to the first newline Note the odd semi-colon
    //at the end, and the doubled parentheses 
    while((someVariable = getchar()) != '\n');

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    i tried to switch that out but the same problems occur

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Standby and let me run it one time.

    I can't run it without the structure definition. Do you have that?

    OK, got it to run.

    Don't understand while(13 == 13) ??? you know this is just another screwball way to write while(1), and invokes an endless loop, right?
    Last edited by Adak; 11-11-2009 at 05:34 PM.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    would you like to see the other functions?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, I'm good now.

    This is what i did to fix that (I believe). I added "gar" as an int variable, just to be sure nothing else was disturbed.

    Code:
    int main(void)
    {
            int i, rating_filter, songCount, gar;
            char oper;
    //        song oneSong[99];
            int j = 0;
    
            printf("How many songs will be entered?");
            scanf("%d", &songCount);
            gar = getchar();
    
            for(i = 0; i <= songCount - 1; i++)
            {
                    printf("Enter the information below for songs:\n");
    
                    printf("Artist: ");
                    fgets(oneSong[i].artist, 40, stdin);
    
                    printf("Title: ");
    
                    fgets(oneSong[i].title, 60, stdin);
    
                    printf("Rating(0-5): ");
    
                    scanf("%d", &oneSong[i].rating);
                    gar = getchar();
            }
    
       while(13 == 13)
       {
            printf("Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)ilter,(q)uit");
            scanf("%c", &oper);
    
            if(oper == 't')
            {
                    printf("Songs by title:\n");
                    //song_by_title(oneSong, songCount);
                    for(j = 0; j <= songCount; j++)
                    {
                    printf("<%s--%s--%d>\n",oneSong[j].title, oneSong[j].artist, oneSong[j].rating);
                    }
            }
            else if(oper == 'a')
            {
                    printf("Song by artists:\n");
                    //song_by_artist(oneSong, songCount);
                    for(j = 0; j <= songCount; j++)
                    {
                    printf("<%s--%s--%d>\n",oneSong[j].title,oneSong[j].artist,oneSong[j].rating);
                    }
            }
            else if(oper == 'r')
            {
                    printf("Songs by ratings:\n");
                    //song_by_artist(oneSong, songCount);
                    for(j = 0; j <= songCount; j++)
                    {
                    printf("<%s--%s--%d>\n",oneSong[i].title, oneSong[i].artist,oneSong[i].rating);
                    }
            }
            else if(oper == 'f')
            {
                    printf("Enter the rating to show:\n");
                    scanf("%d", &rating_filter);
                    //song_by_title(oneSong, songCount);
                    for(j = 0; j <= songCount; j++)
                    {
                    printf("<%s--%s--%d>\n",oneSong[i].title, oneSong[i].artist,oneSong[i].rating);
                    }
            }
            else if(oper == 'q')
            {
                    printf("Thank you for using!\n");
                    break;
            }
            j = 0;
       }
       return 0;
    }
    I made song array global, which is not what it should be, but you have that part working so this was a work around.
    Last edited by Adak; 11-11-2009 at 05:43 PM.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    thanks that works perfect. Now when it outputs the songs in order it comes shows as this

    skeen
    --spencer
    --1
    james
    --james
    --2
    ----0
    ??

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    i found out what the problem is which fgets adds a \n at the end of the strings. so this is what i added to eliminate that.

    Code:
    for(i = 0; i <= songCount - 1; i++)
            {
                    printf("Enter the information below for songs:\n");
    
                    printf("Artist:");
    
                    fgets(oneSong[i].artist, 40, stdin);
                    len = strlen(oneSong[i].artist);
                    oneSong[len-1].artist == '\0';
    
                    printf("Title:");
    
                    fgets(oneSong[i].title, 60, stdin);
    
    
                    printf("Rating(0-5):");
    
                    scanf("%d", &oneSong[i].rating);
                    gar = getchar();
            }
    but i come up with the error

    92: warning: statement with no effect
    any idea how to recify this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  2. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  3. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM