2012-04-22 10 views
0

Intro to Cクラスのプログラムでは、ジャンパーパズルを解くプログラムを書く必要があります。私の教授が私たちに与えた辞書のテキストファイル。辞書から単語をアルファベット順に並べ替え、テキストファイル(jumble.txtと呼ぶ)から雑音を取り込み、それらをアルファベット順に並べ替えて、文字列を比較して一致するものを見つけます。私はすべてのコードを書いていますが、それを実行しようとするとすぐにクラッシュし、理由を理解できません。私はおそらくStackoverflowのユーザーが私をここで助けることができるかもしれないと思った。すべてのあなたの助けを事前にJumble解決プログラムが実行時にクラッシュする

#include <stdio.h> 
#include <strings.h> 
#define MAX_WORD_LEN 6 
#define MAX_NUM_WORDS 30000 

typedef struct { 
    char word[MAX_WORD_LEN+1]; 
    char sort[MAX_WORD_LEN+1]; 
} jumble_type; 

void bubblesort(char letters[], int length); 


int main() { 
    int words, jumbles; 
    int j, q; 
    jumble_type list[MAX_NUM_WORDS]; 
    jumble_type list2[MAX_NUM_WORDS]; 


// Creating file pointers 
FILE *ifp; 
FILE *ifp2; 

//Opens Jumble and dictionary files and reads the info from them 
ifp = fopen("jumbledct.txt", "r"); 
ifp2 = fopen("jumble.txt", "r"); 

//Assigns the value of "words" to the first line of jumbledct.txt 
fscanf(ifp, "%d", words); 

//Assigns the value of "jumbles" to the first line of jumble.txt 
fscanf(ifp2, "%d", jumbles); 

// Reads the words from the dictionary into the "word" section of our 
// list structure. 
int i; 
for (i = 0; i < words; i++){ 
    fscanf(ifp, "%s", &list[i].word); 
    strcpy(list[i].sort, list[i].word); 
    bubblesort(list[i].sort, strlen(list[i].sort)); 
    printf("%s\n", list[i].sort); 
    } 

//Reads from Jumble.txt 
for (i = 0; i < jumbles; i++){ 
    fscanf (ifp2, "%s", &list2[i].word); 
    strcpy(list2[i].sort, list2[i].word); 
    bubblesort(list2[i].sort, strlen(list2[i].sort)); 
    //printf("%s\n", list2[i].sort); 
    } 

for(j=0;j<jumbles; j++){ 
     printf("JUMBLE PUZZLE # %d: %s\n", j+1, list2[j].word); 


    int x=0; 

for (q = 0; q < words; q++){ 



     if(strcmp(list2[j].sort, list[q].sort)==0){ 

      printf("%s\n", list[q].word); 
      x++; 
     } 
} 
if (x == 0){ 
      printf("Sorry, this puzzle has no solutions. \n\n"); 
     } 
     printf("\n"); 

} 


return 0; 
} 

void bubblesort(char letters[], int length) { 
char temp; 
    int x, y; 
    for(x = 0; x < length; x++){ 
     for (y = x; y < length; y++){ 
      if (letters[x] > letters[y]){ 
       temp = letters[y]; 
       letters[y] = letters[x]; 
      letters[x] = temp; 
     } 
    } 
} 
} 

ありがとう:

は、ここで私が持っているコードです。

+0

デバッガを使用して(またはいくつかのprintfsを追加して)、クラッシュする場所を知ることができます。 – John3136

+0

あなたのコメントはオフです。 'int'が' jumbledct.txt'の中で 'words'にスキャンされても、それ以外の方法でスキャンされることはありません。 (別の方法として、「単語の "** name **を...に割り当てる"ことができます)また、@Nick Atomsは正しいです: '* scanf'ポインタを渡す必要があります。 – aib

答えて

3

私のCは少し錆びますが、fscanf関数の3番目の引数はアドレス(&ワードと&ジャングルのようなもの)であってはいけませんか?

+0

それは、私は今とても信じられないほど愚かだと感じます。それを指摘していただきありがとうございます。 – Batteries

関連する問題