it keeps crashing when i enter password

I made this script, and it compiles with no errors, but when i type in the password, it says "program" stopped working?

#include <iostream>
#include <queue>
#include <Windows.h>
#include <fstream>
#define x 1234

using namespace std;


using namespace std;
int main(void)
{
while(true)
{
char *mypwd = "test";
char *password = "test";
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
system("title test");
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "" << endl;
cout << " "<< endl;
cout << "" << endl;
cout << "Enter Password:" << endl;
cin >> password;
if (password != mypwd)
{
while(1)
{
if(GetAsyncKeyState(0x46))
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Sleep(x);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(x);
}
}
}
return 1;
}}
char *password = "test";

In C and C++, anything inside " symbols is a string literal. It is constant. Changing it is forbidden. You are not allowed to change it.

cin >> password;
This looks like you're trying to change the string literal. This is not allowed. Even if you could write over the data, this is very silly. You've got enough space for four letters; the letters in the word "test". What happens if the user enters more than four letters? You'd start writing over the next memory locations, and you'd be trashing data.

if (password != mypwd)
This is nonsensical. You are comparing a char-pointer to another char-pointer. Unless they both point to the same place in memory, they will always be different. I suspect you meant to compare what they are actually pointing to.

I suggest two things. Firstly, you're clearly still learning about pointers. Here is what I wrote to help people learn about pointers: http://www.cplusplus.com/articles/EN3hAqkS/

Secondly, this is C++. We have proper string objects for this sort of thing.
Last edited on
Topic archived. No new replies allowed.