// Can someone explain what is wrong with this code? //it shows these error mess
ID: 3550403 • Letter: #
Question
// Can someone explain what is wrong with this code?
//it shows these error messages
//hangman.cpp: In function 'int main()':
//hangman.cpp:39: error: invalid conversion from 'const char*' to 'char'
//hangman.cpp: In function 'int processGuess(char*, const char*, char, int, int)':
//hangman.cpp:80: error: invalid conversion from 'const char*' to 'char'
// hangman.cpp
// Hangman game illustrates string library functions,
// character arrays, arrays of pointers, etc.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
// Prototype
int processGuess(char* word, const char* targetWord, char guess, int numTurns, int wordsize);
// Define an array of strings (since a string is just a char array)
// and since string are just char *'s, we really want an array of char *'s
const char *wordBank[] = {"computer","president","trojan","program","coffee",
"library","football","popcorn","science","engineer"};
const int numWords = 10;
int main()
{
srand(time(0));
char guess;
bool wordGuessed = false;
int numTurns = 10;
// Pick a random word from the wordBank
const char* targetWord = wordBank[rand()%10];
// More initialization code as needed
char word[80]; // a blank array to use to build up the answer
// It should be initialized with *'s and then
// change them to the actual letters when the
// user guesses the letter
int wordsize = strlen(targetWord);
for (int i=0; i<wordsize; i++) {
word[i] = "*";
}
while (numTurns != 0 || word != targetWord) {
processGuess(word, targetWord, guess, numTurns, wordsize);
numTurns--;
}
if (word == targetWord) {
cout << "The word was : " << word << ". " << "You win!" << endl;
}
else {
cout << "Too many turns... You lose!" << endl;;
}
return 0;
}
// An individual game continues until a word
// is guessed correctly or 10 turns have elapsed
// Print out end of game status
// Given the current version of the word, target/selected word, and
// the character that the user just guessed, change the word to
// "turn on" any occurrences the guess character and return a count
// of how many occurrences of 'guess' were found
int processGuess(char* word, const char* targetWord, char guess, int numTurns, int wordsize) {
cout << endl << "Current Word: " << word << endl;
cout << numTurns << " remain...Enter a letter to guess:" << endl;
cin >> guess;
for (int i=0; i<wordsize; i++) {
if (guess == targetWord[i]) {
word[i] = guess;
}
else {
word[i] = "*";
}
}
}
Explanation / Answer
You have written in your code
word[i] = "*";
which is wrong assignment as word[i] is character of type char* and "*" is a string representation of typr const char*
You can simply solve it by assigning a character to the given word.
i.e.
word[i] = '*';
Notice the inverted commas. Now I am assigning single character to word[i].
Run your code and enjoy :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.