Thread: Need help with a simple password checker using functions!

  1. #1
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20

    Unhappy Need help with a simple password checker using functions!

    Hi, kinda stuck trying to make a simple password checker which uses a function to check the password.
    When i run the program and then enter the password it come up with an error and shuts down the program. I think this may have something to do with the fact that i have not specified a return value but I a not sure.

    Could someone please explain to me what I am doing wrong here?

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    string password;
    int x;
    
    
    string password_checker( string password )
    {
        if ( password == "donkey" )
        {
            x = 16;
        }
    }
    
    
    int main ()
    {
        while ( x != 16 )
        {
            cout <<"PLEASE ENTER THE PASSWORD: ";
            cin >> password;
            password_checker( password );
            cout <<"\n\n\n";
        }
        cout <<"\n\n\n\n                         WELCOME";
    }
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes, either return a value (and make sure you use it), or make the function return void.

    Now is a really good time to learn about local variables, passing parameters and return results.

    Before you learn too many bad habits with global variables.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    1) It seems you want to return an int in your string function, so change string to int.
    2) Your variables should be declared in main, not globally.
    3) You should have an else statement there too, perhaps telling the user wrong password.
    4) since you are reading a string from the user in main, you should use
    Code:
    std::getline(std::cin,password,'\n');

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Rather than return int, you could also use the bool type for this to make the logic clearer. Instead of returning 16, return true if the password was validated, or false otherwise.

  5. #5
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20
    k thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple password program
    By BIGDENIRO in forum C Programming
    Replies: 4
    Last Post: 11-18-2013, 02:07 PM
  2. Simple password system
    By ex-mortis in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2012, 10:56 AM
  3. Password Checker
    By C_ntua in forum C++ Programming
    Replies: 22
    Last Post: 09-23-2008, 04:24 PM
  4. Simple spell checker
    By purplechirin in forum C Programming
    Replies: 31
    Last Post: 03-19-2008, 07:17 AM
  5. Simple login/password check
    By ceevee in forum C++ Programming
    Replies: 26
    Last Post: 01-17-2005, 12:05 AM

Tags for this Thread