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
#include <iostream>
usingnamespace 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 ==.
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.
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)