string.replace()

Dec 13, 2008 at 1:13pm
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.

Thank you all so much for your help!

hangman.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// hangman.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctype.h>
#include "functions.h"

using namespace 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;
}


functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>

using namespace std;


/* UTILITY FUNCTIONS */

//Pause the program and wait for user input
void pause()
{
	/* Clear errors from cin stream as well as any remaining buffered items */
	cin.clear();
	cin.sync();

	/* Perform PAUSE */
	cout << endl << "PAUSED: Press enter to continue...\n";
	cin.get();
	cin.sync();
	cout << endl;
}

// Clear screen
void cls()
{
	int i;
	for(i=0; i<500; i++)
	{
		cout << endl;
	}
}

/* DRAW PARTS OF THE BODY */
// Draw the head and neck
void vHead()
{
     cout << "        _" << endl;
     cout << "       / \\" << endl;
     cout << "      |.. |" << endl;
     cout << "       \\~/" << endl;
     cout << "       ---" << endl;
     cout << "        |" << endl;
}

// Draw both legs
void vBlegs()
{
     cout << "       / \\" << endl << "      /   \\" << endl << "     /     \\" << endl << "   --       --" << endl;
}

// Draw both arms
void vBarms()
{
     cout << "  O-----|-----O" << endl;
}

// Draw area between arms and legs
void vBetweenArmsAndLegs()
{
     int i; //Control while loop
     for(i=0; i<5;i++)
     {
             cout << "        |" << endl;
     }
}


// Draw right leg
void vRleg()
{
     cout << "       /" << endl << "      / " << endl << "     /  " << endl << "   --   " << endl;
}

// Draw right arm
void vRarm()
{
     cout << "        |-----O" << endl;
}



/* DRAW THE BODY */

void vWholeBody()
{
     vHead();
     vBarms();
     vBetweenArmsAndLegs();
     vBlegs();
}

void vFirstMistake()
{
	vHead();
	vBarms();
    vBetweenArmsAndLegs();
	vRleg();
}

void vSecondMistake()
{
	vHead();
	vBarms();
    vBetweenArmsAndLegs();
}

void vThirdMistake()
{
	vHead();
	vBarms();
}

void vFourthMistake()
{
	vHead();
	vRarm();
}

void vFifthMistake()
{
	vHead();
}
Last edited on Dec 13, 2008 at 1:14pm
Dec 13, 2008 at 1:44pm
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:

string& replace ( size_t pos1, size_t n1, size_t n2, char c );.

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
Dec 13, 2008 at 2:00pm
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).
Dec 13, 2008 at 2:21pm
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 ****/
Dec 13, 2008 at 3:17pm
I got it figured out. Thanks again!
Topic archived. No new replies allowed.