C++ error C2181: illegal else without matching if - c++

I'm new to C++. I have been following an online course that teaches you how to make a Hangman game in C++. For the most part the code was working, every time I debugged it ran fine. I completed the entire code and double checked it, but I'm getting an error on one of the 'else' the errors I'm getting is "error C2181: illegal else without matching if.
Here's my code.
// Hangman game
// CN
// Header file
#include "stdafx.h"
// Input/Output
#include <iostream>
// Text
#include <string>
// Information storage
#include <vector>
// Figuring out the information storage
#include <algorithm>
// Converts lower cased letters to higher case letters
#include <cctype>
// Reads computers time and distributes a word from a list accordingly
#include <ctime>
// Library for system
#include <cstdlib>
using namespace std;
int main()
{
// Constant data type which is a fixed value, MaxWrong in Pascal Text and the max number of guesses.
const int MaxWrong = 6;
vector<string> words;
words.push_back("SANTA CLAUSE");
words.push_back("REINDEER");
words.push_back("PRESENT");
words.push_back("TREE");
words.push_back("MILK");
words.push_back("COOKIE");
// Parenthesis must always match
srand(static_cast<unsigned int>(time(0)));
random_shuffle(words.begin(), words.end());
const string theWord = words[0];
int wrong = 0;
string soFar(theWord.size(), '_');
string used = "";
cout << "\n******** Welcome to Chaye's Hangman ********\n\n";
cout << " __ \n";
cout << " | | \n";
cout << " | \n";
cout << " | \n";
cout << " | \n";
cout << " | \n";
cout << " ___|____ \n\n";
cout << " Do Your Best Human \n\n";
// Loop
while ((wrong < MaxWrong) && (soFar != theWord))
{
cout << "\n You have " << (MaxWrong - wrong);
cout << " incorrect tries left.\n";
cout << "\n You've used these letters:\n" << used << endl;
char guess;
cout << "\n\n Enter your guess: ";
cin >> guess;
guess = toupper(guess);
while (used.find(guess) != string::npos)
{
cout << "\n You've already tried " << guess << endl;
cout << " Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}
used += guess;
if (theWord.find(guess) != string::npos)
{
cout << " That's right " << guess << " is in the word,\n";
for (int i = 0; i < theWord.length(); ++i)
{
if (theWord[i] == guess)
{
soFar[i] = guess;
}
}
}
else
{
cout << " sorry. " << guess << " isn't in the word.\n";
++wrong;
}
// If one is wrong
if (wrong == 1)
{
cout << " __ \n";
cout << " | | \n";
cout << " | O \n";
cout << " | \n";
cout << " | \n";
cout << " | \n";
cout << " ___|____ \n\n";
cout << " Do Your Best Human \n\n";
}
else
{
cout << endl;
}
// If one is wrong
if (wrong == 2)
{
cout << " ___ \n";
cout << " | | \n";
cout << " | O \n";
cout << " | # \n";
cout << " | \n";
cout << " | \n";
cout << " ___|____ \n\n";
cout << " Do Your Best Human \n\n";
}
else
{
cout << endl;
}
// If one is wrong
if (wrong == 3)
{
cout << " ___ \n";
cout << " | | \n";
cout << " | O \n";
cout << " | /# \n";
cout << " | \n";
cout << " | \n";
cout << " ___|____ \n\n";
cout << " Do Your Best Human \n\n";
}
else
{
cout << endl;
}
// If one is wrong
if (wrong == 4)
{
cout << " ___ \n";
cout << " | | \n";
cout << " | O \n";
cout << " | /#\ \n";
cout << " | \n";
cout << " | \n";
cout << " ___|____ \n\n";
cout << " Do Your Best Human \n\n";
}
else
{
cout << endl;
}
// If one is wrong
if (wrong == 5)
{
cout << "\n You've been hung \n";
cout << " ___ \n";
cout << " | | \n";
cout << " | O \n";
cout << " | /#\ \n";
cout << " | / \n";
cout << " | \n";
cout << " ___|____ \n\n";
cout << " Do Your Best Human \n\n";
}
else
{
cout << "\n I'm suprised you've got this far\n";
}
cout << "\n The word was " << theWord << endl;
}
// If one is wrong
if (wrong == MaxWrong)
{
cout << "\n You've been hung \n";
cout << " ___ \n";
cout << " | | \n";
cout << " | O \n";
cout << " | /#\ \n";
cout << " | / \ \n";
cout << " | \n";
cout << " ___|____ \n\n";
cout << " Do Your Best Human \n\n";
}
else
{
cout << "\n I'm suprised you've got this far\n";
}
cout << "\n The word was " << theWord << endl;
// Pause Console
system("pause");
// Kills the application once done
return 0;
}

There should be no semicolon in this line:
if (theWord.find(guess) != string::npos);
this semicolon terminates the whole if statement and the following {...} block is executed unconditionally. Following else is therefore no longer matched with the if statement.
And you are also missing closing ) bracket in your while statement:
while (used.find(guess) != string::npos
{
...
}

Change
** else **
to
else
Your compiler is interpreting that as an operator. If it still fails, edit your post w/ the error. Also worth noting, if you compile w/ the -g flag you get some debug output which can be beneficial.

Related

how to tell if a character is inside a string c++

I am making a hangman game on my own and I have ran into a problem where I need to know if the guess is in the secret word. if it is I will change the bool innum to true, if it is not it will stay false. i have looked it up and cannot find anything that works. also the name printe is just the name of the string its just what I've named it.
here is the code I am working with:
using namespace std;
#include <iostream>
#include <conio.h>
void title();
void rightanswer();
void try1();
void try2();
void try3();
void try4();
void try5();
void try6();
void spacer();
int main()
{
bool innum = false;
int printe = 0, attempts_wrong = 0, trynum = 6;
char guess;
string secretword, hint1, hint2;
title();
// the welcoming senteces
cout << "Welcome to HANG MAN\n\n" << endl;
cout << "Please enter the secret word (no spaces): ";
cin >> secretword;
// the hints
cout << "\nenter the first hint: ";
cin >> hint1;
cout << "\nenter the second hint: ";
cin >> hint2;
//explanation for hints
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; //so guesser cant see word
cout << "\nthe hints will be used as the guesser runs out of attemptts" << endl;
cout << "the first hint will be used immediately\n\n" << endl;
cout << "press any button to start...";
_getch();
cout << "\n\n" << endl;
for (int i = 0; secretword[i] != '\0'; ++i)
{
printe++;
}
rightanswer();
cout << "\nyour word is " << printe << " letters long" << endl;
if (attempts_wrong == 0)
{
cout << "your first hint is: ";
cout << hint1 << endl;
}
if (attempts_wrong == 3)
{
cout << "your second hint is: ";
cout << hint2 << endl;
}
cout << "enter a letter: ";
cin >> guess;
// im gonna have the code go here
// <-----------------------------
if (innum == true)
{
spacer();
cout << guess << " is in the secret word" << endl;
rightanswer();
}
else if (innum == false)
{
spacer();
cout << guess << " is not in the secret word" << endl;
rightanswer();
attempts_wrong++;
}
return 0;
}
void title() {
cout << "*****************************************" << endl;
cout << "* _____ *" << endl;
cout << "* | | /\\ |\\ | | *" << endl;
cout << "* |_____| /__\\ | \\ | | ___ *" << endl;
cout << "* | | / \\ | \\ | | | *" << endl;
cout << "* | | / \\| \\| |_____| *" << endl;
cout << "* *" << endl;
cout << "* |\\ /| /\\ |\\ | *" << endl;
cout << "* | \\ / | /__\\ | \\ | *" << endl;
cout << "* | \\ / | / \\ | \\ | *" << endl;
cout << "* | V | / \\| \\| *" << endl;
cout << "* *" << endl;
cout << "*****************************************" << endl;
}
//head, body, 2 arms, 2 legs - 6 in total
void rightanswer() {
//if the guess is right and the start
cout << " ___ " << endl;
cout << " | | " << endl;
cout << " | " << endl;
cout << " __|__ " << endl;
}
void try1() {
cout << " ___ " << endl;
cout << " | | " << endl;
cout << " O | " << endl;
cout << " | " << endl;
cout << " __|__ " << endl;
}
void try2() {
cout << " ___ " << endl;
cout << " | | " << endl;
cout << " O | " << endl;
cout << " | | " << endl;
cout << " __|__ " << endl;
}
void try3() {
cout << " ___ " << endl;
cout << " | | " << endl;
cout << " O | " << endl;
cout << " /| | " << endl;
cout << " __|__ " << endl;
}
void try4() {
cout << " ___ " << endl;
cout << " | | " << endl;
cout << " O | " << endl;
cout << " /|\ | " << endl;
cout << " __|__ " << endl;
}
void try5() {
cout << " ___ " << endl;
cout << " | | " << endl;
cout << " O | " << endl;
cout << " /|\ | " << endl;
cout << " / __|__ " << endl;
}
void try6() {
cout << " ___ " << endl;
cout << " | | " << endl;
cout << " O | " << endl;
cout << " /|\ | " << endl;
cout << " / \__|__ " << endl;
cout << " YOU LOSE" << endl;
cout << "you have run out of guesses";
exit(0);
}
// it creates a line to differ one try from the other
void spacer() {
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
I don't know how much to show so that is all of it, its probably not the best but it works. I know that I don't some of the voids functions at the bottom but it works for now. if you have any suggestions tell me them. also don't be to harsh, I'm a beginner.
There are a few ways of approaching this. If you actually want to simply find if a string contains a character, the following works:
char needle;
haystack.find(needle) != string::npos;
As others have correctly answered. However, in this case, I think you are looking for something slightly different. Its a game of hangman so what you actually may want to do is show the portions of the word that the individual has actually guessed correctly.
Efficient
If we assume that: display is a string initialised with _ (or some character that is not a valid letter in a guessable word) as each character to the length of the string, you can work with this to partially display it.
If we have a map<char, vector<size_t>> that tells us exactly the indices that each character occurs at, which is easy to compute, we can take the guess and iterate through the corresponding vector of indices and replace characters in display with the actual character and then remove the character from the map. Once the map is empty, then the entire string has been guessed. You may also want to maintain a set of characters that have already been guessed to ensure that the same character can only be guessed once.
I will leave it as an exercise for you to implement this as it shouldn't be too difficult. If you'd like some pointers, let me know.
Less Efficient
This second way is less efficient, but since its a real-time game it won't make a difference unless these strings are really long.
Simply hold a copy of the string and iterate through it. Every time check which character you are checking against then replace the character at the index you are at in the _ word with the correct character. You can also keep a flag which checks whether or not any replacements happen and if not, you know that the character does not exist in the word.
You may use std::string::find method to find the given character (returns the position of the character or std::string::npos if it is not found): http://www.cplusplus.com/reference/string/string/find/
Example:
std::string str = "Hello.World";
auto found = str.find('.');
if (found != std::string::npos)
std::cout << "Period found at: " << found << std::endl;
In your case, it could look as such:
innum = (secretword.find(guess) != std::string::npos);
Try string.find.
auto i =secretword.find(guess); gives you the index. If it equals std::string::npos it isn't there.
Also, std::string has a size() method you can rrplace your letter counting with.

C++ Program need help to debug

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct football_game
{
string visit_team;
int home_score;
int visit_score;
};
void printMenu();
int main()
{
int i, totalValues = 0;
ifstream inputFile;
string temp = "";
inputFile.open("games.txt");
if (!inputFile)
{
cout << "Error opening Input file!" << endl;
exit(101);
}
inputFile >> totalValues;
getline(inputFile, temp);
cout << " *** Football Game Scores *** " << endl << endl;
cout << " * Total Number of teams : " << totalValues << endl << endl;
football_game* records = new football_game[totalValues];
// while (!inputFile.eof())
// {// == NULL) {
for (i = 0; i < totalValues; i++)
{
getline(inputFile, records[i].visit_team);
cout << records[i].visit_team << endl;
inputFile >> records[i].home_score >> records[i].visit_score;
cout << records[i].home_score << " " << records[i].visit_score << endl;
getline(inputFile, temp);
}
//}
cout << endl;
int choice = 0;
int avg_home_Score = 0;
int avg_visit_Score = 0;
printMenu(); // prints menu
cout << "Please Enter a choice from the Menu : ";
cin >> choice;
cout << endl << endl;
while (true)
{
switch (choice)
{
case 1:
cout << " Score Table " << endl;
cout << " ***********************" << endl << endl;
cout << " VISIT_TEAM"
<< " "
<< " HIGH_SCORE"
<< " "
<< "VISIT_SCORE " << endl;
cout << " -----------"
<< " "
<< "-----------"
<< " "
<< "------------" << endl;
for (int i = 0; i < totalValues; i++)
{
cout << '|' << setw(18) << left << records[i].visit_team << " " << '|'
<< setw(7) << right << records[i].home_score << " " << '|' << setw(7)
<< right << records[i].visit_score << " " << '|' << endl;
}
cout << endl << endl << endl;
break;
case 2:
{
string team_name;
cout << "Enter the Team Name : ";
cin >> team_name;
for (int i = 0; i < totalValues; i++)
{
if (records[i].visit_team == team_name)
{
cout << " VISIT_TEAM"
<< " "
<< " HIGH_SCORE"
<< " "
<< "VISIT_SCORE " << endl;
cout << " -----------"
<< " "
<< "-----------"
<< " "
<< "------------" << endl;
cout << '|' << setw(18) << left << records[i].visit_team << " " << '|'
<< setw(7) << right << records[i].home_score << " " << '|'
<< setw(7) << right << records[i].visit_score << " " << '|'
<< endl;
}
}
cout << endl;
break;
}
case 3:
{
for (int i = 0; i < totalValues; i++)
avg_home_Score += records[i].home_score;
cout << "Average home_score: " << (avg_home_Score / totalValues) << endl << endl;
break;
}
case 4:
{
for (int i = 0; i < totalValues; i++)
avg_visit_Score += records[i].visit_score;
cout << "Average visit_score: " << (avg_visit_Score / totalValues) << endl << endl;
break;
}
default:
{
cout << "Please enter valid input !!" << endl;
break;
}
}
printMenu();
cin >> choice;
}
return 0;
}
void printMenu()
{
cout << " Menu Options " << endl;
cout << " ================ " << endl;
cout << " 1. Print Information of all Games[Table Form] " << endl;
cout << " 2. Print Information of a Specific Game " << endl;
cout << " 3. Print Average points scored by the Home Team during season" << endl;
cout << " 4. Print Average points scored against the Home Team" << endl << endl << endl;
}
Here is the input file i am using
games.txt
5
SD Mines
21 17
Northern State
10 3
BYU
10 21
Creighton
14 7
Sam Houston State
14 24
When i am using the 2nd option (Print Information of a Specific Game) from the output screen,
it ask me to enter the team name and when i enter the team-name.
For example: SD Mines it gives me an error, but when I enter the team-name with no space like: BYU it works fine for me.
cin >> team_name;
Takes the input only upto space.
You might want to use cin.getline() for taking space separated strings as input.
A small program demonstrating the same :
#include <iostream>
#include <string>
int main ()
{
std::string name;
std::cout << "Please, enter your full name: ";
std::getline (std::cin,name);
std::cout << "Name is : , " << name << "!\n";
return 0;
}
std::cin ignores whitespaces by default.
To include spaces in your input try :
getline(cin, team_name);
This would pick up all the characters in a line until you press enter. This is available in
#include<string>
You need to flush the std::cin buffer after reading the choice:
#include <limits>
//...
cin >> choice;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Refer to this question for detailed explanation.
Also, if you want to read strings with spaces from the standard input, replace this:
cin >> team_name;
with this:
getline(cin, team_name);
as already mentioned in other answers. No need to flush std::cin this time, since you have already read the full line.
Finally, remove extra newlines from your games.txt:
5
SD Mines
21 17
Northern State
...

The first character in the output stream is replaced with the last character of that stream which is removed. Why is that so?

Program's Aim:
The program is a hangman game which get a list of planets from our solar system, saves it to an array then randomly selects one word from the array and subsequently prints two letters of the word on the board. The program runs perfectly in a windows environment, but fails in runtime on linux.
Problem:
The program replaces the first character in the output stream with the last character of that stream. That happens when printing the word on the board and also when the word is displayed when it is correctly or wrongly guessed.
e.g.:
word = Mars
For 6 wrong guess it's supposed to print, Too bad you didn't guess right. It was "mars".
Instead, it prints: "oo bad you didn't guess right. It was "mars
Here are the files:
wordlist.yx:
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Pluto
Hangman.h:
#pragma once
#include <iostream>
using namespace std;
// Check for OS
#ifdef _WIN32
#define SYS "MS"
#else
#define SYS "LINUX"
#endif
class Hangman {
private:
int version = 3;
string release_date = "01/05/2016";
int &v = version;
string &r = release_date;
static const int WORDLIST_SIZE = 10;
string wordlist[WORDLIST_SIZE];
string word, hidden_word, wordfile = {"wordlist.yx"};
int word_length, tries_left;
bool is_match;
public:
Hangman();
~Hangman();
//attributes
string user_input, arch;
char choice;
// Game functionality methods
void printMenu(Hangman &);
void info(Hangman &) const;
void startGame(Hangman &);
void reset();
void getWordlist();
void getWordAndSetHidden();
int getTries();
void printHangman(int);
void printBoard();
void getInput(Hangman &);
int validateInput(char);
void decideFate(Hangman &);
void decision(Hangman &);
// Invalid input and screen clearing methods
void invalidInput1(Hangman &, char &);
void invalidInput2(Hangman &, string &);
void cls() const;
};
Hangman.cpp:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <limits>
#include <algorithm>
#include <string>
#include "Hangman.h"
using namespace std;
//Constructor
Hangman::Hangman(){
//Get's OS
// Microsoft
if(SYS == "MS"){
arch = "cls";
} else{
//linux, unix, OSX
arch = "clear";
}
hidden_word = "";
tries_left = 6;
}
// De-constructor
Hangman::~Hangman(){
//Destroy objects
}
// Menu Options
void Hangman::printMenu(Hangman &s){
cls();
cout << " _____Hangman (v7)_____ " << endl;
cout << "| |" << endl;
cout << "| Play - P |" << endl;
cout << "| Info - I |" << endl;
cout << "| Quit - Q |" << endl;
cout << "------------------------" << endl;
cout << "Choice: ";
getline(cin, user_input);
// Pass first character of string to char
choice = user_input[0];
switch(choice){
case 'p':
case 'P':
startGame(s);
break;
case 'i':
case 'I':
info(s);
break;
case 'q':
case 'Q':
cls();
cout << "Thank you for playing..." << endl;
exit(EXIT_SUCCESS);
break;
default:
invalidInput1(s, choice);
}
}
void Hangman::info(Hangman &m) const{
string ic;
cls();
cout << "+--------------------------------------------------------------------+\n"
<< "| Hangman Instructions |\n"
<< "+--------------------------------------------------------------------+\n"
<< "| This Hangman game is played by entering a guessed letter at a time.|\n"
<< "| 6 wrong guesses are given by default and decrements by 1 (one) on |\n"
<< "| each wrong guess. |\n"
<< "+--------------------------------------------------------------------+\n"
<< "| In-game options |\n"
<< "+--------------------------------------------------------------------+\n"
<< "| restart - Start a new game |\n"
<< "| menu - Display main menu |\n"
<< "| quit - Exit game |\n"
<< "+--------------------------------------------------------------------+\n"
<< "| Game Information |\n"
<< "+--------------------------------------------------------------------+\n"
<< "| Version: " << v << " |\n"
<< "| Date: " <<r<< " |\n"
<< "+--------------------------------------------------------------------+\n"
<< "| Author |\n"
<< "+--------------------------------------------------------------------+\n"
<< "| Philan James (Zoutiyx) |\n"
<< "| zoutiyx#gmail.com |\n"
<< "+--------------------------------------------------------------------+\n\n";
cout << "Press enter to close... ";
getline(cin, ic);
m.printMenu(m);
}
void Hangman::startGame(Hangman &h){
cls();
h.reset();
h.getWordlist();
h.getWordAndSetHidden();
h.decideFate(h);
do {
h.printHangman(h.getTries());
h.printBoard();
h.getInput(h);
h.decideFate(h);
} while(h.getTries() != 0);
}
//Gets the wordlist from a file and stores it into an array
void Hangman::getWordlist(){
fstream in_words(wordfile, ios::in);
if(!in_words.is_open()){
cerr << "Error 0: " << wordfile << " isn't found in current directory!\n";
} else{
for(int i = 0; (i < WORDLIST_SIZE - 1) && !in_words.eof(); i++){
getline(in_words, *(wordlist + i), '\n');
}
}
in_words.close();
}
void Hangman::getWordAndSetHidden(){
srand(time(NULL));
int word_index, p1, p2;
word_index = rand() % (WORDLIST_SIZE - 1) + 0;
word = wordlist[word_index];
// Sets the hidden word letters to underscores
for(unsigned int i = 0; i < word.length(); i++){
if(word[i] == ' ') {
hidden_word.push_back(' ');
} else {
hidden_word.push_back('_');
}
}
//selecting the random two indexes of the word
p1 = rand() % word.length();
p2 = rand() % word.length();
while (p1 == p2 || p2 == p1){
p1 = rand() % word.length();
p2 = rand() % word.length();
}
// Assigning the letter to the corresponding index
for(int i = 0; i < (int)word.length(); i++){
if (i == p1){
hidden_word[i] = word[i];
} else if( i == p2){
hidden_word[i] = word[i];
}
// If a certain letter is the same as the on at the p1 or p2,
// It is also printed on the board, making it more than (2) default letters
if (tolower(word[p1]) == tolower(word[i])){
hidden_word[i] = word[i];
}
if (tolower(word[p2]) == tolower(word[i])){
hidden_word[i] = word[i];
}
}
}
// Get lives remaining
int Hangman::getTries(){
return tries_left;
}
// Print's hangman state based on lives left
void Hangman::printHangman(int ll){
cls();
cout << "Planets in our solor system...\n\n";
cout << "Tries left: " << tries_left << endl;
switch (ll){
case 6:
cout << " |----| " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << "____|____ " << endl;
cout << endl;
break;
case 5:
cout << " |----| " << endl;
cout << " | 0 " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << "____|____ " << endl;
cout << endl;
break;
case 4:
cout << " |----| " << endl;
cout << " | 0 " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | " << endl;
cout << "____|____ " << endl;
cout << endl;
break;
case 3:
cout << " |----| " << endl;
cout << " | 0 " << endl;
cout << " | /| " << endl;
cout << " | | " << endl;
cout << " | " << endl;
cout << "____|____ " << endl;
cout << endl;
break;
case 2:
cout << " |----| " << endl;
cout << " | 0 " << endl;
cout << " | /|\\ " << endl;
cout << " | | " << endl;
cout << " | " << endl;
cout << "____|____ " << endl;
cout << endl;
break;
case 1:
cout << " |----| " << endl;
cout << " | 0 " << endl;
cout << " | /|\\ " << endl;
cout << " | | " << endl;
cout << " | / " << endl;
cout << "____|____ " << endl;
cout << endl;
break;
case 0:
cout << " |----| " << endl;
cout << " | 0 " << endl;
cout << " | /|\\ " << endl;
cout << " | | " << endl;
cout << " | / \\ " << endl;
cout << "____|____ " << endl;
cout << endl;
break;
}
}
// Display hidden word
void Hangman::printBoard(){
for(unsigned int i = 0; i < hidden_word.length(); i++){
if(word[i] == ' '){
cout << word[i];
} else if(i != hidden_word.length()){
cout << hidden_word[i] << " ";
} else{
cout << hidden_word[i];
}
}
cout << endl << endl;
}
// Get letter choice and assign position 0 to character
void Hangman::getInput(Hangman &c){
cout << "Letter choice: ";
getline(cin, user_input);
transform(user_input.begin(), user_input.end(),
user_input.begin(), ::tolower);
if(user_input == "restart"){
c.reset();
c.startGame(c);
} else if(user_input == "menu"){
printMenu(c);
} else if(user_input == "quit"){
cout << "\nThank you for playing...\n";
exit(EXIT_SUCCESS);
} else{
choice = user_input[0];
user_input = "";
validateInput(choice);
}
}
// test character for validity
int Hangman::validateInput(char choice){
for(unsigned int i = 0; i < hidden_word.length(); i++){
//Checks if entered character is already on the board
if(toupper(choice) == hidden_word[i] || tolower(choice) == hidden_word[i]){
--tries_left;
return 0;
}
// Checks if character is a valid input
if(toupper(choice) == word[i] || tolower(choice) == word[i]){
hidden_word[i] = word[i];
is_match = true;
}
// If end of loop and character is invalid, lives left -=1
if(i == hidden_word.length() -1 && is_match != true){
--tries_left;
return 0;
}
}
is_match = false;
return 0;
}
// Checks if the word was found or not
void Hangman::decideFate(Hangman &f){
if(hidden_word == word){
cls();
cout << "Congrats, \"" << word << "\" it is.\n\n";
f.decision(f);
}
if(tries_left == 0 && hidden_word != word){
cin.ignore();
f.printHangman(f.getTries());
cout << "Too bad you didn't guess right. It was \""
<< word << "\"" << endl << endl;
f.decision(f);
}
}
void Hangman::decision(Hangman &c) {
cout << "+----------------------+" << endl;
cout << "| Alternatives |" << endl;
cout << "+----------------------+" << endl;
cout << "\"restart\" or a new game\n"
<< "\"help\" for help information\n"
<< "\"quit\" to exit\n\nChoice: ";
getline(cin, user_input);
transform(user_input.begin(), user_input.end(),
user_input.begin(), ::tolower);
if(user_input == "restart"){
c.startGame(c);
} else if(user_input == "help"){
info(c);
} else if(user_input == "quit"){
cls();
cout << "Thank you for playing..." << endl;
exit(EXIT_SUCCESS);
} else{
invalidInput2(c,user_input);
}
}
void Hangman::invalidInput1(Hangman &i1, char &i){
//cin.clear();
cerr << "' " << i << " ' is not a valid input."
<< " Press <Enter> to try again.";
getline(cin, user_input);
Hangman::printMenu(i1);
}
void Hangman::invalidInput2(Hangman &i2, string &i){
//cin.clear();
cerr << "\" " << i << " \" is not a valid input."
<< " Press <Enter> to try again.";
getline(cin, user_input);
cls();
Hangman::decision(i2);
}
// Reset game
void Hangman::reset(){
tries_left = 6;
hidden_word = "";
}
// Calls clear screen command
void Hangman::cls() const{
// passes cls or clear string as character pointer
system(arch.c_str());
}
main.cpp:
#include "Hangman.h"
int main() {
Hangman hangman;
hangman.printMenu(hangman);
}
Tried:
Running the program in a lower standard (11) from 14.
Online compiler from tutorialspoint.
Manually setting the word.
Observation:
I observed that if I manually set the word, that situation is resolved. How do I narrow in on this issue further?
Edit:
Added wordlist and removed a stray backslash which prevented compiling..
Try running dos2unix on your wordlist.yx file. If it was created on Windows, the newlines are probably \r\n, not just \n. And if that \r is interpreted literally, that will return to the beginning of the line prior to continuing to output to cout, thus overwriting your first character.
Reference to dos2unix can be found here.
To avoid using dos2unix, you could strip the \r characters from your wordlist array. One approach would be to loop through each word in the array and processing them like so:
currWord.erase( std::remove(currWord.begin(), currWord.end(), '\r'), currWord.end() );

While loop and boolean function

Hello guys I'm not an expert on the subject so please excuse my pour skills. I finished my program and it works fine (calculator). The problem is that now I don't know where to locate the while loop in conjunct with the Boolean function to repeat the process once it is done with a task (once the program completes a math operation). Any help, comment or suggestion will be greatly appreciated. Thank you.!!
#include <iostream>
#include <math.h>
#include <cmath>
int main()
{
double a=0.0;
double b=0.0;
double c=0.0;
bool repeat = true;
do {
using namespace std;
int x;
cout << "**********************************" << endl;
cout << "| |" << endl;
cout << "| 0 - Quit |" << endl;
cout << "| 1 - Add |" << endl;
cout << "| 2 - Subtract |" << endl;
cout << "| 3 - Divide |" << endl;
cout << "| 4 - Multiply |" << endl;
cout << "| 5 - Raise X to the power Y |" << endl;
cout << "| 6 - Sine ( x ) |" << endl;
cout << "| 7 - Cosine ( x ) |" << endl;
cout << "| 8 - Tangent ( x ) |" << endl;
cout << "**********************************" << endl;
cout << "Enter a selection, please: " << endl;
cin >> x;
switch (x)
{
{
case 1:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter second value " << endl;
cin >> b;
c=a+b;
cout << "The addition of " << a << " and "<< b << "is" << c << endl;
break;
bool repeat = true;
}
{
case 2:
cout << " Enter the first value" << endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a-b;
cout << "The subtraction of " << a << " and " << b << " is: " << c << endl;
break;
bool repeat = true;
}
{
case 3:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a/b;
cout << " The division os " << a << " and " << b << "is" << c << endl;
break;
bool repeat = true;
}
{
case 4:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a*b;
cout << " The product of " << a << " times " << b << " is " << c << endl;
break;
bool repeat = true;
}
{
case 5:
cout << " Enter the value to be exponentiated " <<endl;
cin >> a ;
cout << " Enter the exponent" << endl;
cin >> b;
c= pow(a,b);
cout << a << " Rased to the power of " << b << " is: " << c << endl;
break;
bool repeat = true;
}
{
case 6:
cout << " Enter the value that you want the sine to be taken of" <<endl;
cin >> a ;
c=sin(a);
cout << " The sine of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 7:
cout << " Enter the value that you want the cosine to be taken of" <<endl;
cin >> a ;
c=cos(a);
cout << " The cosine of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 8:
cout << " Enter the value that you want the tangent to be taken of" <<endl;
cin >> a ;
c=tan(a);
cout << " The tangent of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 0:
cout << "Ending the program" << endl;
return 0;}
break;
bool repeat = true;
}
} while (repeat = true );
return 0;
}
So here is few moments.
Call
using namespace std;
just believe - is bad idea;
In conditions like if() or while() use operator == instead of =. Because "=" - is assigne operator, and return value depended on success of operation. And "==" is compare operator.
Ow and figure one more missunderstanding. Using bool rezult = true; is wrong. You should use rezult = true; Because every time when you write type specifer you create local variable in context of case, and this don`t affect rezult declared in main
My opinion for your question is little change:
from:
do{
int x;
...
case 0:
cout << "Ending the program" << endl;
return 0;}
break;
bool repeat = true;
}
} while (repeat = true );
to
do{
int x;
...
case 0:
cout << "Ending the program" << endl;
repeat = false;}
break;
}
} while (repeat == true);
and if you need a bit more calculations you can wrapped it to new cicle something like:
while(new_condtion == true) {
do {
...
} while(repeat == true);
//change new_condtion
}
Don't redefine repeat within switch case. This creates a different variable named repeat which, although it has the same name, is not the variable named repeat defined before the loop. This is what you get when you copy a definition of the form bool repeat = true; into multiple places.
The continuation condition for the loop (repeat = true) will also loop forever. Comparison is two = signs, not one.

Scorecard Hangman. C++

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?