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".
#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);
}
elseif (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.
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.
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!!!!
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: constchar 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.
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.
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.