Simple password checker

Ok, so i started learning c++ this morning and decided to create a program that checks a password that is already set. I compiled the program with no errors, but for some reason it accepts any text typed in. I need it to only work if the correct password is typed in so i was wondering if you geniuses could help(: I know its very basic, and not complete, but heres the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"

#include <iostream>

int main()
{
    using namespace std;
    int x = 987789;
	cout << "Please Enter The Password ";
	int y;
	cin >> y;

	if (y = 987789)

	{
		cout << "Correct! ";
	}
	


    return 0;
}






Thank you!!!
The = sign is not the same as the == operator =]

(you're assigning 987789 to y in your if parens)
Haha oh wow thank you!(: I knew it was probably something basic!
You can also use x in your expression on line 13 since you already set the number 987789 to equal it. No need to hard-code the number in anywhere because if you decide to change it you have to change it everywhere. If you use the variable x in all of your expressions you only have to change it once. ; )

What is the purpose of this statement:
 
int x = 987789; 


This shouldn't be necessary, unless your if statement is modified like this
1
2
3
if (y == x) {   //since x = 987789, then y == x is the same as y == 987789
     cout << "Correct!" 
} 


hmm Well I Have a few questions.

Why did you include the header #include "stdafx.h"?
if my memory serves me right stdafx.h is commonly used for win32 programing, but your justing programing with c++ so it is not need.

Why did you place using namespace std within the main function. By doing this everything that is written within the main function can access and use using namespace std but if you were to place a function outside of main you would get an error. Why not make using namespace global ( outside of int main() like you did with the headers that you included)

Global= anything can use it
local= only within the function it used in

besides that everyone else covered the true reason that your password checker isn't working
Hey thank you all for your help and I got it working perfectly, and Stormhawk, the reason I included stdafx.h was because my website that i've been learning from told me that if i'm using visual basic it requires it. I haven't checked to see if it does require it or not because I just assumed it was right. And the same thing for the namespace std, the website always had namespace std within the main function, so I thought it was right. Thanks for telling me it wasn't, I'll correct that!
No problem :)
Not sure what IDE you are using, but as long as you are for example creating a console application that has empty headers you won't need it :). Later on when you become really good in c++ you will find out the usefulness of #include "stdafk.h".
opiop65 wrote:
And the same thing for the namespace std, the website always had namespace std within the main function, so I thought it was right. Thanks for telling me it wasn't, I'll correct that!


Actually it is much better to use using namespace std; where you have it in the main function. Ideally using namespace std; should not be used in the global namespace. It can be the source of some subtle hard to find errors.

http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5
@galik opiops is just learning about c++ like he said just started a week ago. he mostly only touch the bare minimum in coding to get the hang of things and slowy progress forward. To tell him it is wrong because of possible errors that he won't encounter for some time will only just confuse him..why? so then where does he place the std and when and for what? he won't know. Hence why the beginner tutorial places it in global 1) because the code you will make for now will not conflict 2) they actually learn about c++ instead of checking for each little chance of creating errors.( not that it is a bad thing but you can't look for something you don't know what your looking for)

I am not saying your link won't help him in the future but for now it will create a lot of confusing. I told him to place it in the global because once he start to place functions outside of main it will give him errors because there is no std unless the tutorial he is following tells him about it.

opiops I am curious are you following www.learncpp.com?
Last edited on
Change "=" to "==" and you're on your way.
Topic archived. No new replies allowed.