0

Hello. I'm not getting the desired output for my program. The problem is specifically in the file. I can get it to save and load but when I try displaying it, its gonna have a General Protection Exception error. Here's my code. Please help

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

#define max 10
#define gmax 100

typedef struct cell{
	 char name[24];
	 int rank;
	 struct cell *next;
}node, *nodeptr;

typedef struct{
	 nodeptr header[max];
	 nodeptr rankladder[gmax];
	 int nextrung;
}DDS;

void initialize(DDS *D);
int hash(char n[]);
void insert(DDS *D, char n[]);
void challenge(DDS *D, char n[]);
void display_storage(DDS D);
void display_rank(DDS D);
void save(DDS D);
void load(DDS *D);

void initialize(DDS *D)
{
	 int ctr;

	 for(ctr = 0; ctr < max; ctr++)
		  D->header[ctr] = NULL;

	 D->nextrung = 0;
}

int hash(char n[])
{
	 int ctr, value = 0;

	 for(ctr = 0; ctr < strlen(n); ctr++)
		  value += (int)n[ctr];

	 return value % max;
}

void insert(DDS *D, char n[])
{
	 nodeptr temp;
	 int h;

	 temp = (nodeptr) malloc(sizeof(node));
	 h = hash(n);

	 strcpy(temp->name, n);

	 temp->next = D->header[h];
	 D->header[h] = temp;
	 D->rankladder[++(D->nextrung)] = temp;
	 temp->rank = D->nextrung;
}
void challenge(DDS *D, char n[])
{
	 nodeptr p;
	 int h, Challenger, Challengee, flag = 0;

	 h = hash(n);
	 randomize();

	 for(p = D->header[h]; p != NULL && flag == 0;)
	 {
                     if(strcmp(p->name, n) == 0)
                    {
		if(p->rank != 1)
		{
		  printf("%s vs %s!\n", D->rankladder[p->rank - 1]->name, p->name);
		      Challenger = rand();
		      Challengee = rand();

		      if(Challenger < Challengee)
		      {
		           D->rankladder[p->rank - 1]->rank++;
		           D->rankladder[p->rank] = D->rankladder[p->rank - 1];
		           p->rank--;
		           D->rankladder[p->rank] = p;
		           printf("%s won!\n", p->name);
		       }
		       else
		           printf("%s lost\n", p->name);
		  }
		  else
		       printf("%s is the best there is, there are no challengers\n", n);
		       flag = 1;
	      }
	          else
		p = p->next;
	 }


	 if(p == NULL)
		  printf("%s does not exist\n", n);

	 getch();

}

void display_storage(DDS D)
{
	 int ctr;
	 nodeptr p;

	 for(ctr = 0; ctr < max; ctr++)
	 {
		  printf("%d ", ctr);
		  for(p = D.header[ctr]; p != NULL; p = p->next)
				printf("%s ", p->name);
		  printf("\n");
	 }

	 getch();
}
void display_rank(DDS D)
{
	 int ctr;

	 printf("Ranking: \n");
	 for(ctr = 1; ctr <= D.nextrung; ctr++)
		  printf("%d. %s\n", D.rankladder[ctr]->rank, D.rankladder[ctr]->name);

	 getch();
}
void save(DDS D)
{
	 FILE *fp;

	 if((fp = fopen("DDSPrac.dat", "wb")) == 0)
	 {
		  printf("Cannot write on file\n");
		  exit(1);
	 }

	 fwrite(&D, sizeof(DDS), 1, fp);

	 printf("File Saved\n");
	 fclose(fp);

	 getch();
}

void load(DDS *D)
{
	 FILE *fp;
	 int ctr, ctr2 = 0;
	 nodeptr *p;

	 if((fp = fopen("DDSPrac.dat", "rb")) == 0)
	 {
		  printf("Cannot open file\n");
		  exit(1);
	 }
	 /*for(ctr = 0; ctr < max; ctr++)
	 {
		  p = &D->header[ctr];
			 do
			 {
				fread(p, sizeof(nodeptr), 1, fp);
				printf("%s\n", (*p)->name);
			 }while(*p != NULL);
	 }
	 do
	 {
		  p = &D->rankladder[ctr];
	 }while((fread(p, sizeof(nodeptr), 1, fp)) == 1);
	 */
	 fread(D, sizeof(DDS), 1, fp);
	 printf("File Loaded\n");

	 fclose(fp);

	 getch();

}
void main()
{
	 DDS D;
	 char n[24], check;
	 int h, exit = 0;

	 initialize(&D);

	 do
	 {
		  printf("What do you want to do? \n");
		  printf("Insert(I)\n");
		  printf("Challenge(C)\n");
		  printf("Display Storage(D)\n");
		  printf("Display Rank(R)\n");
		  printf("Save(S)\n");
		  printf("Load(L)\n");
		  printf("Quit(Q)\n");
		  check = getch();


		  if(check == 'I' || check == 'i')
		  {
				printf("Input a name: ");
				flushall();
				gets(n);

				insert(&D, n);
		  }
		  else if(check == 'D' || check == 'd')
				display_storage(D);
		  else if(check == 'C' || check == 'c')
		  {
				printf("Input the challenger's name: ");
				flushall();
				gets(n);

				challenge(&D, n);
		  }
		  else if(check == 'R' || check == 'r')
				display_rank(D);
		  else if(check == 'S' || check == 's')
				save(D);
		  else if(check == 'L' || check == 'l')
				load(&D);
		  else if(check == 'Q' || check == 'q')
				exit = 1;
		  else
				printf("Error\n");
		  clrscr();
	 }while(exit == 0);

}

Edited by Whilliam: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Tom Gunn
0

I can get it to save and load but when I try displaying it, its gonna have a General Protection Exception error.

Your data structure is probably getting corrupted during the load. Maybe you are not terminating one of the lists with NULL, or not handling NULL cells as a special case and trying to dereference them. The best way to troubleshoot is to find a small case that fails and step through it with a debugger while watching your memory for changes.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.