Ok i decided to start from scratch for my CD database.
I've created an array structure, i can enter the fields, but for some reason it dont display to screen neone know why please???

also how can i add an option to either add more records or just quit . tried it with a while loop didnt work..

here is the program i've done so far....

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

// Idea here is to enter an array of structures
// write them to disk and read them back


// simple structure
struct tuple {
	char artist[50];
	char album[50];
	char label[50];
	int year[4];
	char type[30];
};
	
int main() {
	
	// file pointer
	FILE * mydata;
	
	// structures
	struct tuple myDB[3];
	struct tuple otherDB[3];
		
	// variable needed
	int i;
	
	// go through each record
	// write each to disk using formatted output
	mydata = fopen("mymusic.txt","w");
	
	for (i=0; i<2; i++) {	
		
		printf("Enter the artist: \n");
		scanf("%s",myDB[i].artist);
		printf("Enter the album: \n");
		scanf("%s",myDB[i].album);
		printf("Enter label name: \n");
		scanf("%s",myDB[i].label);
		printf("Enter year: \n");
		scanf("%d",&myDB[i].year);
		printf("Enter the type of music: \n");
		scanf("%s",&myDB[i].type);
		
		fprintf(mydata, "%s %s %s %d %s\n",myDB[i].artist, myDB[i].album, myDB[i].label,myDB[i].year,myDB[i].type);
	
		//printf("\n\n press 1 to continue,0 to stop");
  		//	scanf("%d",&ch);
	}
	
	fclose(mydata);
	
	
	// read the data 
	printf("reading data...\n");
	
	mydata = fopen("mymusic.txt","r");
	
	for (i=0; i<2; i++) {
		fscanf(mydata,"%s %s %s %d %s",otherDB[i].artist, otherDB[i].album, otherDB[i].label, &otherDB[i].year, otherDB[i].type);
		printf("Record is...\n");
		printf("%s\n",otherDB[i].artist);
		printf("%s\n",otherDB[i].album);
		printf("%s\n",otherDB[i].label);
		printf("%d\n",otherDB[i].year);
		printf("%s\n",otherDB[i].type);
		
	}
	fclose(mydata);
	
}

Sorry ppl didnt know how to edit this post,,,, here is a more updated version of my problem ....

Ok i decided to start from scratch for my CD database.
I've created an array structure, i can enter the fields, but for some reason it dont display to screen neone know why please???

sorted out the records thing.... but now i cant display and when i press the sort option it dont sort,, i dont get no errors when i compil tohugh.. please neone help me out here...


here is the program i've done so far....

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

// Idea here is to enter an array of structures
// write them to disk and read them back


// simple structure
struct tuple {
	char artist[50];
	char album[50];
	char label[50];
	int year[4];
	char type[30];
};
	
int filewrite() {
	
	// file pointer
	FILE * mydata;
	
	// structures
	struct tuple myDB[20];
	struct tuple otherDB[20];
		
	// variable needed
	int i;
	
	// go through each record
	// write each to disk using formatted output
	mydata = fopen("mymusic.txt","w");
	i=1;
	
		while(i){
		printf("Enter the artist: \n");
		scanf("%s",myDB[i].artist);
		printf("Enter the album: \n");
		scanf("%s",myDB[i].album);
		printf("Enter label name: \n");
		scanf("%s",myDB[i].label);
		printf("Enter year: \n");
		scanf("%d",&myDB[i].year);
		printf("Enter the type of music: \n");
		scanf("%s",&myDB[i].type);
		
		fprintf(mydata, "%s %s %s %d %s\n",myDB[i].artist, myDB[i].album, myDB[i].label,myDB[i].year,myDB[i].type);
		
		printf("\n\n press 1 to continue,0 to stop");
 		 scanf("%d",&i);
	
}
	
	fclose(mydata);
	
	{
	// read the data 
	printf("\n\n\n\n\n\n\n\nreading data...\n");
	
	mydata = fopen("mymusic.txt","r");
	
	
	while(i){
		fscanf(mydata,"%s %s %s %d %s",otherDB[i].artist, otherDB[i].album, otherDB[i].label, &otherDB[i].year, otherDB[i].type);
		printf("\n\n\nRecord is...\n\n\n");
		printf("%s\n",otherDB[i].artist);
		printf("%s\n",otherDB[i].album);
		printf("%s\n",otherDB[i].label);
		printf("%d\n",otherDB[i].year);
		printf("%s\n",otherDB[i].type);
		
	}
	fclose(mydata);
	
}
}

