# Thread: sorting structured array problem

1. ## 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

2. 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. i tried to switch that out but the same problems occur

4. 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?

5. would you like to see the other functions?

6. 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.

7. 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. 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?