Question

c++. I'm working on a hangman game. It has to utilize a class. It should show...

c++. I'm working on a hangman game. It has to utilize a class. It should show the number of attempts that have been made, the length of the word represented by dots, and then the alphabet. There also needs to be input validation. If the word was horse, the very first attempt at the user guessing should display " 1 ..... choose: abcdefghijklmnopqrstuvwxyz ". As the user guesses letters, the number of attempts should go up, the letter should be changed to a " . " in the alphabet, and if the letter was within the word that was generated, then the dots would be changed to that letter. With the previous example, if the user entered " h " then the screen would display " 2 h.... choose: abcdefg.ijklmnopqrstuvwxyz " I've got a string vector of the generated word, but I'm not sure if I need a second vector full of dots that equal the length of the first and if I need a vector for the alphabet.

This is what I have so far.

#pragma once

#include <vector>

#include <string>

using namespace std;

class wordguess {

private:

    int attempts;

    int location;

    string random_word;

    vector<string> string_vector;

public:

    void get_words();

    void generate_random();

    void get_guess();

};

#include "wordguess.h"

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <time.h>

#include <stdlib.h>

using namespace std;


void wordguess::get_words() {

    ifstream in("wordpool.txt");

    string str;

    while (getline(in, str)){

        if(str.size() > 0) {

            string_vector.push_back(str);

        }

    }

    in.close();

    // for (int i = 0; i < string_vector.size(); i++) {

    // cout << string_vector[i] << " ";

    // }

}

void wordguess::generate_random() {

    srand(time(0));

    int r = string_vector.size() -1;

    int random = rand() % (r + 1);

    random_word = string_vector[random];

    cout << random_word;

}

void wordguess::get_guess() {

    

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//======wordguess.h===============
#pragma once

#include <vector>

#include <string>

using namespace std;

class wordguess {

private:

int attempts;

int location;

string random_word;

vector<string> string_vector;

public:

void get_words();

void generate_random();

void get_guess();

};
//=============end of wordguess.h================


//======test.cpp======================
#include "wordguess.h"

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <time.h>

#include <stdlib.h>

using namespace std;

void wordguess::get_words() {

ifstream in("wordpool.txt");

string str;

while (getline(in, str)){

if(str.size() > 0) {

string_vector.push_back(str);

}

}

in.close();

// for (int i = 0; i < string_vector.size(); i++) {

// cout << string_vector[i] << " ";

// }

}

void wordguess::generate_random() {

srand(time(0));

int r = string_vector.size() -1;

int random = rand() % (r + 1);

random_word = string_vector[random];

//cout << random_word;

}

void wordguess::get_guess() {
   int attempt=0;
   char charIn;
   string userWord="";
   string alphabet="abcdefghijklmnopqrstuvwxyz";
  
   int correctGuess=0;
  
   for(int i=0;i<random_word.length();i++){
       userWord=userWord+".";
  
   }
  
   while(1){
       attempt++;
       cout<<attempt<<" "<<userWord<<" choose: "<<alphabet<<endl;
       cin>>charIn;
       for(int i=0;i<random_word.length();i++){
           if(random_word.at(i)==charIn){
               correctGuess++;
               string s(1,charIn);
               userWord.replace(i,1,s);
           }
       }
       alphabet.replace(charIn-97,1,".");
       if(correctGuess==random_word.length()){
           cout<<"Word is: "<<random_word<<endl;
           cout<<"Made correct guess in "<<attempt<<" attemppts"<<endl;
           break;
       }
   }

}
//-----------------------
int main(){
   wordguess W;
   W.get_words();
   W.generate_random();
   W.get_guess();
   return 0;
}
//================end of test.cpp====================
wordpool.txt

players
might
actually
guess
working
off
the
assumption
that
average
person
would
guess
common
letters
like

output:

Add a comment
Know the answer?
Add Answer to:
c++. I'm working on a hangman game. It has to utilize a class. It should show...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • Hangman using while and fo loops, if's, and functions.

    I would like to preface this by saying this is NOT a current homework assignment; but it is an assignment from 3 years ago before I dropped out of school. I am self teaching and am revisiting an old assignment. I am NOT asking for the entire program, I'm simply looking for help building the skeleton for the initial start of the game.MORE INFO: Player 1 will enter word(of any length / i have been using "Testing") for Player 2...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two...

    Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two functions: encryption and decryption. 2. Use any key you like. (Don't use deceptive in the slides) 3. choose a sentence or a paragraph you like as the plaintext. I have the code I just need the implementation in a different way // C++ code to implement Vigenere Cipher #include<bits/stdc++.h> using namespace std; // This function generates the key in // a cyclic manner until...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    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...

  • Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for...

    Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for push_back() function as below: void push_back(string str) { // increase vector size by one // initialize the new element with str } In addition, the standard library vector doesn't provide push_front(). Implement push_front() for your vector. Test your code with the main function below. int main() {    vector v1(3);    cout<<"v1: ";    v1.print(); // this should display -, -, -    for...

  • *****Complete void example 4, 7, 8, 9, 10 #include <iostream> #include <fstream> #include <vector> #include <string>...

    *****Complete void example 4, 7, 8, 9, 10 #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; void rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") { str.erase(str.find_last_not_of(chars) + 1); } vector<string> *get_words() { vector<string> *words = new vector<string>(); fstream infile; string word; infile.open("words.txt"); getline(infile, word); while(infile) { rtrim(word);     words->push_back(word);     getline(infile, word); } return words; } void example_1(vector<string> *words) { // find words that contain the substring 'pet' and 'cat' // HINT: use find(str, p) method....

  • In C++ write a program that will read three strings from the user, a phrase, a...

    In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi". in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter...

  • C++ Create a program that finds the dot product of two vectors. I'm currently trying to...

    C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...

  • C++ Write a recursive function that reverses the given input string. No loops allowed, only use...

    C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sherry";    reverse(name);    cout << name << endl; //should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT