Hangman program

I am trying to make a hangman program to practice i made the following and it runs without errors but it doesnt what its supposed to do i get some weird text instead :
Untitled(537) malloc: *** error for object 0x100006280: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
sharedlibrary apply-load-rules all



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

#include <iostream>
using namespace std;

int lives(int x,int y)
{

	if (y=1) {
	
	x=x-1;
	}

	return x;
	

}

int main (int argc, char * const argv[]) {
    
	/* Hmm i am thinking of a new program. I could fix something... entertaining maybe. */ 
	
	/*Hangman */
	
	
	
		int i,u,y,x,o;
	
	

	x=4;
	
	
	string word; //the word we will be looking for
	cin >> word;
	word.size();
	char solution[word.size()]; // the word we are going to build
	
	for (i=0; i<=word.size(); i++) {
		solution[i]='-';
	}
	
	
	// User starts to input Chars
	
	char a;
	cin >> a;
	
	if (lives(x,y)>0) {
		
		
	for(i=0;i<=word.size();i++){
		
		
		if (word[i]==a) {
			
			solution[i]=a;}
		
			else {
				y=1;
				o=lives(x,y);
				cout << o << "lives remaining"; // function for lives
				
			}

		}
		cin>>a;
	}
	
		

	
	else{
		cout << "GAME OVER";
		}
	

	
	
	
	for (i=0; i<=word.size(); i++) {
		cout << " "<<solution[i] << endl;
		cout << " _";
	}
	
	
    return 0;
}

It looks like you have a problem with every one of your "for" loops. You should compare i < word.size(), as solution[ word.size() ] is an invalid address.

Also, in your lives function, did you mean to assign 1 to y or compare it? If you want to compare it, use ==.
fixed what you told me but still have same problem when running

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
GNU gdb 6.3.50-20050815 (Apple version gdb-1344) (Fri Jul  3 01:19:56 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
Loading program into debugger…
Program loaded.
run
[Switching to process 799]
Running…
hello
Untitled(799) malloc: *** error for object 0x100006280: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all
(gdb) h
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help" followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.
This is the GDB debugger, and it is attaching to process 799. Is that the process id of your program?
1
2
3
4
5
6
7
8
Running…
hello
Untitled(6281) malloc: *** error for object 0x100006280: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all
(gdb) 


Here is the problem i get this error above
Topic archived. No new replies allowed.