need help! how to create a simple password program using if statement

Aug 15, 2013 at 5:04am
so i need a simple program for password identifier so please help...
:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	char Password=PLP;
	
	cout<<"please enter password \n";
	cin>>Password;
	if(Password == PLP)
		cout<<"Password Accepted \n";
	else
		cout<<"Invalid Password \n";

	return 0;
}
Aug 15, 2013 at 5:27am

Ok, first lets learn some basics:

If you want a variable that will hold more than one char ( which is called "string" or "array of char" you have to declare it like this:

char Password[] // this is also called "array" of char, You will learn this someday...;

and since you want a variable that will hold the input of the user, you do not need to assign any value to it... you should just declare it like this:

char Password[];

and in line 10 when comparing string values( which is a char greater than 1 ) you should enclosed PLP in quotes since this is a string and strings are always enclosed in quotes:

if( Password == "PLP" ) { ... }

And, if you want you can also declare using: string Password;.
But, you should first #include <string> to make that work..
That is just the same as declaring char Password[];


Hope you understand it!!!
Last edited on Aug 15, 2013 at 5:33am
Aug 15, 2013 at 6:37am
Many mistakes, did you started c++ or just jumped to this program. Whatever, here is the corrected form:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
	char Password [];
	
	cout << "please enter password \n";
	cin >> Password;
	if(Password == "PLP")
        {// I prefer to use these
		cout << "Password Accepted \n";
	}
        else
	{
		cout << "Invalid Password \n";
	}
	return 0;
}

You can also use string but you must include it; #include<string> and then no need of [code][][code]
Aug 15, 2013 at 6:43am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

using namespace std;

int main()
{
	const char SecretWord[] = "PLP";
	string Password;
	
	cout << "please enter password: ";
	cin>>Password;

	if ( Password == SecretWord )
		cout << "Password Accepted \n";
	else
		cout << "Invalid Password \n";

	return 0;
}
Aug 15, 2013 at 7:08am
@MrTrollFace

Many mistakes, did you started c++ or just jumped to this program. Whatever, here is the corrected form:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include <iostream>
using namespace std;

int main()
{
	char Password [];
	
	cout << "please enter password \n";
	cin >> Password;
	if(Password == "PLP")
        {// I prefer to use these
		cout << "Password Accepted \n";
	}
        else
	{
		cout << "Invalid Password \n";
	}
	return 0;
}



May I repeat your words?:)

Many mistakes, did you started c++ or just jumped to this program.
Aug 15, 2013 at 9:42am
@vlad from moscow
Why do you make a const char? Why? Why do you make your work harder?
Last edited on Aug 15, 2013 at 9:42am
Aug 15, 2013 at 2:33pm
Why do you make a const char? Why? Why do you make your work harder?
why is this harder? const prevents from accidentally overwriting data and it improves optimization by the compiler
Aug 15, 2013 at 4:40pm
@MrTrollFace

Why do you make a const char? Why? Why do you make your work harder?


it is harder only in your head because you do not know C++.:)
Sep 5, 2013 at 4:05am
thanks for the help guys! since you guys know things around here could any of you help me with a adding loop prog???
:D
Topic archived. No new replies allowed.