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?????
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.
My best code is written with the delete key.
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"; }
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
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; }
Last edited by twomers; 08-17-2006 at 02:40 PM.
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.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; }
Last edited by reeL; 08-18-2006 at 08:02 PM.
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
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.Originally Posted by stuart_cpp
A brute-force hackers dream, I'd say.Originally Posted by MacNillyCode:if(passEntered != passStored) std::cout << "You didn't enter the correct password, which should be \"" << passStored << "\".\n";
Last edited by SlyMaelstrom; 08-22-2006 at 04:49 AM.
Sent from my iPadŽ
I'm not sure if you're agreeing w/ me or not.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...
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
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 toWhen 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?Originally Posted by mburt
FAQ > How do I... (Level 1) > Stop my Windows Console from disappearing everytime I run my program?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*