I am trying to make a password word program
that uses the number of characters in the password
so i need help counting the characters?????
Printable View
I am trying to make a password word program
that uses the number of characters in the password
so i need help counting the characters?????
Please be specific if you want useful answers.
store the password in a string and get the number of characters with the size() member function.
Code:#include <iostream>
#include <string>
int main() {
std::string pass;
pass = "mypassword";
std::cout << "Password has " << pass.size() << " characters";
}
Password.size() returns the sizeCode:#include <string>
#include <iostream>
using namespace std;
int main( void )
{
string Password = "is Secret",
Attempt = "";
cout<< "Enter Password - ";
getline( cin, Attempt );
// or getline( cin, Attempt, '\n' ); but
// it automatically terminates at '\n', I think
if ( Password == Attempt ) cout<< endl << "Successful login!";
else cout<< endl << "No good!";
return 0;
}
I hope i dont sound stupid but when i attempted to use this code, after you input a password it seems it closes so i added a simple cin.ignore(); before the return, anyways here is a code that worked for me.Quote:
Originally Posted by twomers
EDIT: Maby it's because im using Dev-C++ ???
Code:#include <string>
#include <iostream>
using namespace std;
int main( void )
{
string Password = "thepass",
Attempt = "";
cout<< "Enter Password: ";
getline( cin, Attempt );
if ( Password == Attempt ) cout<< endl << "Succsessful Login!";
else cout<< endl << "No Good!";
cin.ignore();
return 0;
}
You may run into funny things like these that will seem kind of unintuitive. There's a nice FAQ that addresses some of Dev C++'s quirks that you might like.
http://www14.brinkster.com/aditsu/dev-cpp-faq.html
Quote:
When I run my console application, the black window flashes a bit then closes automatically. How can I see the output of my program?
Your program does exactly what you tell it to do, and you didn't ask it to wait before closing. You can add a command to wait for keyboard input before the program exits (e.g. before the return from main). A very simple example is system("pause"); which executes the "pause" system command (portability note: this only works in Windows and DOS); you need to #include <cstdlib> (or #include <stdlib.h> for C programs) if the compiler complains about the system function. Or you can use an instruction that reads something from the keyboard (e.g. cin.get();, getchar(); or cin>>variable;). In that case you may need to clear the input buffer before reading, e.g. cin.sync(); before cin.get();. Another option is to run your program from the command prompt; this is especially useful if you don't want it to wait before closing, or if it crashes before it reaches the keyboard-reading instruction.
Code:#include <iostream>
#include <string>
using namespace std;
int main()
{
string atmpt; // Attempt
string pass = "password"; // Password
cout << "Enter your password: ";
cin >> atmpt;
if ( atmpt.length() > 10 )
{
cout << "You entered nore than 10 charecters";
}
else
{
cout << "You're logged in!";
}
return 0;
}
You can set width to your input!!
And the header file for that is "iomanip"Code:cin >> setw(8) >> attempt;
I would avoid telling potential hackers that your password is of any certain length.Quote:
Originally Posted by stuart_cpp
A brute-force hackers dream, I'd say.Quote:
Originally Posted by MacNilly
Code:if(passEntered != passStored)
std::cout << "You didn't enter the correct password, which should be \"" << passStored << "\".\n";
I'm not sure if you're agreeing w/ me or not.Quote:
Originally Posted by SlyMaelstrom
But yeah, telling the length of the password is a BAD idea.
Um, reeL, there is actually an amazingly simple way to view the output. Run the program from the command line, as it was meant to be run. You kids have it so easy nowadays, what with IDEs and such...
you can use strcmp()
yup giving a length of password can be BAD idea (^^)Code:// assume string is a variable where you place your string
if(!strcmp(string,"mypassword)) cout<< "wrong password"
What happened toQuote:
When I run my console application, the black window flashes a bit then closes automatically. How can I see the output of my program?
Your program does exactly what you tell it to do, and you didn't ask it to wait before closing. You can add a command to wait for keyboard input before the program exits (e.g. before the return from main). A very simple example is system("pause"); which executes the "pause" system command (portability note: this only works in Windows and DOS); you need to #include <cstdlib> (or #include <stdlib.h> for C programs) if the compiler complains about the system function. Or you can use an instruction that reads something from the keyboard (e.g. cin.get();, getchar(); or cin>>variable. In that case you may need to clear the input buffer before reading, e.g. cin.sync(); before cin.get();. Another option is to run your program from the command prompt; this is especially useful if you don't want it to wait before closing, or if it crashes before it reaches the keyboard-reading instruction.
system("PAUSE")
??
My window closes immediately as well, so I do that.
Or I use a never-ending for-loop, and then break it if the requirements are met.
What happened to the FAQ?Quote:
Originally Posted by mburt
FAQ > How do I... (Level 1) > Stop my Windows Console from disappearing everytime I run my program?
This code may help u :) although it is very very very simpl. Just add the code that returns the characters :)
Code://Logical operators
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int main()
{
cout << "\tGame Designer's Network\n";
int security = 0;
string username;
cout << "\nUser Name: ";
cin >> username;
string password;
cout << "\nPassword: ";
cin >> password;
if (username == "S.Meier" && password == "civilization")
{
cout << "\nHey, Sid.";
security = 5;
}
if (username == "Josh" && password == "Josh")
{
cout << "\nWhat's up, Josh.";
security = 5;
}
if (!security)
cout << "\nYour LogIn has failed";
getch();
return 0;
}