My hangman game is complete. All i'm trying to do now is write the results of the end of the game to a file but the program is not doing that??

//Libraries
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <windows.h>
using namespace std;

//Constructs
//stores a letter and whether it is hidden or revealed
struct letter{
    letter(char blank):character(blank),hidden(true){}
    char character;
    bool hidden;

  char display()const{
    if(!hidden){
      return character;
    }

    return ('_');
  }
};

//No Global Constants

//Function Prototypes
bool winGame(const vector<letter>&word);//returns true if letters are guessed
bool guess(vector<letter>&word);//inputs guess from user and return t/f
void dspWord(const vector<letter>&word);//displays correctly guessed letters/word
vector<letter>getWord();//Inputs secret word from user, stores in a vector
int writeFile ();

//Begin Execution
int main(){
    //Prompt User
    cout<<"                            Welcome to Hangman!             "<<endl<<endl;
    cout<<" You are allowed a total of 6 guesses "<<endl<<endl;
    cout<<" If you think you may know the whole word. Enter a '$' symbol"<<endl<<endl;
    cout<<" Have someone input a secret word for you to guess "<<endl<<endl;

    //Declare Variables
    vector<letter>word;//stores secret word in a vector
    unsigned bdGuess(0); //keeps track of bad guesses from user
    word=getWord();//Input secret word from user

    //User is allowed 6 guesses
    while(bdGuess<6){
    dspWord(word);//display guessed and not guessed parts of word

    //increment bad guesses and output them to user
    if(!guess(word)){
      bdGuess++;
      cout<<" Number of wrong guesses: "<<bdGuess<<endl;
}
    //Different hangman figure states
    if(bdGuess==1){
        cout<<endl<<endl
           <<"   +----+  "<<endl
           <<"   |    |  "<<endl
           <<"   |       "<<endl
           <<"   |       "<<endl
           <<"   |       "<<endl
           <<"   |       "<<endl
           <<"  ============="<<endl<<endl;
    }
    if(bdGuess==2){
        cout<<endl<<endl
           <<"   +----+  "<<endl
           <<"   |    |  "<<endl
           <<"   |    O  "<<endl
           <<"   |    |  "<<endl
           <<"   |       "<<endl
           <<"   |       "<<endl
           <<"  ============="<<endl<<endl;
    }
    if(bdGuess==3){
        cout<<endl<<endl
           <<"   +----+  "<<endl
           <<"   |    |  "<<endl
           <<"   |    O  "<<endl
           <<"   |   /|  "<<endl
           <<"   |       "<<endl
           <<"   |       "<<endl
           <<"  ============="<<endl<<endl;
    }
    if(bdGuess==4){
        cout<<endl<<endl
         <<"   +----+  "<<endl
         <<"   |    |  "<<endl
         <<"   |    O  "<<endl
         <<"   |   /|\\ "<<endl
         <<"   |       "<<endl
         <<"   |       "<<endl
         <<"  ============="<<endl<<endl;
    }
    if(bdGuess==5){
        cout<<endl<<endl
           <<"   +----+  "<<endl
           <<"   |    |  "<<endl
           <<"   |    O  "<<endl
           <<"   |   /|\\ "<<endl
           <<"   |     \\ "<<endl
           <<"   |       "<<endl
           <<"  ============"<<endl<<endl;
    }
    if(bdGuess==6){
        cout<<endl<<endl
          <<"   +----+     "<<endl
          <<"   |    |     "<<endl
          <<"   |    O     "<<endl
          <<"   |   /|\\   "<<endl
          <<"   |   / \\   "<<endl
          <<"   |You Died "<<endl
          <<"  ============"<<endl<<endl;
    }
    //if all letters guessed user wins
    if(winGame(word)){
        cout<<endl<<" Congrats! You Won! The word was: "<<endl;
        dspWord(word);
        cout<<endl;
        writeFile();

        return 0;
    }
  }
     cout<<" Sorry, You Lost... "<<endl;
}

//Gets secret word from user
vector<letter>getWord(){
    //Declare Variables
    string usrWord;
    //char ch;
    vector<letter>vecWord;

    //Prompt User
    cout<<"Enter secret word: ";
    cin>>usrWord;

    //secret word is converted and returned as a vector
    for(unsigned i=0;i<usrWord.length();i++){
    vecWord.push_back(letter(toupper(usrWord[i])));
    system("CLS");
}
    return vecWord;
}

//displays word to user (The parts of it that are hidden and also not hidden)
void dspWord(const vector<letter>& word){
    for(unsigned i=0;i<word.size();i++){
        cout<<word[i].display()<<" ";
    }
}

//Inputs guess from user, return true or false
bool guess(vector<letter>&word){
    //Declare Variables
    char guess;//character being guessed
    bool correct(false);//keeps track of whether a guess is good or not

    //Prompt User
    cout<<" Guess a letter ";
    cin>>guess;
    guess=toupper(guess);

    //Full word guessing with $ sign
    if(guess=='$'){
        string input;
        cout<<" Guess the complete word "<<endl;
        cin>>input;

    //compare guess to word, return false if incorrect guess
    for(unsigned i=0;i<input.length();i++){
        if(toupper(input[i])!=word[i].character){
            cout<<" Sorry! That is not a correct guess... "<<endl;

            return false;
      }
    }

    //if guess is correct, reveal letter and return true
    for(unsigned i=0;i<word.size();i++){
        word[i].hidden=false;
    }

    return true;
  }

    //if a correct letter is guessed, it is unhidden
    for(size_t i=0;i<word.size();i++){
        if(word[i].character==guess){
            correct=true;
            word[i].hidden=false;
    }
  }
  //return whether guess correct
  return correct;
}

//If whole word is guessed, returns true
bool winGame(const vector<letter>&word){
    for(size_t i=0;i<word.size();i++){
        if(word[i].hidden){

            return false;
        }
  }
    return true;
}

int writeFile ()
{
  ofstream myfile;
  myfile.open ("results.txt");
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}

Edited 2 Years Ago by Is_at_work

Your writefile function is fine. What makes you think it isn't working? It will create the file "results.txt" in the working directory. Have you looked in the working directory? This will (if you're using an IDE) probably not be the same directory that the source code is in.

If you want to be sure, try this (assuming you're on win):

myfile.open ("C:/complete/path/to/results.txt");

Edited 2 Years Ago by Moschops

It works for me. Make sure you check the results.txt in the folder where your project is saved.

EDIT: What IDE are you using?

Edited 2 Years Ago by kal_crazy

This article has been dead for over six months. Start a new discussion instead.