int main() { char ch[ArSize] = "Snickers"; double weight = 3.12; int cals = 428; CandyBar bar; fill_bar(bar); show_bar(bar); fill_bar(bar, ch, weight, cals); show_bar(bar); return 0; }
/* plays a round of hangman, calling all appropriate functions Parameters/return type: none */ void playHangMan(void) { FILE* inFile; int col_index; int row_index; char word[MAX_NAME_LENGTH]; char wordarray[NUMROWS][MAX_NAME_LENGTH]; char copyarray[MAX_NAME_LENGTH] = { 0 }; char category[NUMCOLS][20] = { "Animals.txt", "Countries.txt", "Companies.txt", "Science.txt", "Palindromes.txt" }; int i = 0; int length; int flag = 0; int flag1 = 0; int index; int result; char guessed_letters[26]; int used_words[MAX_NAME_LENGTH] = { 0 }; //store column and row index alternating int already_used; do { memset(guessed_letters, 0, sizeof(guessed_letters)); //memset to reset arrays every time you play again for (index = 0; index < NUMROWS; index++) memset(wordarray[index], 0, sizeof(wordarray[index])); memset(word, 0, sizeof(word)); memset(copyarray, 0, sizeof(copyarray)); do { already_used = FALSE; col_index = rand() % NUMCOLS; if (col_index == 4) row_index = rand() % NUMCOLSPALINDROME; else row_index = rand() % NUMROWS; if (flag1 == 0) { used_words[0] = col_index; used_words[1] = row_index; flag1++; index = 2; } else { for (i = 0; i < MAX_NAME_LENGTH; i += 2) { if (used_words[i] == col_index) { if (used_words[i + 1] == row_index) already_used = TRUE; } } if (!already_used) { used_words[index] = col_index; used_words[index + 1] = row_index; index += 2; } } } while (already_used); inFile = fopen(category[col_index], "r"); if (inFile == NULL) printf("Error: could not locate file.\n"); else { i = 0; while (fscanf(inFile, "%s", &word) == 1) { copy_1D_to_2D(word, wordarray, i); i++; } length = strlen(wordarray[row_index]); for (i = 0; i < length; i++) { if (wordarray[row_index][i] == '-') { copyarray[i] = ' '; wordarray[row_index][i] = ' '; } else if (wordarray[row_index][i] == '\'') copyarray[i] = '\''; else if (wordarray[row_index][i] == '?') copyarray[i] = '?'; else if (wordarray[row_index][i] == ',') copyarray[i] = ','; else if (wordarray[row_index][i] == '.') copyarray[i] = '.'; else copyarray[i] = '_'; } copyarray[i] = '\0'; initializeHangMan(length, row_index, flag, copyarray, guessed_letters, 0); result = fill_bar(length, wordarray, row_index, guessed_letters, flag, copyarray); if (result == 0) printf("You lost.\n"); else printf("You won!\n"); fclose(inFile); } } while (doAgain()); }