So I have part of my code done and I'm stuck on how to sort structures.
This is the code i have until now.

Its supposed to ask the user 3 things title, artist name, rating of song

and im supposed to ask the user to sort them in certain order

so its supposed to look something like this on the bottom
"Select the operation to perform -- (t)itle, (a)rtist, (r)ating, (f)ilter, (q)uit"

and its supposed to sort however the user prompts it to

#include <stdio.h>
#include <stdlib.h>

struct playlist
{
	int rating;
	char title[80]; 
	char artist[60];
};
int main (void)
{
	struct playlist list1;
	int numOfSongs, i;

	printf("Enter the # of songs to be entered: ");
	scanf("%d", &numOfSongs);
	printf("Enter song info in this order <Title, Artist, Rating>: ");
	for (i = 0; i < numOfSongs; i++)
	{
		
		scanf("%s%s%d", &list1.title, &list1.artist, &list1.rating);
		if (i%3)
		{ 
			printf("Enter the next song info in this order <Title, Artist, Rating>: ");
			scanf("%s%s%d", &list1.title, &list1.artist, &list1.rating);
		}
	}
}

why have u written the code from line #22 to line #26.
Do u think u need them.....?

I was going to use that for later, this is what I have for now

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct 
{
  int rating;
  char title[80]; 
  char artist[60];
} playlist;

void sortTitle(playlist *list, int length)
{
	  char tmp[80];
	  int i,x;
	  for(i = 0; i < length - 1; i++)
	  {
		  x = strcmp(list[i].title, list[i + 1].title);
		  if(x < 0)
		  {
			  strcpy(tmp, list[i].title);
			  strcpy(list[i].title, list[i + 1].title);
			  strcpy(list[i + 1].title, tmp);
		  }
		  printf("%s\n", tmp);
	  }
}
int main (void)
{
    playlist list[99];
    int numOfSongs, i;
    char user = 'x';

    printf("Enter the # of songs to be entered: ");
    scanf("%d", &numOfSongs);
  
	for (i = 0; i < numOfSongs; i++)
	{
		printf("Enter Artist, Title, Rate(1-5):\n");
		scanf("%s%s%d", &list[i].artist, &list[i].title, &list[i].rating);
    }
	while(user != 'q')
	{
		printf("Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)ilter,(q)uit:\n");
		scanf(" %c",&user);

		if (user == 't' || user == 'T')
		{	
			  int z;
			  printf("Songs sorted by Title: \n");
			  sortTitle(list, numOfSongs);
			  
			  for (z = 0; z < numOfSongs; z++)
			  {
				  printf("%s\n%s\n%d", list[i].artist, list[i].title, list[i].rating);
			  }
		}
	
    }
}

go ahead.
U are in the right direction.

But I am stuck thats why I am asking for help
PLEASE HELP!!!

would u put some light on what is the exact problem u r facing.....

my output is showing funky characters

and i think something is wrong in the sortTitle

check in code:
line #56:
Are u sure it should be list or something else. Check.

line #22 to #24:
Is that portion of code really swapping the whole song or just the title......!!!.... correct it.

check in code:
line #56:
Are u sure it should be list or something else. Check.

line #22 to #24:
Is that portion of code really swapping the whole song or just the title......!!!.... correct it.

Yea its supposed to be like that in line 56
in line 22 to 24
its supposed to shuffle the title only

Yea its supposed to be like that in line 56

then think about the value of "i". What should list point to?

then think about the value of "i". What should list point to?

The i is similar to the number of songs. it goes through the for loop.
So i = 0 which is the first set of list, then if i = 1 its the second list

i would suggest u to check (may be u can put a printf statement to see what i contain) again for i in line #56 without making just a guess.
Just do it, u will know:
add

printf("%d", i);

before line#56

i would suggest u to check (may be u can put a printf statement to see what i contain) again for i in line #56 without making just a guess.
Just do it, u will know:
add

printf("%d", i);

before line#56

Be honest do you have any idea whats going on?
Do u have a compiler? Because I need help immediately

Edited 7 Years Ago by yasaswyg: n/a

i suppose u are not trying what I am telling u to do.

I am still saying to recheck your code.
And this time better do it before giving just a reply.
Or check whether u have posted the same code that u have in your machine.

i suppose u are not trying what I am telling u to do.

