-1

I am trying to do password handling in C++.

For example, I want the password to be 12345678, but no matter what I insert as a meaning of 'a' it always says that the password is correct, so basically it always thinks the quantity of 'a' is equal to the quantity of 'x'.

At the beginning, I tried to do it like this:

if (a = 12345678)                                                              
cout << "password is correct";

This didn't work either.

here is the picture of the code: https://i.stack.imgur.com/3zrvy.jpg

Mode77
  • 991
  • 8
  • 28
  • 8
    Don''t put pictures of code in the question. Put a [mcve] as text in the question. Btw: `a = 12345678` is assigning `12345678` to `a`, not comparing it for equallity. Use `==`. – Ted Lyngmo Oct 09 '19 at 19:57
  • 2
    Please try write code here using the adequate format. It is easier than watch a picture. BTW review the difference between = and ==. – Mauricio Ruiz Oct 09 '19 at 20:04
  • 3
    not everybody can access your link, not everybody can view the image. Questions about code are required to include a [mcve] in the question, if they dont then thats a reason to close the question. I am just telling this so you hopefully understand that Ted actually was trying to help you – 463035818_is_not_an_ai Oct 09 '19 at 20:05
  • 4
    ... i forgot the most important reason: nobody can copy paste your image to compile the code and see what it does (see [here](https://stackoverflow.com/questions/5508110/why-is-this-program-erroneously-rejected-by-three-c-compilers) for more on that topic) – 463035818_is_not_an_ai Oct 09 '19 at 20:07
  • @ლუკაალექსაია -- At some places `imgur` links are blocked and/or blacklisted. – PaulMcKenzie Oct 09 '19 at 20:08
  • 1
    Possible duplicate of [Variable changes on it's own in C++](https://stackoverflow.com/questions/58253071/variable-changes-on-its-own-in-c) –  Oct 09 '19 at 20:14

1 Answers1

4

The problem is a = 12345678 is operation that sets ato 12345678 and returns a by reference. So your if statement simply doesn't do what you want - instead of checking whether a is 12345678, it sets a to 12345678 and then checks if a isn't zero.

To fix it replace = with == for comparison.

if (a == 12345678)
    cout << "password is correct";

Though, it is a bit strange that password is a number and not a string... but whatever.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
ALX23z
  • 4,456
  • 1
  • 11
  • 18