Hiding letters in guessing game

I'm working on a program to hide letters for a guessing game. The puzzle is read from a file to select a random puzzle. The puzzle can have multiple words and punctuation. I made it so that the puzzle appears on the screen, but the puzzle is supposed to show up as dashes so that you can fill in the puzzle yourself. However, only the letters should appear as dashes. The spaces and punctuation should appear normally. I don't know what I did wrong when I tried to make the puzzle into dashes, but it still appears as the word. Can anyone help? Here is the code that I wrote.

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;
string getPuzzleFromFile();
string puzzle;
string puzzle2;
string hidden;
char letter;
bool isValidLetter(char letter);
bool answer=false;

ifstream inputFile("input.txt"); //by putting this outside all of the functions, you can use this in all of the functions
//just don't create (define) a new variable with this name.

string getPuzzleFromFile(){ //get the puzzle from the file.

ifstream inputFile;
inputFile.open("input.txt");
getline (inputFile, puzzle);
inputFile.close();
return puzzle;
}



bool isValidLetter(char letter){ //Check whether input is A...Z or a...z
int x=(int) letter;
bool answer=false;


if((x>=65 && x<=90) || (x>=97 && x<=122)){
answer=true;
}

return answer;
}

string createPuzzle(string puzzle){ //create the puzzle to be guessed.
int count;
count = 0;
while (count = 0, count < puzzle.length(), count++){
isValidLetter(letter);
char letter=puzzle[count];

if(answer==true)
{
letter='-';
puzzle2==puzzle.insert(count,1,letter);
}
else(answer==false);
{
letter=letter;
puzzle2==puzzle.insert(count,1,letter);
}
}
return puzzle2;
}

void intro(){
cout<<"Welcome to the Puzzle Game. Guess letters to solve the puzzle. "<<endl;
cout<<"Here's your puzzle:"<<endl;
}
void display(string puzzle2)
{

cout<<puzzle2<<endl;
}
void body(){
getPuzzleFromFile();
createPuzzle(puzzle);
display(puzzle2);
}

int main(){

intro();


body();

return 0;

}
You're trying to solve everything with global variables which is bad.

In isValidLetter() you use a local variable answer which hides the global variable answer. That means when you call isValidLetter() the global answer is not changed and since you don't use the result of isValidLetter() answer (the global one) remains unchanged.

You see that with gobal variables things get easily confusing. Try do this without global variables. Pass them as parameter instead and use the returned variables


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.