My assignment was to convert a C++ program into a C program. This program is like the game Hangman. My problem is the code compiles and runs in Unix VI editor, but the output is not what I want it to be.
My professor's sample output was this:
Please enter a word: howdy
The word is 5 characters
What letter would you like to guess? (Enter zero to quit.) a
There are 0 a's.
What letter would you like to guess? (Enter zero to quit.) e
There are 0 e's.
What letter would you like to guess? (Enter zero to quit.) d
There are 1 d's.
What letter would you like to guess? (Enter zero to quit.) o
There are 1 o's.
What letter would you like to guess? (Enter zero to quit.) y
There are 1 y's.
What letter would you like to guess? (Enter zero to quit.) b
There are 0 b's.
What letter would you like to guess? (Enter zero to quit.) 0
You found these letters
d o y
However, I am getting this:
Please enter a word: howdy
The word is 13 characters
What letter would you like to guess? (Enter zero to quit.) h
There are 1 h's.
What letter would you like to guess? (Enter zero to quit.) o
There are 0 o's.
What letter would you like to guess? (Enter zero to quit.) w
There are 0 w's.
What letter would you like to guess? (Enter zero to quit.) d
There are 0 d's.
What letter would you like to guess? (Enter zero to quit.) y
There are 0 y's.
You found these letters
h
Please tell me what I did wrong, thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// function prototypes
/* The function instruct describes the use
and purpose of the program. */
void instruct();
/* the function getWord reads the word
entered */
char* getWord();
/* the function findLetter searches the word
for a letter and returns the number of times
the letter appears in the word */
int findLetter (char letter, int number, char* word);
/* the function getGuess prompts the user for
the letter and returns the letter */
char getGuess();
/* the function display displays the letters
found in the word */
void display (const int alphabet[]);
int main()
{
// declare variables
/* a char pointer variable Word to read in the word,
an array of characters called word, since C doesn't use string,
a character variable guess to enter the letter,
an integer variable number for the length of the word,
an integer variable occurrence for the number
of times the letter appears in the word,
and an int array alphabet to indicate
if the letter has been found */
char* word;
char wordGuess[25];
char guess;
int number, occurrence;
int alphabet[26] = {0};
// call the function instruct
instruct();
// call the function getWord to read in the word
word = getWord();
// find the length of the word
number = strlen(word);
strcpy (wordGuess, word);
// display a message showing the length of the word
printf("The word is %d characters\n", number);
// call the function getGuess to read the letter
guess = getGuess();
// loop until a 0 is entered
while (guess)
{
/*call the function find letter to find the number
occurrences of the letter in the word */
occurrence = findLetter(guess, number, wordGuess);
// display the number of occurrences of the letter
printf("There are %d %c s.", occurrence, guess);
/* set the array element corresponding with the
letter to true */
if (occurrence)
alphabet[guess-97] = 1;
// call the function getGuess to read the letter
guess = getGuess();
}
display (alphabet);
return 0;
}
// function prototypes
/* The function instruct describes the use
and purpose of the program. */
void instruct()
{
printf("This program prompts the user for "
"a word and then a letter.\n"
"The word is searched to find the letter.\n"
"The user can enter more letters until a "
"zero is entered.\nThe program displays the "
"number of times a letter is found, and all "
"the letters found in the word.\n\n");
}
/* the function display displays the letters
found in the word */
void display (const int alphabet[])
{
printf("You found these letters\n");
/* loop through the array for the
letters found */
int count;
for (count = 0; count < 26; count++)
{
if (alphabet[count])
printf("%c \t", (count+97));
}
}
/* the function getWord reads the word
entered */
char* getWord()
{
int len = 0;
char word[25];
printf("Please enter a word: ");
scanf("%s", &word);
return &word[0];
}
/* the function getGuess prompts the user for
the letter and returns the letter */
char getGuess()
{
char guess = '0';
while (guess < 97 || guess > 122)
{
printf("\nWhat letter would you like to guess?"
"(Enter zero to quit.)\n");
scanf("%c", &guess);
if (guess == '\n')
scanf("%c", &guess);
guess = tolower(guess);
if (guess == '0')
return 0;
}
return guess;
}
/* the function findLetter searches the word
for a letter and returns the number of times
the letter appears in the word */
int findLetter (char letter, int number, char* word)
{
// declare variables
/* declare an integer variable foundLetter
initialized to zero to count the number of times
the letter appears in the word, an integer
variable start initalized to 0 to indicate
the starting search location in the word,
an integer variable stop initialized to the
length of the word to indicate the end
location in the word */
int foundLetter = 0, start = 0, stop = number;
while (word[start] != '\0')
{
if (toupper(word[start]) == toupper(letter))
foundLetter++;
start++;
}
// return the number of occurences in the letter
return foundLetter;
}
|