Whats wrong with my code? I have attached the error prompt and the input file. Whenever I follow the error message, I get even more errors. Ty!! /* Here is the code to copy & paste to be easier to run and fix. Thank you!!!!*/
Whats wrong with my code? I have attached the error prompt and the input file. Whenever I follow the error message, I get even more errors. Ty!! /* Here is the code to copy & paste to be easier to run and fix. Thank you!!!!*/
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
100%
C++
Whats wrong with my code? I have attached the error prompt and the input file. Whenever I follow the error message, I get even more errors.
Ty!!
/* Here is the code to copy & paste to be easier to run and fix. Thank you!!!!*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// Prototypes
int read_words(string filename, string words[]);
bool check_guess(string &availableLetters, string &visibleLetter, string word, char guess, int &guessesLeft);
int main() {
// Seeding the random function
srand(time(0));
int n;
string words[100], filename = "PA7words.txt";
//reading words from a file
n = read_words(filename, words);
int guessesLeft = 7;
char guess;
string availableLetters = "abcdefghijklmnopqrstuvwzyz";
string visibleLetters = word;
//replace all visible letters with '-'
for (int i = 0; i < word.length(); i++) {
visibleLetters.at(i) = '-';
}
cout << "the word to guess has " << word.length() << " letters" << endl;
//loops to check the word
while (guessesLeft) {
//show hangman state
cout << visibleLetters << endl;
cout << endl << guessesLeft << " incorrect guesses left";
cout << "\nPlease enter your guess: ";
cin >> guess;
//call check guess
if (check_guess(availableLetters, visibleLetters, word, guess, guessesLeft)) {
break;
}
}
// if loop terminates without guesses left, game is lost
// otherwise game is won
if (guessesLeft)
cout << "\nCongrats! You guessed the word: " << word;
else
cout << "\nYou ran out of guesses!";
return 0;
}
// function to read words from a file
int read_words(string filename, string words[]) {
//open the file
ifstream infile("PA7words.txt");
int i = 0;
//populate the array
while (infile >> words[i])
i++;
//close the files
infile.close();
//return the number of words read
return i;
}
//this function checks whether nthe guess matches the word
//reurns true if the word is completely guessed
bool check_guess(string &availableLetters, string &visibleLetter, string word, char guess, int &guessesLeft) {
//check if guessed letter is available
if (availableLetters.at(guess - 'a') == ' ') {
cout << "\n" << guess << " is not an available letter.";
return false;
}
//replace the guessed letter with blankspace
else
availableLetters.at(guess - 'a') = ' ';
bool found = false;
//check if guessed letter is in the word
for (int i = 0; i < word.length(); i++) {
if (word[i] == guess) {
visibleLetter[i] = word[i];
found = true;
}
}
//if found a matching letter, print message
if (found)
cout << "Nice!" << guess << " is in the word" << endl;
else {
cout << guess << " is not in the word. To bad.";
guessesLeft--;
}
// if all letters have been guessed return true, else false
if (visibleLetter != word)
return false;
else
return true;
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps with 3 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education