Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi so i have started c++ 3 days ago, read some tutorials etc. I wanted to make my own hangman game since it seemed as an easy task for a begginer but i stumbled upon an issue. Everything is working well ecept i cant seem to find a way to make 1 letter strings swap with underscores until underscores change to the missing word. So basically when you compile it you can guess the whole word only.
Here is the code:
#include <iostream>
#include <string>
using namespace std;
string player1,player2,word,underscore,guess;
int wrong=0;
int main (){
string copy = word;
cout << "----------------------Hello! Welcome to the HANGMAN game!---------- ----------" << endl;
cout << "Please type in your name, PLAYER 1" << endl;
cin >> player1;
cout << "Please type in your name, PLAYER 2" << endl;
cin >> player2;
cout << "OK " << player1 << " and " << player2 << ". Let's start with the game!" << endl;
cout << player1 << " please input the word you want " << player2 << " to guess." << endl;
cin >> word;
//space
for (int x=0; x<30; x++){
cout << endl;
}
//UNDERSCORE
while (underscore.size() != word.size()){
underscore.push_back('_');}
cout << underscore << endl;
//MAIN WHILE
while(wrong<12){
cin >> guess;
//IF GUESS ISNT LETTER
if(guess.size() > 1){
if(guess==word){
cout << "Thats the right word." << endl;
break;
}
else{
cout << underscore << endl;
cout << "Wrong word try again." << endl;
cout << "Used: " << usedguess << endl;
wrong ++;
}
}
if(underscore == word){
cout << "You win!" << endl;
break;
}
if(wrong==1){
cout << "I" << endl;
}
else if(wrong==2){
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==3){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==4){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==5){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==6){
cout << "I===" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==7){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==8){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I |" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==9){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==10){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==11){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I /" << endl;
cout << "I" << endl;
}
else if(wrong==12){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I / /"<< endl;
cout << "I YOU ARE DEAD" << endl;
cout << "Game over bro! The word was: " << word <<endl;
break;
}
}
}
To compare the strings of word and guess, you can iterate over the characters in a for-loop, and check if there is a match
string word = "hangman";
string guess = "mansomething";
string underscore = string(word.size(), '_'); // init a string with underscores equal to the length of 'word'
// iterate over the characters in word and guess
for (size_t i = 0, iend = min(word.size(), guess.size()); i < iend; i++) {
if (word[i] == guess[i])
underscore[i] = word[i]; // if the characters match at position i, update the underscore.
}
cout << underscore << endl;
Afterwards, underscore contains the following
_an____
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Link to Code: https://onlinegdb.com/B1DsFDa8D
Hello, my while loops aren't responding to their set conditions once the value is updated. the code just keeps going until it reaches the end. If you run the code you'll see what I'm talking about(in fact it is necessary that you do so). When the value of the parameter is updated the value does add up and changes. When I print out the value of it at the end of the code it does in fact register as the number that should be assigned to it throughout the loop but the loop just doesn't do anything when it should be stopped. Basically my problem might be an infinite loop. the only thing that counters it is a "return 0;" at the end of the loop.
My professor gave specific instructions: Create a hangman game. The game usually involves one player guessing letters to a secret word. Bad guesses cause the picture of a hangman to be drawn one segment at a time. Once there are 7 bad guesses, the hangman picture has been drawn and the player guessing loses the game. In your game, seven bad answers to any of 16 multiple choice questions will result in a lost game.
Program 1 Writes 16 questions to a file called “infile.txt”
Program 2 reads “infile.txt” and uses the questions for the hangman game
Use a boolean value-returning function called "is_hung". This function takes an integer parameter called "num_errors". This parameter is tested using an "if-else" statement to determine how much of the hangman to display to the screen based on the number of wrong answers.
The entire hangman can be displayed by the code segment below:
{
cout << "\t \t \t" << " O " << endl;
cout << "\t \t \t" << "/|\\" << endl;
cout << "\t \t \t" << " | " << endl;
cout << "\t \t \t" << "/ \\" << endl;
cout << " YOU ARE HUNG" endl;
return false;
}
1 incorrect answer displays the head
2 incorrect answers displays the left arm
3 incorrect answers displays the right arm
4 incorrect answers displays the top half of the body
5 incorrect answers displays the bottom half of the body
6 incorrect answers displays the left leg
7 incorrect answers displays the right leg
My Code:
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
bool is_hung(int, bool);
int main()
{
string question1;
string answer1;
string answer2;
string answer3;
string answer4;
string question2;
string answer5;
string answer6;
string answer7;
string answer8;
string question3;
string answer9;
string answer10;
string answer11;
string answer12;
string question4;
string answer13;
string answer14;
string answer15;
string answer16;
string question5;
string answer17;
string answer18;
string answer19;
string answer20;
string question6;
string answer21;
string answer22;
string answer23;
string answer24;
string question7;
string answer25;
string answer26;
string answer27;
string answer28;
string question8;
string answer29;
string answer30;
string answer31;
string answer32;
ifstream reader;
reader.open("infile.txt");
int num_right = 0;
int num_error = 0;
bool power = true;
cout << power << endl;
string user_answer1 = "";
string user_answer2 = "";
string user_answer3 = "";
string user_answer4 = "";
string user_answer5 = "";
string user_answer6 = "";
string user_answer7 = "";
string user_answer8 = "";
while (power == true) {
while (num_error < 7) //should stop the while loop when number of incorrect answers are equal to 7
{
//1
getline(reader, question1); //get line from infile.txt
getline(reader, answer1);
getline(reader, answer2);
getline(reader, answer3);
getline(reader, answer4);
cout << question1 << endl; //print out line from file
cout << answer1 << endl;
cout << answer2 << endl;
cout << answer3 << endl;
cout << answer4 << endl;
cout << "Enter Answer: ";
cin >> user_answer1; //have the user input answer
if (user_answer1 == "B") //if statement to determine correct answer
{
cout << "correct" << endl;
num_right++; //adds 1 to num_right
cout << "" << endl;
}
else //if user_answer is not equal answer/wrong answer
{
cout << "incorrect" << endl;
num_error++; //add 1 to num_error
power = is_hung(num_error, power); //get function
}
//2
getline(reader, question2);
getline(reader, answer5);
getline(reader, answer6);
getline(reader, answer7);
getline(reader, answer8);
cout << question2 << endl;
cout << answer5 << endl;
cout << answer6 << endl;
cout << answer7 << endl;
cout << answer8 << endl;
cout << "Enter Answer: ";
cin >> user_answer2;
if (user_answer2 == "B") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//3
getline(reader, question3);
getline(reader, answer9);
getline(reader, answer10);
getline(reader, answer11);
getline(reader, answer12);
cout << question3 << endl;
cout << answer9 << endl;
cout << answer10 << endl;
cout << answer11 << endl;
cout << answer12 << endl;
cout << "Enter Answer: ";
cin >> user_answer3;
if (user_answer3 == "C") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//4
getline(reader, question4);
getline(reader, answer13);
getline(reader, answer14);
getline(reader, answer15);
getline(reader, answer16);
cout << question4 << endl;
cout << answer13 << endl;
cout << answer14 << endl;
cout << answer15 << endl;
cout << answer16 << endl;
cout << "Enter Answer: ";
cin >> user_answer4;
if (user_answer4 == "D") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//5
getline(reader, question5);
getline(reader, answer17);
getline(reader, answer18);
getline(reader, answer19);
getline(reader, answer20);
cout << question5 << endl;
cout << answer17 << endl;
cout << answer18 << endl;
cout << answer19 << endl;
cout << answer20 << endl;
cout << "Enter Answer: ";
cin >> user_answer5;
if (user_answer5 == "A") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//6
getline(reader, question6);
getline(reader, answer21);
getline(reader, answer22);
getline(reader, answer23);
getline(reader, answer24);
cout << question6 << endl;
cout << answer21 << endl;
cout << answer22 << endl;
cout << answer23 << endl;
cout << answer24 << endl;
cout << "Enter Answer: ";
cin >> user_answer6;
if (user_answer6 == "D") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//7
getline(reader, question7);
getline(reader, answer25);
getline(reader, answer26);
getline(reader, answer27);
getline(reader, answer28);
cout << question7 << endl;
cout << answer25 << endl;
cout << answer26 << endl;
cout << answer27 << endl;
cout << answer28 << endl;
cout << "Enter Answer: ";
cin >> user_answer7;
if (user_answer7 == "A") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
cout << power << endl;
//8
getline(reader, question8);
getline(reader, answer29);
getline(reader, answer30);
getline(reader, answer31);
getline(reader, answer32);
cout << question8 << endl;
cout << answer29 << endl;
cout << answer30 << endl;
cout << answer31 << endl;
cout << answer32 << endl;
cout << "Enter Answer: ";
cin >> user_answer8;
if (user_answer8 == "D") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
cout << "" << endl;
if (num_error > 7) {
cout << "You got " << num_right << " out of 8. You lose." << endl;
}
else if (num_error < 7) {
cout << "You got " << num_right << " out of 8. You Win." << endl;
}
reader.close();
return 0;
}
}
reader.close();
cout << "" << endl;
if (num_error > 7) {
cout << "You got " << num_right << " out of 16. You lose." << endl;
}
else if (num_error < 7) {
cout << "You got " << num_right << " out of 16. You Win." << endl;
}
return 0;
}
bool is_hung(int num_errors2, bool power2)
{
if (num_errors2 == 1) {
cout << "\t \t \t"
<< " O " << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 2) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/" << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 3) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/ \\" << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 4) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 5) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "\t \t \t"
<< " | " << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 6) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "\t \t \t"
<< " | " << endl;
cout << "\t \t \t"
<< "/ " << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 7) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "\t \t \t"
<< " | " << endl;
cout << "\t \t \t"
<< "/ \\" << endl;
cout << " YOU ARE HUNG" << endl;
cout << "" << endl;
power2 = false;
return power2;
}
}
There is a ridiculous amount of wasteful, repetitive code, which makes it extremely difficult to read and debug, and significantly increases the risk of making mistakes.
This code can be greatly reduced by making use of fewer variables, better loops, and a little arithmetic, eg:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool is_hung(int);
int main()
{
string question;
string answer;
string user_answer;
const string correct_answers = “BBCDADADBDABCABB”;
int num_right = 0;
int num_error = 0;
ifstream reader("infile.txt");
for(int i = 0; i < 16; ++i)
{
getline(reader, question); // get line from infile.txt
cout << question << endl; // print out line from file
for (int j = 0; j < 4; ++j)
{
getline(reader, answer);
cout << answer << endl;
}
cout << "Enter Answer: ";
cin >> user_answer; // have the user input answer
if (user_answer == correct_answers[i]) // if statement to determine correct answer
{
cout << "correct" << endl;
num_right++; // adds 1 to num_right
cout << endl;
}
else // if user_answer is not equal answer/wrong answer
{
cout << "incorrect" << endl;
num_error++; // add 1 to num_error
if (is_hung(num_error)) // get function
break;
}
}
reader.close();
cout << endl;
if (num_error == 7)
{
cout << "You got " << num_right << " out of 16. You lose." << endl;
}
else
{
cout << "You got " << num_right << " out of 16. You Win." << endl;
}
return 0;
}
bool is_hung(int num_errors)
{
cout << "\t \t \t" << " O ";
if (num_errors >= 2)
{
cout << endl;
cout << "\t \t \t" << "/";
}
if (num_errors >= 3)
{
cout << (num_errors >= 4 ? ‘|’ : ‘ ‘) << “\\”;
}
if (num_errors >= 5)
{
cout << endl;
cout << "\t \t \t" << " | ";
}
if (num_errors >= 6)
{
cout << endl;
cout << "\t \t \t" << "/";
}
if (num_errors >= 7)
{
cout << " \\" << endl;
cout << " YOU ARE HUNG";
}
cout << endl;
return (num_errors < 7);
}
Don’t you think that is much easier to work with?
#include <iostream>
using namespace std;
int main() {
char gun;
char att;
cout << "Guns" << endl;
cout << "[1] AK" << endl;
cout << "[2] MP5" << endl;
cout << "[3] M2" << endl;
cout << "[4] SAR" << endl;
cout << "[5] Tommy" << endl;
cout << "[6] Custom" << endl;
cin >> gun;
cout << "[A] Holosight" << endl;
cout << "[B] Simplesight" << endl;
cout << "[C] Silencer" << endl;
cout << "[D] Muzzel Boost" << endl;
cout << "[E] Muzzel Break" << endl;
cout << "[F] 8x Zoom Scope" << endl;
cout << "[G] 16x Zoom Scope" << endl;
cout << "[H] Lasersight" << endl;
cin >> att;
switch (gun) {
case'1':
if (gun = 1)
cout << 'test' << endl;
else
cout << "Wrong Command" << endl;
break;
}
}
When I run this and select 1 (code is only half-built) it prints "1952805748". I have tried adding more cases and it still has this problem.
Can someone please help me with this problem.
You have several issues here:
if (gun = 1)
cout << 'test' << endl;
that's not a comparison; gun is always assigned the value 1, and the if condition is always true. The comparison operator is ==.
Also, you are printing out 'test', which is why you are getting the weird number. You need to print "test".
Also note that gun is a char, so you might want to compare against the char '1'.
Also, avoid using namespace std;
Good day everyone. My program is about a Computer Shop (Laptop). So, people can buy laptop through this program and "PCPurchase" is the function that gonna handle the customer transaction.
I use file-pointer and struct to save information about all the laptops. And of course, the struct store more than 1 type of laptops because obviously this is a laptop shop.
I encounter a problem where user can only buy (enter name) of the first entry (first laptop) in the struct. Here: cout << "Enter the laptop company and name you want to buy: " << endl;
If customer enter the name of 2nd laptop,3rd and so on, it will jump to line
cout << endl << "\tNot available!" << endl; cout << "\tPress A to try again or B to return to main menu" << endl;
It indicates that the laptop's name is not in database which actually is.
Can I know what the problem here actually is?
int PCPurchase()
{
struct customer cust;
system("color 0A");
char laptop[100];
double total_bill;
const double TAX=0.06;
system("cls");
cout << setfill ('-') << setw (55) << "-" << endl;
cout << "\t\tCustomer Dashboard" << endl;
cout << setfill ('-') << setw (55) << "-" << endl;
fptr=fopen("laptop.txt","ab+");
cout << "Available laptops: " << endl;
rewind(fptr);
while(fread(&PC,sizeof(PC),1,fptr)==1)
{
cout << endl << "Laptop company and name: ";
cout << PC.laptopcompany << endl;
cout << "RAM: ";
cout << PC.RAM << endl;
cout << "Processor: ";
cout << PC.Processor << endl;
cout << "Price: RM";
cout << PC.price << endl;
}
cout << "\nPress any key to continue purchase" << endl;
getch();
fflush(stdin);
getInfo(cust); //get information of customer
cout << "Enter the laptop company and name you want to buy: " << endl;
cout << "(Type 'RETURN' if you do not want to purchase)" << endl << endl;
gets(laptop);
rewind(fptr);
while(fread(&PC,sizeof(PC),1,fptr)==1)
{
if(strcmpi(PC.laptopcompany,laptop)==0)
{
cout << setfill ('-') << setw (55) << "-" << endl;
cout << "\tYou have selected" << endl;
cout << setfill ('-') << setw (55) << "-" << endl;
cout << "Laptop company and name: ";
cout << PC.laptopcompany << endl;
cout << "RAM: ";
cout << PC.RAM << endl;
cout << "Processor: ";
cout << PC.Processor << endl;
cout << "Price: ";
cout << PC.price << endl;
total_bill=PC.price+(PC.price*TAX);
cout << setfill ('-') << setw (55) << "-" << endl;
cout << fixed << showpoint << setprecision (2);
cout << "Name: "<< cust.name << endl; // struct output
cout << "Email: "<< cust.email << endl;
cout << "Phone Number: " << cust.number << endl;
cout << "Your total bill (including 6% tax): RM" << total_bill << endl;
cout << setfill ('-') << setw (55) << "-" << endl;
cout << endl << "\tPress 1 to return to main screen!";
cout << endl << "\tPress 2 to quit the program!";
char afterpurchase;
afterpurchase=getche();
if (afterpurchase=='1')
{
fclose(fptr);
main();
}
else
exit_system();
}
else if(strcmpi("RETURN",laptop)==0)
main();
else
{
cout << endl << "\tNot available!" << endl;
cout << "\tPress A to try again or B to return to main menu" << endl;
char choice1;
choice1=getche();
choice1=toupper(choice1); // Transform to uppercase
switch (choice1)
{
case 'A': fclose(fptr);
PCPurchase();
break;
default : fclose(fptr);
main();
break;
}
}
}
}
It is because of else statement. If you want to buy the laptop from the second record of the data and so on, it will compare with the first record and will not return true. So, it will proceed to else statement and will not repeat the while loop. Simply change the else statement to make the loop working.
I am trying to do a hangman project, but my code isn't working. Whenever I put in the proper letter, the code tells me it is wrong (even though it is right). Not really sure why - the code worked at some point but I changed some things and now I don't know why it doesn't work. So it is probably a simple fix, but I am just not seeing it.
Any help would be very appreciated!
#include <iostream>
using namespace std;
int letterFill (char, string, string&);
int main()
{
string name;
int maxAttempts = 5;
int wrongGuesses;
char letter;
srand(time(0));
const string wordList[15] = { "hanukkah", "sparklers", "mistletoe", "menorah", "presents", "reindeer",
"kwanzaa", "snowman", "eggnog", "celebration", "yuletide", "resolution", "nutcracker", "ornaments", "gingerbread" };
string correctWord = wordList[rand() % 15];
string unknown(correctWord.length(),'*');
cout << correctWord << endl;
cout << "Welcome to a fun game of winter holiday hangman! What is your name? " << endl;
cin >> name;
cout << name <<", there are some simple things you should know about this game before you start playing!" << endl;
cout << "You will be trying to guess a randomly selected word by typing in ONE letter at a time " << endl;
cout << "You will have " << maxAttempts << " tries before losing the game " << endl;
cout << "And remember, all of the words are winter holiday related. Good luck " << name <<"!" << endl;
cout << "*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*" <<endl;
while (wrongGuesses == 0)
{
cout << "Guess a letter" << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 1)
{
cout << "You have 4 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 2)
{
cout << "You have 3 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 3)
{
cout << "You have 2 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 4)
{
cout << "You have 1 guess left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 5)
{
cout << "Sorry " << name << " you have made 5 wrong guesses!" << endl;
cout << "Game over. Click any key to exit. Play again soon :) " << endl;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
system("pause");
return 0;
}
int letterFill (char guessLetter, string mysteryWord, string& guessWord)
{
int x;
int matches=0;
int lengthWord=mysteryWord.length();
for (x = 0; x< lengthWord; x++)
{
if (guessLetter == mysteryWord[x])
return 0;
if (guessLetter == mysteryWord[x])
{
guessWord[x] = guessLetter;
matches++;
}
}
return matches;
}
You aren't updating the string guessWord in your int letterFill() function. As soon as you see a letter that matches you return without entering that second if statement.
I assume what you want is only to return after fully updating the guessWord, based on that what you want to do is iterate through the string, updating guessWord as you find matches and after your loop do a check
if(matches == 0) return 0;
else return matches;
Right now I am trying to get my scorecard to work for hangman. This is a game I can play as many times as I want and I want it to show my lowest score when I press 2 at the game menu. For some reason my logic isn't working. Can anyone help? I have attached the 3 sections of code necessary for this to work.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
using namespace std;
int maxAttempts = 10; //max attempts possible
void poorStiff(int);
int wordFill(char, string, string&);
int main()
{
int intro;
int bodyPart = 0;
ifstream wordIn; //file stream read in
wordIn.open("WordList.txt"); //list of words used in this game
//if (!wordIn.good()) { std::cerr << "open failed.\n"; exit(1); }
char letter; //letter guessed
int numWrongGuesses = 0; //counts the number of wrong guesses
string theWord;
string words[13];
int play = 0;
bool run = true;
int makeYourSelection = 0;
int bestScore = 11;
while (run == true)
{
int attemptsGame = 0;
// put a game menu to loop that asks if you want to keep playing or exit and go to scorecard
cout << " WELCOME TO THE GAME OF HANGMAN\n\n\n\n";
cout << " Press 1 to play the game, press 2 to exit to the scorecard \n\n";
cout << " You have 10 attempts to guess the words before you get hung \n\n";
cin >> play;
while (play == 1)//loops while user has entered 1, 0 exits the game
{
for (int i = 0; i < 13; i++) //labeled 13 and not words because 13 is a constant
{
wordIn >> words[i];//replaces the file words and puts them in an array and counts them out of the file
cout << words[i] << endl;
}
srand(time(NULL));//to get a random word
int n = rand() % 12;
theWord = words[n]; //pulls word from file
wordIn.close();
string mystery(theWord.length(), '*'); //replaces word letters with asterisks
while (numWrongGuesses < maxAttempts) // while the amount of guesses is less than the max wrong guesses
{
cout << mystery << endl << endl;
cout << "You now have the length of the word represented by the *'s. \n\n";
cout << "Guess a letter \n\n";
cin >> letter;
if (wordFill(letter, theWord, mystery) == 0) //fuction call
{
bodyPart++;
poorStiff(bodyPart);
cout << "You have entered a letter that isn't in the word, guess again. \n\n";
numWrongGuesses++;
attemptsGame++;
}
else
{
for (int i = 0; i < mystery.length(); i++)
{
if (theWord[i] == letter)
{
mystery[i] = letter;
}
}
cout << "You have found one of the letters. Congratulations! \n\n";
cout << "You have: " << maxAttempts - numWrongGuesses;
cout << " guesses left \n\n" << endl;
}
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break;
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
}
}
if (numWrongGuesses == maxAttempts) //when you run out of guesses
{
cout << "Too bad, you ran out of guesses and have been hung at the gallows. \n\n";
cout << "The word you were trying to guess was " << theWord << endl;
poorStiff(bodyPart);
}
cin.ignore();
cin.get();
break;
}
while (play == 2)
{
cout << "Best Scores: \n\n";
cout << bestScore << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}
int wordFill(char guess, string theWordSecret, string&guessWord) //function for determing if you guess a letter contained
{
int i;
int hits = 0; //letter hits within the word
int many = theWordSecret.length();
for (i = 0; i < many; i++)
{
if (guess == guessWord[i])
return 0;
if (guess == theWordSecret[i])
{
guessWord[i] == guess;
hits++;
}
}
return hits;
}
void poorStiff(int bodyPart)
{
if (bodyPart == 1)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 2)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 3)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 4)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "| / " << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 5)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "| / |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 6)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 7)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 8)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 9){
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "| / " << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 10)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "| / \." << endl;
cout << "_______________" << endl;
}
}
Did you try debugging? If you step through your code you will immediately find the problem, which is here:
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break; // problem here
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
// break should be here
}
You're breaking out of your loop before your checking logic. So move your break statement to after the check, because that code never gets executed.
For a start enable your compiler warnings, you'll get many from a quick look at your code.
Change this
guessWord[i] == guess;
to this
guessWord[i] = guess;
At this point
if (wordFill(letter, theWord, mystery) == 0) //fuction call
{
...
cout << "You have entered a letter that isn't in the word, guess again. \n\n";
}
else
{
...
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break;
...
}
}
Here, when you are entering the first if, then you will enter the second if too, which doesn't make sense. That happens because theWord and mystery are both empty strings!
Also notice that break should be after this part of code:
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
because as it is, this part of code will never be executed.
I suggest taking at this answer (not relevant with your logical error)
System(“pause”); - Why is it wrong?