Need help with this C program?
I cannot get it to compile. I have to use Microsoft Studio compiler for my course.
It's a word game style program and I can't figure out where the issue is.
\* Program *\
// Michael Paul Laessig, 07 / 17 / 2019.
/*COP2220 Second Large Program (LargeProg2.c).*/
#define _CRT_SECURE_NO_DEPRECATE
//Include the following libraries in the preprocessor directives:
stdio.h, string.h, ctype.h
#include <stdio.h> /*printf, scanf definitions*/
#include <string.h> /*stings definitions*/
#include <ctype.h> /*toupper, tolower definitions*/
#define MAXGUESSES 5
#define WORDSIZE 25
// Function Prototypes.
void GameRules(void);
void LowerCaseWord(char word[WORDSIZE]);
void CreateSecretWord(char solution[WORDSIZE], char
secretword[WORDSIZE]);
void PlayOneGame(char solution[WORDSIZE], char
secretword[WORDSIZE]);
void GetTheLetterGuess(char lettersGuessed[WORDSIZE], char
*letterPtr, int *numPtr);
void ReplaceDash(char solution[WORDSIZE], char
secretword[WORDSIZE], char letter);
void DidYouWin(char solution[WORDSIZE], char
guess[WORDSIZE]);
void PlayAgain(int *againPtr);
int main(void)
{
char solution[WORDSIZE], secretword[WORDSIZE];
int again = 1;
printf("Welcome to the word guessing game!\n");
GameRules();
FILE *WordGame;
WordGame = fopen("inputWords.txt", "r");
if(WordGame != NULL) {
do {
fscanf(WordGame, "%s", word);
LowerCaseWord(solution);
CreateSecretWord(solution, secretword);
PlayOneGame(solution, secretword);
PlayAgain(&again);
} while (again == 1);
fclose(WordGame);
else {
printf("Unable to open file inputWords.txt");
}
return 0;
}
// Function Definitions.
//this function provides instructions to the user on how to play
the game
void GameRules(void) {
printf("\nIn this game you will try to guess the
Secret Word.\n");
printf("\nYou will guess each character of the Secret
Word one at a time.\n");
printf("\nYou will be allowed a maximum of 5
guesses.\n");
printf("\nYou will be notified of each correct
guess.\n");
printf("\nYou will be also notified of each incorrect
guess.\n");
printf("\nEach correct guess will reveal part of the
Secret Word piece by piece.\n");
printf("\nThe game will continue until either you have
guessed the Secret Word,");
printf("or you have exhausted all 5 of your
guesses.\n");
printf("\nAt such time you will be allowed to either
try your luck again,");
printf("or discontinue playing and exit the
game.\n");
}
//this function changes a character array to all lowercase
letters
void LowerCaseWord(char word[WORDSIZE]) {
int i, length_A;
length_A = strlen(word);
for (i = 0; i < length_A; i++) {gbv43r
word[i] = tolower(word[i]);
}
}
//this function creates the secret word array that is all
dashes
void CreateSecretWord(char solution[WORDSIZE], char
secretword[WORDSIZE]) {
int j, length_B;
length_B = strlen(solution);
for (j = 0; j < length_B; j++) {
secretword[j] = '-';
}
secretword[j] = '\0';
}
// this function runs one round of the game
//input: solution word from the file and the secret word made of
dashes
//void return type
//all other functions to Play one round of a game
//are called from within the PlayOneGame function
void PlayOneGame(char solution[WORDSIZE], char
secretword[WORDSIZE]) {
char lettersGuessed[WORDSIZE],
word[WORDSIZE];
char letter;
int count, k;
printf("\nOkay lets begin!\n");
printf("\nThe Secret Word is: ");
for (k = 0; k < strlen(secretword); k++) {
printf("%c", secretword[i]);
printf("\n");
}
for (count = 0; count < MAXGUESSES; count++)
{
printf("\n---------------------------------------------\n");
GetTheLetterGuess(lettersGuessed,
&letter, &count);
ReplaceDash(solution, secretword,
&letter);
}
printf("\nNow enter your guess for the word: ");
scanf("%s", word);
DidYouWin(solution, word);
}
//this function gets the users guess letter
//changes it to lowercase
//and adds it to the lettersGuessed array
void GetTheLetterGuess(char lettersGuessed[WORDSIZE], char
*letterPtr, int *numPtr) {
printf("\nNow make a letter guess: ");
scanf(" %c", letterPtr);
*letterPtr = tolower(*letterPtr);
letterGuessed[*numPtr] = *letterPtr;
}
//this function replaces any dash in the secretword with the
current character entered by the user
//this function will let the user know if the letter was in the
word
void ReplaceDash(char solution[WORDSIZE], char
secretword[WORDSIZE], char letter) {
int l, found;
for (l = 0; l < strlen(solution); l++) {
if (solution[l] == letter) {
found = 1;
}
}
if (found) {
printf("\nThe letter %c was present
in the secret word", letter);
}
else {
printf("\nThe letter %c was not
present in the secret word", letter);
}
printf("\nThe Secret Word is: ");
for(l = 0; l < strlen(secretword); l++)
printf("%c",secretword[l]);
printf("\n");
}
//this function compares the solution and the guess word
//tells the user if they have correctly guessed the word
void DidYouWin(char solution[WORDSIZE], char guess[WORDSIZE]) {
int comp_2;
comp_2 = strcmp(solution, guess);
if (comp_2 == 0) {
printf("\nYou guessed the Secret
Word correctly!\n");
printf("\nThe Secret Word was: %s",
solution);
printf("\n");
}
else {
printf("\nSorry you guessed the
Secret Word incorrectly...\n");
printf("\nThe Secret Word was: %s",
solution);
printf("\n");
}
}
//this function asks the user if they want to play another game
(1 to continue, 0 to Quit)
void PlayAgain(int *againPtr) {
printf("\nDo you wish to play again?\n");
printf("\nEnter the number 1 for Yes and the number 0
for No.\n");
scanf(" %d", againPtr);
}
\* End of Program *\
Here's the code after removing all errors:
/*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower definitions*/ #define MAXGUESSES 5 #define WORDSIZE 25 // Function Prototypes. void GameRules(void); void LowerCaseWord(char word[WORDSIZE]); void CreateSecretWord(char solution[WORDSIZE], char secretword[WORDSIZE]); void PlayOneGame(char solution[WORDSIZE], char secretword[WORDSIZE]); void GetTheLetterGuess(char lettersGuessed[WORDSIZE], char *letterPtr, int *numPtr); void ReplaceDash(char solution[WORDSIZE], char secretword[WORDSIZE], char letter); void DidYouWin(char solution[WORDSIZE], char guess[WORDSIZE]); void PlayAgain(int *againPtr); int main(void) { char solution[WORDSIZE], secretword[WORDSIZE]; int again = 1; printf("Welcome to the word guessing game!\n"); GameRules(); FILE *WordGame; WordGame = fopen("inputWords.txt", "r"); if (WordGame != NULL) { do { fscanf(WordGame, "%s", WordGame); LowerCaseWord(solution); CreateSecretWord(solution, secretword); PlayOneGame(solution, secretword); PlayAgain(&again); } while (again == 1); fclose(WordGame); } else { printf("Unable to open file inputWords.txt"); } return 0; } // Function Definitions. //this function provides instructions to the user on how to play the game void GameRules(void) { printf("\nIn this game you will try to guess the Secret Word.\n"); printf("\nYou will guess each character of the Secret Word one at a time.\n"); printf("\nYou will be allowed a maximum of 5 guesses.\n"); printf("\nYou will be notified of each correct guess.\n"); printf("\nYou will be also notified of each incorrect guess.\n"); printf("\nEach correct guess will reveal part of the Secret Word piece by piece.\n"); printf("\nThe game will continue until either you have guessed the Secret Word,"); printf("or you have exhausted all 5 of your guesses.\n"); printf("\nAt such time you will be allowed to either try your luck again,"); printf("or discontinue playing and exit the game.\n"); } //this function changes a character array to all lowercase letters void LowerCaseWord(char word[WORDSIZE]) { int i, length_A; length_A = strlen(word); for (i = 0; i < length_A; i++) { word[i] = tolower(word[i]); } } //this function creates the secret word array that is all dashes void CreateSecretWord(char solution[WORDSIZE], char secretword[WORDSIZE]) { int j, length_B; length_B = strlen(solution); for (j = 0; j < length_B; j++) { secretword[j] = '-'; } secretword[j] = '\0'; } // this function runs one round of the game //input: solution word from the file and the secret word made of dashes //void return type //all other functions to Play one round of a game //are called from within the PlayOneGame function void PlayOneGame(char solution[WORDSIZE], char secretword[WORDSIZE]) { char lettersGuessed[WORDSIZE], word[WORDSIZE]; char letter; int count, k; printf("\nOkay lets begin!\n"); printf("\nThe Secret Word is: "); for (k = 0; k < strlen(secretword); k++) { printf("%c", secretword[k]); printf("\n"); } for (count = 0; count < MAXGUESSES; count++) { printf("\n---------------------------------------------\n"); GetTheLetterGuess(lettersGuessed, &letter, &count); ReplaceDash(solution, secretword, letter); } printf("\nNow enter your guess for the word: "); scanf("%s", word); DidYouWin(solution, word); } //this function gets the users guess letter //changes it to lowercase //and adds it to the lettersGuessed array void GetTheLetterGuess(char lettersGuessed[WORDSIZE], char *letterPtr, int *numPtr) { printf("\nNow make a letter guess: "); scanf(" %c", letterPtr); *letterPtr = tolower(*letterPtr); lettersGuessed[*numPtr] = *letterPtr; } //this function replaces any dash in the secretword with the current character entered by the user //this function will let the user know if the letter was in the word void ReplaceDash(char solution[WORDSIZE], char secretword[WORDSIZE], char letter) { int l, found; for (l = 0; l < strlen(solution); l++) { if (solution[l] == letter) { found = 1; } } if (found) { printf("\nThe letter %c was present in the secret word", letter); } else { printf("\nThe letter %c was not present in the secret word", letter); } printf("\nThe Secret Word is: "); for(l = 0; l < strlen(secretword); l++) printf("%c",secretword[l]); printf("\n"); } //this function compares the solution and the guess word //tells the user if they have correctly guessed the word void DidYouWin(char solution[WORDSIZE], char guess[WORDSIZE]) { int comp_2; comp_2 = strcmp(solution, guess); if (comp_2 == 0) { printf("\nYou guessed the Secret Word correctly!\n"); printf("\nThe Secret Word was: %s", solution); printf("\n"); } else { printf("\nSorry you guessed the Secret Word incorrectly...\n"); printf("\nThe Secret Word was: %s", solution); printf("\n"); } } //this function asks the user if they want to play another game (1 to continue, 0 to Quit) void PlayAgain(int *againPtr) { printf("\nDo you wish to play again?\n"); printf("\nEnter the number 1 for Yes and the number 0 for No.\n"); scanf(" %d", againPtr); }
OUTPUT:
FOR ANY HELP JUST DROP A COMMENT
Need help with this C program? I cannot get it to compile. I have to use...
I am trying to figure out why my C code is outputting "exited, segmentation fault". The game is supposed to generate 4 random numbers and store them in the "secret" array. Then the user is suppose to guess the secret code. The program also calculates the score of the user's guess. For now, I printed out the random secret code that has been generated, but when the game continues, it will output "exited, segmentation fault". Also, the GetSecretCode function has...
C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...
JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...
Question: Please Provide Comments on each Line of code explaining what the C Function is doing throughout the code. // Function used for substitution encryption void SubEncrypt(char *message, char *encryptKey) { int iteration = 0; printf("Enter Aphabet Encryption Key: \n"); scanf("%s", encryptKey); for (iteration = 0; iteration < strlen(message); iteration++) { char letter = message[iteration]; if (letter >= 'A' && letter <= 'Z') { letter = encryptKey[letter - 'A']; } message[iteration] = letter; } printf("CipherText message: %s\n", message); } //_________________________________________________________________________________________________________________________________________________...
For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...
C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } } return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...
Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1. Select a word at random from the wordBank. This is done for you. 2. On each turn display the word, with letters not yet guessed showing as *'s, and letters that have been guessed showing in their correct location 3. The user should have 10 attempts (?lives?). Each unsuccessful guess costs one attempt. Successful guesses do NOT count as a turn. 4. You must...
Missing multiple labeled functions. Card matching game in C. Shouldn't need any more functions. I am lost on how to complete the main function (play_card_match) without the sub functions complete. The program runs fine as is, but does not actually have a working turn system. It should display question marks on unflipped cards when the player is taking a turn and should also clear the screen so the player can't scroll up to cheat. Thank you #include "cardMatch.h" //main function /*...
C program help: Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main(). example: #include void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints int main () { int numDucks,numCats; getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to...