Code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
struct comic
{
char title[100];
char issue[100];
int issuenum;
//struct comic *next;
};
//void assign(struct comic **comics, char title[100], char issue[100], int issuenum);
void mergesort(struct comic *comics, int len);
void merge(struct comic *comicsc, struct comic *leftc, int leftlen, struct comic *rightc, int rightlen);
main()
{
char filename[50], title[100], issue[100];
char *p;
char uscore = '_';
char blank = ' ';
int issuenum, numcomics, count = 0, cnt = 0, past = 0, pcnt = 0, tempcnt = 0;
struct comic *thecomics;
struct comic *temp;
FILE *f1;
printf("--------------------------------------------------------------------------------------");
printf("Welcome to the comic book sorter!\n");
printf("Please enter in the name of the file containing the comic books: ");
//fflush();
fgets(filename, sizeof(filename), stdin);
//Removes the \n that ENTER adds into the filename input
if((p = strchr(filename, '\n')) != NULL)
{
*p = '\0';
}
//Opens the file
f1 = fopen(filename, "r");
//fflush();
if(f1 != NULL)
{
fscanf(f1, "%d", &numcomics);
system("pause");
thecomics = (struct comic*)calloc(numcomics, sizeof(struct comic));
do
{
fscanf(f1, "%s %d %s", title, &issuenum, issue);
cnt = 0;
//Checks for the _ in the string and replaces it with a "space"
do
{
if(title[cnt] == uscore)
{
title[cnt] = blank;
}
cnt++;
}while(cnt < 100);
//Checks for the _ in the string and replaces it with a "space"
cnt = 0;
do
{
if(issue[cnt] == uscore)
{
issue[cnt] = blank;
}
cnt++;
}while(cnt < 100);
////THE ERROR STARTS HERE
strcpy(thecomics[count]->title, title);
strcpy(thecomics[count]->issue, issue);
thecomics[count]->issuenum = issuenum;
count++;
}while(count < numcomics);
//Sorts by title and then mergesorts by comic number and prints
count = 0;
do
{
//Tests to see if the title has already been run
pcnt = count - 1;
while(pcnt >= 0)
{
if(strcmp(thecomics[count]->title, thecomics[pcnt]->title) == 0)
{
past = 1;
}
pcnt--;
}
if(past != 1)
{
tempcnt = 1;
cnt = count +1;
do
{
if(strcmp(thecomics[count]->title, thecomics[cnt]->title) == 0)
{
temp[0] = thecomics[count];
temp[tempcnt] = thecomics[cnt];
tempcnt++;
}
cnt++;
}while(cnt < numcomics);
mergesort(temp, tempcnt);
cnt = 0;
printf("%s\n", temp->title);
while(cnt <= tempcnt)
{
printf("\t%d. %s\n", temp[cnt]->issuenum, temp[cnt]->issue);
cnt++;
}
}//End IF
count++;
}while(count < numcomics);
}//Close if
else
{
printf("There is nothing in the file or it doesn't exist!\n");
}//Close Else
system("pause");
}//Close main
///////////////////////////////////////////////////////////////////////////
/////////In Case I wanted to use linked lists//////////////////////////////
///////////////////////////////////////////////////////////////////////////
/*void assign(struct comic **comics, char title[100], char issue[100], int issuenum)
{
struct comic *count = *comics;
struct comic *start;
if(count == NULL)
{
strcpy(count->title, title);
strcpy(count->issue, issue);
count->issuenum = issuenum;
count->next = NULL;
}
else
{
start = (struct comic*)malloc(sizeof(struct comic));
strcpy(start->title, title);
strcpy(start->issue, issue);
start->issuenum = issuenum;
start->next = NULL;
while(count->next != NULL)
{
count = count->next;
}
count->next = start;
}
}*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void merge(struct comic *comicsc, struct comic *leftc, int leftlen, struct comic *rightc, int rightlen)
{
int comicsnum = 0;
int leftnum = 0;
int rightnum = 0;
// As long as both arrays have more integers, keep merging
while(leftnum < leftlen && rightnum < rightlen)
{
// If the smallest remaining integer in the left half is smaller than
// the smallest remaining integer in the right half, put the integer
// from the left half into the final array first
if(leftc[leftnum]->issuenum <= rightc[rightnum]->issuenum)
{
comicsc[comicsnum] = leftc[leftnum];
comicsnum++;
leftnum++;
}
// Otherwise, go with the right half
else
{
comicsc[comicsnum] = rightc[rightnum];
comicsnum++;
rightnum++;
}
}
// If there aren't any integers left in the right half, that means there
// must still be some in the left half. Copy the remaining integers from the
// left half of the array.
if(rightnum >= rightlen)
{
while(leftnum < leftlen)
{
comicsc[comicsnum] = leftc[leftnum];
comicsnum++;
leftnum++;
}
}
// Otherwise, there are still integers in the right half, so copy those
else
{
while(rightnum < rightlen)
{
comicsc[comicsnum] = rightc[rightnum];
comicsnum++;
rightnum++;
}
}
}//End merge
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void mergesort(struct comic *comics, int len)
{
struct comic *left;
struct comic *right;
int ct;
if(len <= 1)
{
return;
}
left = (struct comic*)calloc(len/2, sizeof(struct comic));
for(ct = 0; ct < len/2; ct++)
{
left[ct] = comics[ct];
}
mergesort(left, len/2);
right = (struct comic*)calloc(len - len/2, sizeof(struct comic));
for(ct = len/2; ct<len; ct++)
{
right[ct - len/2] = comics[ct];
}
mergesort(right, len - len/2);
merge(comics, left, len/2, right, len-len/2);
free(left);
free(right);
}//End mergesort
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Anyone know whats going on?