I am still saying to recheck your code.
And this time better do it before giving just a reply.
Or check whether u have posted the same code that u have in your machine.

k my code looks different now. a little bit.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  
typedef struct 
{
	int rating;
    char title[80]; 
	char artist[60];
} playlist;

void sortArtist(playlist list[80], int length)
{
	  playlist tmp;
	  int j, i;
	  int x = 0;
	 
	  for(i = 0; i < length; i++)
	  {
		  j = strcmp(list[length].artist, list[length + 1].artist);
		 
		  if(j < 0)
		  {
			  strcpy(tmp, list[length].artist);
			  strcpy(list[length].artist, list[length + 1].artist);
			  strcpy(list[length + 1].artist, tmp);
		  }
	  }
}

void sortTitle(playlist list[80], int length)
{
	  char tmp[80];
	  int i,x;
	  for(i = 0; i < length - 1; i++)
	  {
		  x = strcmp(list[length].title, list[length + 1].title);
		  
		  if(x < 0)
		  {
			  strcpy(tmp, list[length].title);
			  strcpy(list[length].title, list[length + 1].title);
			  strcpy(list[length  + 1].title, tmp);
			  
		  }
	  }
}
int main (void)
{
    playlist list[99];
    int numOfSongs, i;
    char user = 'x';
 
    printf("Enter the # of songs to be entered: ");
    scanf("%d", &numOfSongs);
 
	for (i = 0; i < numOfSongs; i++)
	{
		printf("Enter Artist, Title, Rate(1-5):\n");
		scanf("%s%s%d", &list[i].artist, &list[i].title, &list[i].rating);
    }
	while(user != 'q')
	{
		printf("Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)ilter,(q)uit:\n");
		scanf(" %c",&user);
 
		if (user == 't' || user == 'T')
		{	
			  int z;
			  printf("Songs sorted by Title: \n");
			  sortTitle(list, numOfSongs);
 
			  for (z = 0; z < numOfSongs; z++)
			  {
				  printf("%s\n%s\n%d\n", list[i].artist, list[i].title, list[i].rating);
			  }
		}
		if (user == 'a' || user == 'A')
		{
			int z;
			printf("Songs sorted by Artist: \n");
		//	sortArtist(list, numOfSongs);
			
			for (z = 0; z < numOfSongs; z++)
			{
				printf("%s\n%s\n%d\n", list[i].artist, list[i].title, list[i].rating);
			}
		}
		if (user == 'r' || user == 'R')
		{	
			int z;
			printf("Songs sorted by Rating: \n");
			sortRating(list, numOfSongs);
			
			for (z = 0; z < numOfSongs; z++)
			{
				printf("%s\n%s\n%d\n", list[i].artist, list[i].title, list[i].rating);
			}
			
		}
		
		printf("End Program!\n");
	} 
}

Im sorry im just really annoyed by this code.

for (z = 0; z < numOfSongs; z++){				printf("%s\n%s\n%d\n", list[i].artist, list[i].title, list[i].rating);
}

u are iterating using the variable z and using i to access the list which contains numOfSongs because of the loop which u used for reading input.

NB: Better listen to peaple who are trying to help rather than just going blah blah about anything.... anything at all........

Edited 7 Years Ago by dkalita: n/a

u are iterating using the variable z and using i to access the list which contains numOfSongs because of the loop which u used for reading input.

k got it what else?

Comments
better tell whats your problem. What u expect and wat u r getting and stop giving a S**t...

k got it what else?

I am not going to tell what your problem is. Its u who need to ask.........

I am not going to tell what your problem is. Its u who need to ask.........

k well i got problem on sortArtist
and it says function incomplete for all the strcpys

Edited 7 Years Ago by yasaswyg: n/a

u have declared tmp as playlist whereas strcpy takes char *
make it as

char *tmp = malloc(80);

edit:
OR

make it as

char tmp[80];

Edited 7 Years Ago by dkalita: n/a

u have declared tmp as playlist whereas strcpy takes char *
make it as

char *tmp = malloc(80);

edit:
OR

make it as

char tmp[80];

alrite cool
taht made the title sort work but not the artist sort

what is the error u are getting.
Or what is the output coming and what u expected....

It just returns the things in the order that i wrote it

what kind of sorting algorithm is that.???
Read about sorting first.
U are not using a correct sorting algo.

This article has been dead for over six months. Start a new discussion instead.