Thread: I need help with a password program

  1. #1

    I need help with a password program

    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?????

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Please be specific if you want useful answers.
    My best code is written with the delete key.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    #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;
    }
    Password.size() returns the size
    Last edited by twomers; 08-17-2006 at 02:40 PM.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Location
    southern california
    Posts
    6
    Quote Originally Posted by twomers
    Code:
    #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;
    }
    Password.size() returns the size
    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.

    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.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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.

  7. #7
    Registered User stuart_cpp's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    31
    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;
    }

  8. #8
    Registered User stuart_cpp's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    31
    You can set width to your input!!
    Code:
    cin >> setw(8) >> attempt;
    And the header file for that is "iomanip"

  9. #9
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by stuart_cpp
    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;
    }
    I would avoid telling potential hackers that your password is of any certain length.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by MacNilly
    I would avoid telling potential hackers that your password is of any certain length.
    A brute-force hackers dream, I'd say.
    Code:
    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Ž

  11. #11
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by SlyMaelstrom
    A brute-force hackers dream, I'd say.
    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.

    But yeah, telling the length of the password is a BAD idea.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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;}

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    you can use strcmp()

    Code:
    // assume string is a variable where you place your string
    if(!strcmp(string,"mypassword)) cout<< "wrong password"
    yup giving a length of password can be BAD idea (^^)

  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    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.
    What happened to
    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.

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by mburt
    What happened to
    system("PAUSE")
    ??
    What happened to the FAQ?
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. Beginner needs help with password program
    By dnottus in forum C++ Programming
    Replies: 6
    Last Post: 04-22-2002, 06:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM