-
Code Help needed.
I need some help just starting learning C++ a couple days ago. I REALY! wonna learn I tried to make a small password asking program. But i get a "password:no such directory/file exists" here is the code.
Code:
]#include <iostream>
#include <password>
using namespace std;
int main()
{
int password;
char Password;
cout<<"Please input your Password: ";
cin>> Password;
cin.ignore();
if ( Password = JJ66547) {
cout<<"WELCOME!!\n";
}
else if ( Password == adam ) {
cout<<"HAHA WRONG!\n";
}
else {
cout<<"LEAVE ME ALONE!!\n";
}
cin.get();
}
Any help please :(
-
>#include <password>
There is no need for this line.
int password;
There is no need for this line.
> char Password;
Declare this as a string:
string Password;
> if ( Password = JJ66547) {
Instead this should be:
if ( Password == "JJ66547") {
> else if ( Password == adam ) {
Same as above.
To use strings, make sure to:
#include <string>
at the top.
-
Code:
#include <password>
As far as I know, that's not a standard header file. Take that part out.You never use this variable. You can leave it in there, but there's really no need.
Code:
char Password;
//snip
cin>> Password;
Just so that you understand, you'll only read in a single character. You might want to try using a string instead.
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string Password;
//snip
Code:
if ( Password = JJ66547) {
You need to enclose your literal in quotes. Like this:
Code:
if ( Password == "JJ66547") {
You'll have to do that for each of your if-statements. Also, you need the equality operator '==' instead of the assignment operator '='.
-
Code:
#include <password>
There is no password header (unless it is a custom header you've created and placed in with the rest of your compilers header files). This is where I believe your error is comming from.
This only stores a single character, not enough for what you need. You want to use a string container (in which case you also need to include the <string> header), or an array of characters (in which case you are likely to need the functions declared in the <cstring> header).
Code:
if ( Password = JJ66547) {
= is the assignment operator, you want the equality operator == instead. Also, if using the string class, you would want enclose the text in red above in quotes "JJ66547". If using character arrays, you would need to use the strcmp function to test for equality.
Code:
else if ( Password == adam ) {
Same as my previous comment above (with regards to enclosing the text within quotes). At least you got the operator correct here.
-
Heh, anyone else want to take a stab at it? ;) Nice catch on the equality/assignment operator, hk_mp5kpdw. I'd hit you with some rep, but apparently I have to spread it around.
-
Thank you.
:D Thank You all SO much! It worked great. I'm gonna continue to expand on it. And learn to loop a little bit better.