What is "Stack around the variable was corrupted."

I have made a very simple number game that add minus divide and multiply however when I run it at the first option the ide( I think) says that Stack around the variable 'answer' was corrupted. What did I do wrong and if you do know what I did wrong can you please clarify what I did wrong. Thank you.
I am very very very newwb C++ "programmer".

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
  #include<iostream>
#include<stdlib.h>

int main()
{
	std::cout << "This is a Numbers Game Made By " << "\n" << "A small description of this game is that it produces diferrent Numbers that you must minus add divide multiply in the same order." << "\n" << "Enjoy";
	char yes[] = "yes";
	char no[] = "no";
	char answer[]="0";
	std::cout << "If you would like to play this game type down 'yes'if not however type down 'no'.";
	std::cin >> answer;
	if (answer == yes)
	{
		int w, x, y, z, sum, zata;
		int m;
		do
		{
			m = rand() % 30 + 1;
			w = rand() % 30 + 1;
			x = rand() % 30 + 1;
			y = rand() % 30 + 1;
			z = rand() % 30 + 1;
			sum = w - x + y / z * m;
			std::cout << w << "-" << x << "+" << y << "/" << z << "*" << m; 
			std::cin >> zata;
			if (zata == sum)
			{
				std::cout << "Congrats....want to play again (say yes or no)";
				std::cin >> answer;
			}
			else
			{
				std::cout << "try again (say yes) or give up (say no)";
				std::cin >> answer;
			}
		} while (answer == yes);
	}
	else if (answer == no)
	{
	std::cout<< "well why did you even come here for";
	return 0;
	}
}
Line 9, you create a character array that holds two characters: '0' and '\0' (the null terminator). On line 11 you overrun this storage space and start trying to write to other memory, thus corrupting it.
char answer[]="0";

I think you should use some other kind of buffer here,
because user input will overrun this array which is set to size 1, but input contains more than one character.

ie. std::string answer should work
or char [10]
Last edited on
code kiddy Thank you very much the call stack is gone now. however whenever I run the program when I say 'yes' the program immediately exits with a code 0 without anything appearing after it
I tried this on several different compiler but its still the same
I will keep checking what happens if I change the code a little bit
Edit
I fixed the problem by changing char yes,no into std::string yes,no.
My game works!!!!
Last edited on
if (answer == yes)
this will most likely be false because:
char yes[] = "yes";
is not "yes",
since answer is {'y' 'e' 's' '\0' } but yes variable is {'y' 'e' 's'} which is not the same.

changing variable yes to:
const char yes[] = {'y' 'e' 's' '\0' };
should solve the problem, although it's not usual to write the code that way.

every char sequence should contain a null character '\0' which signals the end of a string.

I really never use plain chars so I might be forgot how this works really.

To compare two character strings, use strcmp()

This will output "not equal":
1
2
3
4
5
6
7
    char yes[]    = "yes";
    char answer[] = "yes";
    
    if (answer == yes)
        cout << "yes";
    else
        cout << "not equal";

The reason why is that none of the characters in the strings are ever compared. The code merely tests whether the addresses where the strings are stored is the same, obviously they are not.

This would work:
1
2
3
4
    if (!strcmp(yes, answer))
        cout << "yes";
    else
        cout << "not equal";


Though using std::string is much easier, as already suggested.

http://www.cplusplus.com/reference/cstring/strcmp/


codekiddy wrote:
char yes[] = "yes";
is not "yes",
since answer is {'y' 'e' 's' '\0' } but yes variable is {'y' 'e' 's'} which is not the same.
This is false. The null terminator is automatically appended to string literals. Both answer and yes will hold the same things, but will not evaluate as equal because of reasons specified in Chervil's post.
Last edited on
Topic archived. No new replies allowed.