void main()
{
  int c;
  
  while(c!=5)
   {  
     
     printf("GIVE CHOICE--\n");
     printf("   1 TO ENTER CD INFO.\n");
     printf("   2 TO SEE STUDENT.TXT FILE\n");
     printf("   3 TO SORT CDs BY YEAR\n");
     printf("   4 TO SEARCH USING ALBUM NAME\n");
     printf("   5 TO EXIT\n\n--");
     scanf("%d",&c);
     
     switch(c)
	 {
	 case 1:
		  filewrite();
		  break;
	 case 5:
		  break;
	case 3:
		  filesort();
		  break;
	 default:
		  break;
	 }
    }

  }

int bubble(int x[],int n)
{
 int hold,j,pass,i,switched = 1;
 for(pass = 0; pass < n-1 && switched == 1;pass++)
 {
  switched=0;
  for (j=0;j<n-pass-1;j++)
   if (x[j]>x[j+1])
   {
    switched=1;
    hold = x[j];
    x[j] = x[j+1];
    x[j+1]=hold;
    }
   }
return(0);
}

int filesort()
  { 
    char artist[50];
	char album[50];
	char label[50];
	int year[4],x[100],n,i,j;
	char type[30];
    FILE *fp,*fm;
    FILE * mydata;
	struct tuple myDB[20];
	struct tuple otherDB[20];
    fp = fopen("mymusic.txt","r");
    fm = fopen("year.txt","w");
    i=0;
   while(! feof(fp))
    {

     fscanf(mydata,"%s %s %s %d %s",otherDB[i].artist, otherDB[i].album, otherDB[i].label, otherDB[i].year, otherDB[i].type);
     x[i]= year[i];
     i=i+1;
      }

       n=i;

       bubble(x,n);

	for(i=0;i<n;i++)
	{
	printf(" %d\t",x[i]);
	}

 for(i=0;i<n;i++)
 {
   for (j=0;j<n;j++)
   {
   if(x[i]==year[j])
   {
      fprintf(mydata, "%s %s %s %d %s\n",myDB[j].artist, myDB[j].album, myDB[j].label,&myDB[j].year,myDB[j].type);
     }
   }
 }
  fclose(fm);
  fclose(fp);
  printf("\n\n\nPRESS ANY KEY");
  getch();

}
  1. Don't use void main(). Use int main() and return 0; .
  2. You shouldn't use scanf("%s", string); . As Salem says, it's a poor replacement for gets(). Use fgets() instead.

You don't print anything.

For the sort function,, i cant work out how to pass the structure arguments into that function....
Can you explain how to do this or point me to a post that can explain this to me.. i've tried searching for it but the info i get is too hard to understand im very new to C...

man is your saviour, do man qsort or get your libc.hlp from devcpp if you are on win...

for a struct of type

struct tuple {
	char artist[50];
	char album[50];
	char label[50];
	int year[4];
	char type[30];
} array[ MAX_NUM ], *sorted_array[ MAX_NUM ] ;

a view sorted by artist of array can be constructed in sorted_array like so

int compare_fn( void *tuple1, void *tuple2 )
{
    return strcmp( ((tuple *) tuple1)->artist, ((tuple *) tuple2)->artist ) ;
}

void sort( int num, tuple *array, tuple **result )
{
    int i ;
    for( i = 0 ; i < num ; i++ ) result[ i ] = array + i ;
    qsort( result, sizeof( tuple ), compare_fn ) ;
}

...
sort( MAX_NUM, array, array_sorted ) ;

*Disclamer: This code has not been tested at all and no guarante about correctness what so ever is made ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.