Thread: Simple password system

  1. #1
    Registered User ex-mortis's Avatar
    Join Date
    Mar 2012
    Posts
    37

    Simple password system

    Code:
    #include <iostream>
    
    using namespace std;
    
    string pw;                                              //The variable in which the password is stored.
    int endprogram;                                         //A way to keep the program open.
    int exitprog;
    
    int main()
    {
        cout<<"Enter password: \n";
        cin>> pw;                                           //Places the user input in the string variable pw.
        for(; ;) {                                          //An unconditional loop intended as a way to have multiple attempts.
                   if(pw == "pw") {                         //If the password is "pw",
                         cout<< "Welcome.\n";               //print this.
                         cin>> exitprog;                    //Keeping the program open,
                         if (exitprog == 1) {               //and compares user input to 1
                                  exit(0);                  //Exits program
                         }
                   }
                   else {                                   //Otherwise,
                        cout<< "Try again.\n";              //print this and begin the loop again so that the password may be entered.
                        cin>> endprogram;                   //^
                   }
        }
    }
    It's obvious the program doesn't work but I'm not sure what to do to make it work. I want it to check whether pw is equal to "pw" and if it's not, print "Try again", and consequently "Enter password" again. Right now if you type in the wrong password it prints "Try again" and if you try to type in anything after that it loops that message infinitely.

    Also I wonder if someone can tell me how to detect keypresses by the user, because if(exitprog == 1) is a quite awkward way to go about it. Instead I want a "Press any key to terminate" sort of thing.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Depending on exactly what you want it to do, you may just need to move the first cout and cin into the loop (at the top).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User ex-mortis's Avatar
    Join Date
    Mar 2012
    Posts
    37
    Quote Originally Posted by oogabooga View Post
    Depending on exactly what you want it to do, you may just need to move the first cout and cin into the loop (at the top).
    Indeed. With a little tweaking (removing the cin from else) it works perfectly, at least it's what I wanted it to be. Though the welcome part is still very buggy. Anything but inputting 1 causes it to loop the messages "Enter password: " and "Welcome." infinitely. As I said I have no idea how to detect keypresses in C++, which is what I had in mind.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Detecting keypresses can't be done without some nonportable solution.
    If you enter anything but an integer when reading into an integer, you must clear the input stream (google it).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Input is buffered in standard C/C++, which means you don't know anything until the user hits return. There are OS-specific solutions however...

    Here is a Windows solution: Masking password input

    If you're not on Windows, here is how to implement "_getch()" on *nix: while getch question

    gg

  6. #6
    Registered User ex-mortis's Avatar
    Join Date
    Mar 2012
    Posts
    37
    However I still don't understand why it prints the messages "Enter password: " and "Welcome." precisely thrice when I input anything but 1. Here is a slightly updated version of the code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    string pw;                                              //The variable in which the password is stored.
    int endprogram;                                         //A way to keep the program open.
    int exitprog;
    
    int main()
    {
    
        for(; ;) {                                          //An unconditional loop intended as a way to have multiple attempts.
            cout<<"Enter password: \n";
            cin>> pw;                                       //Places the user input in the string variable pw.
                   if(pw == "pw") {                         //If the password is "pw",
                         cout<< "Welcome.\n";               //print this.
                         cin>> exitprog;                    //Keeping the program open,
                         if (exitprog == 1) {               //and compares user input to 1
                                  exit(0);                  //Exits program
                         }
                         else {
                              getchar();
                         }
                   }
                   else {                                   //Otherwise,
                        cout<< "Try again.\n";              //print this and begin the loop again so that the password may be entered.
                   }
        }
    }
    Compile it and run it and you'll see what I mean. Input "pw" as the password and then if you write anything other than 1 after that it does what I described above. Very peculiar.

  7. #7

  8. #8
    Registered User ex-mortis's Avatar
    Join Date
    Mar 2012
    Posts
    37
    Thanks. I get the idea, but it is a bit over my head. Namely "std::"; what exactly does it do (e.g. what is the difference between a non-std cout function and a std one?)? Otherwise it's simple enough, except for this part:

    Code:
    // Remove the unrecognized characters
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    Why do you need to input any arguments anyway? And what does the maximum number of characters have to do with it? Isn't it just checking whether the input is an integer?

    Besides, my program doesn't enter an infinite loop. As I said, it just prints the messages "Enter password: " and "Welcome.". It depends on the number or type of characters inputted how many times it prints those messages.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Hash/Salt/Shadow Password system
    By xivshin in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2010, 08:56 AM
  2. system() asks for password
    By prtsoft in forum Linux Programming
    Replies: 3
    Last Post: 10-13-2004, 04:29 PM
  3. system() clear password (XP)
    By B-Con in forum C Programming
    Replies: 4
    Last Post: 05-27-2004, 01:12 AM
  4. How can I display the system password
    By fundu725 in forum C Programming
    Replies: 4
    Last Post: 08-04-2002, 10:56 PM
  5. Ideas in creating a password system
    By subdene in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2002, 04:34 AM