Archived

This topic is now archived and is closed to further replies.

dchar3

mysterious error

Recommended Posts

hi im dchar3! you may remember me from such forums as the one from yesterday about hangman. i finished my hangman game and today i decided to try something someone told me yesterday. instead of declaring each string one at a time, i used an array. when it was done it worked perfectly fine, so i deleted the old source code and took a break. when i came back to the computer a few hours later, for absolutly no reason at all it says windows has encountered an error and needs to close. i tried to find the old source code that i deleted but it wasnt there. what the crap?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
... because you deleted it?


You might have to be more specific. Do you mean you saved the source file, made the change, then compiled, then crashed before it was saved? I think most compilers save when you compile.

Share this post


Link to post
Share on other sites
Without looking at the code, I could think to only a possibility: when you changed the code, and restarted the programm, an old memory state was used. I try to explain a bit better:
-You ran the first version. Everything went OK, and you exited from your program. But the memory was not cleaned-up.
-You changed the code, and restarted the application. This time the code isn''t correct, but, in some way, it doesn''t change the memory, and the program uses the values stored by the first version because OS assigns the same memory address to the variables.
-You put off the pc: memory is deleted.
-When the program runs again, it fails, because it try to access to memory of other processes.

I repeat: this is just an idea. Try to debug your app in order to figure out where is the problem, and while you''re executing it step by step, take a look into the memory it is changing.

Hope this helps. Good luck!
Alessandro

Share this post


Link to post
Share on other sites
quote:
Original post by cignox1
Without looking at the code, I could think to only a possibility: when you changed the code, and restarted the programm, an old memory state was used. I try to explain a bit better:
-You ran the first version. Everything went OK, and you exited from your program. But the memory was not cleaned-up.
-You changed the code, and restarted the application. This time the code isn''t correct, but, in some way, it doesn''t change the memory, and the program uses the values stored by the first version because OS assigns the same memory address to the variables.
-You put off the pc: memory is deleted.
-When the program runs again, it fails, because it try to access to memory of other processes.

I repeat: this is just an idea. Try to debug your app in order to figure out where is the problem, and while you''re executing it step by step, take a look into the memory it is changing.

Hope this helps. Good luck!
Alessandro


that makes sense, it is the first time i used arrays so i might have done it wrong. but every other time i compile something with bad code, it doesnt even compile so i know theres nothing wrong with the source code.

Share this post


Link to post
Share on other sites
heres the code, it should be right


#include <iostream.h>
#include <string>
using namespace std;

