Hi, I'm making a program for my CS1 class that will read in comic book names from a file, put them in a linked list, sort them, and then print them in a specific order. The problem I'm having right now is that when I run the program and type in the filename a runtime error occurs that pops up and asks me if I want to run Visual C++. I know where the problem is in my code but I don't know how to fix it. I marked where the problem is in my code below:
Code:
#include<stdio.h>
#include<math.h>
#include<stdlib.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);
main()
{
char filename[50], title[100], issue[100];
char uscore = '_';
char blank = ' ';
int issuenum, numcomics, count = 0, cnt = 0;
struct comic *thecomics;
FILE *f1;
printf("--------------------------------------------------------------------------------------");
printf("Welcome to the comic book sorter!\n");
printf("Please enter in the name of the file containing the comic books: ");
//PROBLEM IS SOMEWHERE RIGHT IN HERE
fflush(stdin);
gets(filename);
//Opens the file
f1 = fopen(filename, "r");
fflush(stdin);
if(f1 != NULL)
{
fscanf(f1, "%d", numcomics);
system("pause");
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);
assign(thecomics, title, issue, issuenum);
count++;
}while(count < numcomics);
}//Close if
else
{
printf("There is nothing in the file!");
}//Close Else
}//Close main
void assign(struct comic *comics, char title[100], char issue[100], int issuenum)
{
struct comic *count = comics;
struct comic *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;
}
if(count == NULL)
{
count = start;
}
else
{
count->next = start;
}
}
Anyone know whats going on?