I have my c project which works fine and compiles fine on my home machine (MacBook Pro Mac OSX). When I upload it to my school server and compile it there it compiles but I get a bus error when I try option 2. My program gets graded on the school server, so its important I get it to compile lol. My school server is some sort of linux machine, not sure which.
I think the problem code is here:
Any help is greatly appreciated.
I think the problem code is here:
Code:
head->paths[letter] = (node*)malloc(sizeof(node)); head->paths[letter]->c = word[i]; head->paths[letter]->setn = malloc((strlen(head->setn) +2) * sizeof(char)); strcpy(head->paths[letter]->setn,head->setn); head->paths[letter]->setn[strlen(head->setn)] = word[i]; head->paths[letter]->setn[strlen(head->setn)+1] = '\0'; head->paths[letter]->isWord = 0; head = head->paths[letter];
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char c; // the current char
char *setn; //total
int isWord; /* To check if this is a valid word to be written into dict.txt,
0 is false, 1 is true*/
struct node *paths[26]; // A subexpression can be followed by any of the 26 characters
};
typedef struct node node;
void suggest(node *n);
int main() {
int choice,i,letter,j;
int startWord = 0;
char word[100];
FILE *fp;
char c;
char cc[2];
cc[1] = '\0';
node *head = (node *) malloc(sizeof(node));
node *cur = (node *) malloc(sizeof(node));
node *paths[26];
for(i = 0;i<26;i++) {
paths[i] = (node *) malloc(sizeof(node));
paths[i]->c = (char)(i+65);
paths[i]->setn = malloc(2);
paths[i]->setn[0] = '\0';
cc[0] = (char)(i+65);
paths[i]->setn = strcat(paths[i]->setn,cc);
paths[i]->isWord = 0;
} //create tree root
while(1) {
printf("\n=============================================================================\n\n");
printf("1. Create a dictionary\n");
printf("2. Add a word\n");
printf("3. Suggest a word\n");
printf("4. Spell check\n");
printf("5. Save the dictionary\n");
printf("6. Exit\n\n");
printf("Enter choice: ");
scanf("%d",&choice);
if(choice == 1) {
//Build Dictionary
fp = fopen("dict.txt","r");
while((c = fgetc(fp)) != EOF) {
letter = (((int)c)-65);
if(c == '#') {
if(startWord == 0) {
startWord = 1;
head = 0;
}
else {
startWord = 0;
head->isWord = 1;
i = 0;
}
}
else if((int)c >= 65 && (int)c <= 90) {
if(head == 0) {
head = paths[letter];
}
else {
if(head->paths[letter] == 0) {
head->paths[letter] = (node *)malloc(sizeof(node));
head->paths[letter]->c = c;
head->paths[letter]->setn = malloc((strlen(head->setn) +2) * sizeof(char));
strcpy(head->paths[letter]->setn,head->setn);
head->paths[letter]->setn[strlen(head->setn)] = c;
head->paths[letter]->setn[strlen(head->setn)+1] = '\0';
head->paths[letter]->isWord = 0;
head = head->paths[letter];
}
else {
head = head->paths[letter];
}
}
}
}
printf("Dictionary has been created.");
fclose(fp);
head = 0;
}
if(choice == 2) {
//Add Word
printf("Enter word: ");
scanf("%s",word);
j = 0;
while(word[j] != '\0')
{j++;} //length
for(i = 0;i<j;i++) {
letter = (((int)word[i])-65);
if(head == 0) {
head = paths[letter];
}
else {
if(head->paths[letter] == 0) {
head->paths[letter] = (node*)malloc(sizeof(node));
head->paths[letter]->c = word[i];
head->paths[letter]->setn = malloc((strlen(head->setn) +2) * sizeof(char));
strcpy(head->paths[letter]->setn,head->setn);
head->paths[letter]->setn[strlen(head->setn)] = word[i];
head->paths[letter]->setn[strlen(head->setn)+1] = '\0';
head->paths[letter]->isWord = 0;
head = head->paths[letter];
}
else {
head = head->paths[letter];
}
}
}
head->isWord = 1;
printf("New word \"%s\" added to the dictionary",word);
head = 0;
}
if(choice == 3) {
//Suggest a word
printf("Enter subexpression: ");
scanf("%s",word);
j=0;
while(word[j] != '\0')
{j++;} //length
for(i = 0;i<j;i++) {
letter = (((int)word[i])-65);
if(head == 0) {
head = paths[letter];
}
else {
head = head->paths[letter];
}
}
printf("The suggested words are: ");
suggest(head);
head = 0;
}
if(choice == 4) {
//Spell Check
printf("Enter string: ");
scanf("%s",word);
j=0;
while (word[j] != '\0')
{j++;} //string length
for(i = 0;i<j;i++) {
letter = (((int)word[i])-65);
if(head == 0) {
head = paths[letter];
}
else {
if(head->paths[letter] == 0) {
printf("Result: Spelling is incorrect.");
break;
}
else {
head = head->paths[letter];
}
}
}
if(head->c == word[i-1]) {
printf("Result: Spelling is correct.");
}
head = 0;
}
if(choice == 5) {
//Save the Dictionary
}
if(choice == 6) {
//Exit
break;
}
}
}
void suggest(node *n) {
int i;
if(n->isWord == 1) {
printf("%s, ",n->setn);
}
for(i = 0;i<26;i++) {
if(n->paths[i] != 0) {
suggest(n->paths[i]);
}
}
}
Comment