Ok, I have been chasing my tail on this so maybe you guys can help. I think the only way for me to articulate what is happening is to post my entire code. Sorry if it's too long (is there a way to upload files?). This code compiles fine and runs. It is by far not a complete project. I am stuck on getting the string "_____" to replace characters as the user inputs letters. When it goes to do the replace it just adds tons of that letter instead of one. I am sure I am doing something wrong but have exausted all ideas at this point.
Also, if you guys could critique me that would be nice also. I want to do things right and I want to do them in a way that is easily readable. If you see things that are bad practice please let me know or if you see things I could have done better also please let me know.
// hangman.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctype.h>
#include "functions.h"
usingnamespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// Delare variables
int stringsize;
int mistakes;
string word;
string guessed;
char userInput;
bool match;
// Variable assignment
word = "test";
stringsize = word.size();
mistakes = 0;
/****************Start running program*********************/
cout << "Hangman by George Ward. v.Alpha.\n\n\n";
vWholeBody();
/*
Setting guessed to a string of underscores the length of the secret word
*/
int h = 0;
while(h < word.length())
{
guessed.append(1,'_');
h++;
}
while(mistakes < 6)
{
/****
Get user input of one character. If more than one character is input, destroy the rest
****/
cout << "Current number of mistakes:\t" << mistakes << endl;
cout << "\n\nType a letter to make a guess:\t";
cin >> userInput;
userInput = tolower(userInput);
cin.clear();//Clear errors
cin.sync(); //Destroy remaining characters in stream
/*****clear screen*********/
cls();
/*****clear screen*********/
int i = 0;
while(i < stringsize)
{
if(word.at(i) == userInput)
{
cout << "Guessed before guessed.replace():\n" << guessed << endl << endl;
pause();
guessed.replace(i, i, '_', userInput ); //This should replaced from character i to character i that is '_' with userInput
cout << "Guessed is now:\t" << guessed << endl;
pause();
match = 0;
} else {
match = 1;
}
i++;
}
if(match == 1)
{
mistakes++;
}
switch(mistakes)
{
case 0:
vWholeBody();
break;
case 1:
vFirstMistake();
break;
case 2:
vSecondMistake();
break;
case 3:
vThirdMistake();
break;
case 4:
vFourthMistake();
break;
case 5:
vFifthMistake();
break;
default:
cout << "YOU LOSE, GAME OVER!!\n\n";
break;
}
}
/*****************END OF PROGRAM****************************/
pause();
/*****************END OF PROGRAM****************************/
return 0;
}
You seem to be using the replace function incorrectly.
The string class has a lot of overloaded replace functions.
The only one that matches what you are trying to do is this one:
The parameters in order from left to right are: pos1 //The position in the string to start the replacement n1 //Number of characters in the string to be removed/replaced. n2 //The number of characters to be inserted into the string . c //The char to be used used for the insertion. This character will be repeated n2 times.
So in your code: guessed.replace(i, i, '_', userInput ); means that (let us suppose for convinience that i is 5) - starting at position 5 in the string, remove 5 characters and insert the userinput character , '_' number of times
As the underscore character has a value of 95 you are adding 95 characters!!!!!
What you probably wanted to do was: guessed.replace(i, 1, 1, userInput ); //starting at position i, replace 1 character, with the userInput characterer, repeated 1 time
Thank you so much! I knew I was making a mistake with syntax but misunderstood the particular overloaded function I was trying to use. I thought paramater 1 was where to start replacing, parameter 2 was where to stop replacing, paramater 3 was what the character you were replacing was, and paramater 4 was what character your replacing with. It seeemed strange that it would be the case but that's what I through I read. Obviously not :)
I am well on my way to having this program working... now I just need to fix the order at which my Mr. Ascii is drawn (currently reversed order).
Perhaps I could draw on your help once more? My logic statement isn't working the way I want. I do know why it isn't but my brain is having a hard time putting a logic statement together that will do what I want. What I want is to check if the letter the user input matches a letter in the secret word. If it does, then it replaces the corresponding letters in guessed with that letter. The problem is how my program is thinking mistakes are made... somehow I need to seperate that stuff.
This is the code that I currently have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**** BELOW LOGIC STATEMENT NEEDS TO BE FIXED ****/
int i = 0;
while(i < stringsize)
{
if(word.at(i) == userInput)
{
guessed.replace(i, 1, 1, userInput ); //This should replaced from character i to character i that is '_' with userInput
match = 0;
} else {
match = 1;
}
i++;
}
/**** ABOVE LOGIC STATEMENT NEEDS TO BE FIXED ****/