void newgame();
void input1();
void input2();
void input3();
void output1();
void output2();
void output3();
static int word, check, wrong;
static string a, hint, w[5], s[10], sb[10];
void output1()
{
if (check == 6)
{
cout << endl << "---" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl << endl;
cout << "You win!!" << endl;
cout << "Play again? " << endl;
cout << "1 - Yes" << endl;
cout << "X - No" << endl;
cin >> check;
if (check == 1)
{
newgame();
}
}
else
if (wrong == 0)
{
cout << endl << "---" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input1();
}
else
if (wrong == 1)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input1();
}
else
if (wrong == 2)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| V" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input1();
}
else
if (wrong == 3)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IV" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input1();
}
else
if (wrong == 4)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IVI" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input1();
}
else
if (wrong == 5)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IVI" << endl;
cout << "| I" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input1();
}
else
if (wrong == 6)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IVI" << endl;
cout << "| II" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl << endl;
cout << "you dead" << endl;
cout << "Play again? " << endl;
cout << "1 - Yes" << endl;
cout << "X - No" << endl;
cin >> check;
if (check == 1)
{
newgame();
}
}
}
void input1()
{
cout << endl << "Guess a letter: ";
cin >> a;
if (a == s[0])
{
sb[0] = "d";
check++;
output1();
}
else
if (a == s[1])
{
sb[1] = "i";
sb[3] = "i";
check++;
output1();
}
else
if (a == s[2])
{
sb[2] = "g";
check++;
output1();
}
else
if (a == s[4])
{
sb[4] = "t";
check++;
output1();
}
else
if (a == s[5])
{
sb[5] = "a";
check++;
output1();
}
else
if (a == s[6])
{
sb[6] = "l";
check++;
output1();
}
else
if (a == s[7])
{
sb[0] = "d";
sb[1] = "i";
sb[2] = "g";
sb[3] = "i";
sb[4] = "t";
sb[5] = "a";
sb[6] = "l";
check = 6;
output1();
}
else
if (w[0] == " ")
{
w[0] = a;
wrong++;
output1();
}
else
if (w[1] == " ")
{
w[1] = "," + a;
wrong++;
output1();
}
else
if (w[2] == " ")
{
w[2] = "," + a;
wrong++;
output1();
}
else
if (w[3] == " ")
{
w[3] = "," + a;
wrong++;
output1();
}
else
if (w[4] == " ")
{
w[4] = "," + a;
wrong++;
output1();
}
else
if (w[5] == " ")
{
w[5] = "," + a;
wrong++;
output1();
}
}
void output2()
{
if (check == 8)
{
cout << endl << "---" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << sb[7] << sb[8] << sb[9] << sb[10] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl << endl;
cout << "You win!!" << endl;
cout << "Play again? " << endl;
cout << "1 - Yes" << endl;
cout << "X - No" << endl;
cin >> check;
if (check == 1)
{
newgame();
}
}
if (wrong == 0)
{
cout << endl << "---" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << sb[7] << sb[8] << sb[9] << sb[10] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input2();
}
else
if (wrong == 1)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << sb[7] << sb[8] << sb[9] << sb[10] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input2();
}
else
if (wrong == 2)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| V" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << sb[7] << sb[8] << sb[9] << sb[10] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input2();
}
else
if (wrong == 3)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IV" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << sb[7] << sb[8] << sb[9] << sb[10] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input2();
}
else
if (wrong == 4)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IVI" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << sb[7] << sb[8] << sb[9] << sb[10] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input2();
}
else
if (wrong == 5)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IVI" << endl;
cout << "| I" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << sb[7] << sb[8] << sb[9] << sb[10] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input2();
}
else
if (wrong == 6)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IVI" << endl;
cout << "| II" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << sb[7] << sb[8] << sb[9] << sb[10] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
cout << "you dead" << endl;
cout << "Play again? " << endl;
cout << "1 - Yes" << endl;
cout << "X - No" << endl;
cin >> check;
if (check == 1)
{
newgame();
}
}
}
void input2()
{
cout << endl << "Guess a letter: ";
cin >> a;
if (a == s[0])
{
sb[0] = "p";
check++;
output2();
}
else
if (a == s[1])
{
sb[1] = "r";
sb[4] = "r";
check++;
output2();
}
else
if (a == s[2])
{
sb[2] = "o";
check++;
output2();
}
else
if (a == s[3])
{
sb[3] = "g";
sb[10] = "g";
check++;
output2();
}
else
if (a == s[5])
{
sb[5] = "a";
check++;
output2();
}
else
if (a == s[7])
{
sb[6] = "m";
sb[7] = "m";
check++;
output2();
}
else
if (a == s[8])
{
sb[8] = "i";
check++;
output2();
}
else
if (a == s[9])
{
sb[9] = "n";
check++;
output2();
}
else
if (a == s[10])
{
sb[0] = "p";
sb[1] = "r";
sb[2] = "o";
sb[3] = "g";
sb[4] = "r";
sb[5] = "a";
sb[6] = "m";
sb[7] = "m";
sb[8] = "i";
sb[9] = "n";
sb[10] = "g";
check = 8;
output2();
}
else
if (w[0] == " ")
{
w[0] = a;
wrong++;
output1();
}
else
if (w[1] == " ")
{
w[1] = "," + a;
wrong++;
output1();
}
else
if (w[2] == " ")
{
w[2] = "," + a;
wrong++;
output1();
}
else
if (w[3] == " ")
{
w[3] = "," + a;
wrong++;
output1();
}
else
if (w[4] == " ")
{
w[4] = "," + a;
wrong++;
output1();
}
else
if (w[5] == " ")
{
w[5] = "," + a;
wrong++;
output1();
}
}
void output3()
{
if (check == 5)
{
cout << endl << "---" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl << endl;
cout << "You win!!" << endl;
cout << "Play again? " << endl;
cout << "1 - Yes" << endl;
cout << "X - No" << endl;
cin >> check;
if (check == 1)
{
newgame();
}
}
if (wrong == 0)
{
cout << endl << "---" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input3();
}
else
if (wrong == 1)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input3();
}
else
if (wrong == 2)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| V" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input3();
}
else
if (wrong == 3)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IV" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input3();
}
else
if (wrong == 4)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IVI" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input3();
}
else
if (wrong == 5)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IVI" << endl;
cout << "| I" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input3();
}
else
if (wrong == 6)
{
cout << endl << "---" << endl;
cout << "| |O" << endl;
cout << "| IVI" << endl;
cout << "| II" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
cout << "you dead" << endl;
cout << "Play again? " << endl;
cout << "1 - Yes" << endl;
cout << "X - No" << endl;
cin >> check;
if (check == 1)
{
newgame();
}
}
}
void input3()
{
cout << endl << "Guess a letter: ";
cin >> a;
if (a == s[0])
{
sb[0] = "d";
check++;
output3();
}
else
if (a == s[1])
{
sb[1] = "u";
check++;
output3();
}
else
if (a == s[2])
{
sb[2] = "n";
sb[5] = "n";
check++;
output3();
}
else
if (a == s[3])
{
sb[3] = "c";
check++;
output3();
}
else
if (a == s[4])
{
sb[4] = "a";
check++;
output3();
}
else
if (a == s[5])
{
sb[0] = "d";
sb[1] = "u";
sb[2] = "n";
sb[3] = "c";
sb[4] = "a";
sb[5] = "n";
check = 5;
output3();
}
else
if (w[0] == " ")
{
w[0] = a;
wrong++;
output1();
}
else
if (w[1] == " ")
{
w[1] = "," + a;
wrong++;
output1();
}
else
if (w[2] == " ")
{
w[2] = "," + a;
wrong++;
output1();
}
else
if (w[3] == " ")
{
w[3] = "," + a;
wrong++;
output1();
}
else
if (w[4] == " ")
{
w[4] = "," + a;
wrong++;
output1();
}
else
if (w[5] == " ")
{
w[5] = "," + a;
wrong++;
output1();
}
}
void newgame()
{
w[0] = " ";
w[1] = " ";
w[2] = " ";
w[3] = " ";
w[4] = " ";
w[5] = " ";
sb[0] = "-";
sb[1] = "-";
sb[2] = "-";
sb[3] = "-";
sb[4] = "-";
sb[5] = "-";
sb[6] = "-";
sb[7] = "-";
sb[8] = "-";
sb[9] = "-";
sb[10] = "-";
check = 0;
wrong = 0;
cout << "Which word?(1-3): ";
cin >> word;
if (word == 1)
{
s[0] = "d";
s[1] = "i";
s[2] = "g";
s[4] = "t";
s[5] = "a";
s[6] = "l";
s[7] = "digital";
hint = "electronics";
cout << endl << "---" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input1();
}
else
if (word == 2)
{
s[0] = "p";
s[1] = "r";
s[2] = "o";
s[3] = "g";
s[5] = "a";
s[7] = "m";
s[8] = "i";
s[9] = "n";
s[10] = "programming";
hint = "C++";
cout << endl << "---" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << sb[6] << sb[7] << sb[8] << sb[9] << sb[10] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input2();
}
else
if (word == 3)
{
s[0] = "d";
s[1] = "u";
s[2] = "n";
s[3] = "c";
s[4] = "a";
s[5] = "duncan";
hint = "yoyos";
cout << endl << "---" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "----" << endl;
cout << "hint: " << hint << endl;
cout << sb[0] << sb[1] << sb[2] << sb[3] << sb[4] << sb[5] << endl;
cout << "wrong: " << w[0] << w[1] << w[2] << w[3] << w[4] << w[5] << endl;
input3();
}
}
int main()
{
newgame();
return 0;
}

Share this post


Link to post
Share on other sites
i changed the arrays back to single strings and it works. but id still like to know what i did wrong with the arrays. also, how do i get the window to close without prompting the user to hit the X button in the corner?

Share this post


Link to post
Share on other sites