无论出于何种原因,for
循环getLetters
都会导致崩溃。
我无法在其中挑选出特定的来源,也许有人可以提供帮助?
我想我可能做错了我的指针。我应该如何解决这个问题?
#include <stdio.h>
#include <string.h>
#include <time.h>
int main() {
FILE *ifp;
ifp = fopen("dictionary.txt", "r");
int* lValues[26];
int* lDist[26];
int* lUsed[26];
int*fRun = 0;
int dictLen;
int i;
int bestValue;
fscanf(ifp, "%d", &dictLen);
char bestWord[7];
char dictionary[dictLen][7];
char* letters[7];
char* userWord[7];
//reads the dictionary into the array
for (i = 0; i < dictLen; i++) {
fscanf(ifp, "%s", &dictionary[i]);
}
distribution();
values();
while (i != 2) {
getLetters();
printf("Welcome to the Scrabble Practice Program!\n");
printf("\nHere are your letters: %s \n", letters);
printf("\nWhat would you like to do?\n");
printf("\t1-Enter Word\n");
printf("\t2-Quit\n\n");
printf("User Selection:");
scanf("%d", &i);
}
//fclose(ifp); not sure why, but this crashes the program
return 0;
}
//Gets the user's set of letters
void getLetters() {
char* letters[7];
int* lDist[26];
int* lUsed[26];
int lCur;
int i;
srand(time(0));
for (i = 0; i < 7; i++) {
*letters[i] = 65 + rand()%26;
lCur = (int)*letters[i] - 65;
while (*lUsed[lCur] >= *lDist[lCur]){
*letters[i] = 65 + rand()%26;
lCur = (int)*letters[i] - 65;
}
*lUsed[lCur]++;
}
}
//Sets the number of each letter available for distribution
void distribution() {
int* lDist;
//Number for A
lDist[0] = 9;
//Number for B
lDist[1] = 2;
//Number for C
lDist[2] = 2;
//Number for D
lDist[3] = 4;
//Number for E
lDist[4] = 12;
//Number for F
lDist[5] = 2;
//Number for G
lDist[6] = 3;
//Number for H
lDist[7] = 2;
//Number for I
lDist[8] = 9;
//Number for J
lDist[9] = 1;
//Number for K
lDist[10] = 1;
//Number for L
lDist[11] = 4;
//Number for M
lDist[12] = 2;
//Number for N
lDist[13] = 6;
//Number for O
lDist[14] = 8;
//Number for P
lDist[15] = 2;
//Number for Q
lDist[16] = 1;
//Number for R
lDist[17] = 6;
//Number for S
lDist[18] = 4;
//Number for T
lDist[19] = 6;
//Number for U
lDist[20] = 4;
//Number for V
lDist[21] = 2;
//Number for W
lDist[22] = 2;
//Number for X
lDist[23] = 1;
//Number for Y
lDist[24] = 2;
//Number for Z
lDist[25] = 1;
}
//Sets the value of all of the letters
void values() {
int* lValues;
//Value of A
lValues[0] = 1;
//Value of B
lValues[1] = 3;
//Value of C
lValues[2] = 3;
//Value of D
lValues[3] = 2;
//Value of E
lValues[4] = 1;
//Value of F
lValues[5] = 4;
//Value of G
lValues[6] = 2;
//Value of H
lValues[7] = 4;
//Value of I
lValues[8] = 1;
//Value of J
lValues[9] = 8;
//Value of K
lValues[10] = 5;
//Value of L
lValues[11] = 1;
//Value of M
lValues[12] = 3;
//Value of N
lValues[13] = 1;
//Value of O
lValues[14] = 1;
//Value of P
lValues[15] = 3;
//Value of Q
lValues[16] = 10;
//Value of R
lValues[17] = 1;
//Value of S
lValues[18] = 1;
//Value of T
lValues[19] = 1;
//Value of U
lValues[20] = 1;
//Value of V
lValues[21] = 4;
//Value of W
lValues[22] = 4;
//Value of X
lValues[23] = 8;
//Value of Y
lValues[24] = 4;
//Value of Z
lValues[25] = 10;
}