I am trying to write a code that checks if password is pass with the use of functions...but i am not able to ....Please help me i am a beginner....and i need this program to be written strictly using functions....thsnk u in advance

Show us the code you have so far ... are where you think you are stuck.

Here is a student beginner level example of using functions to validate user input ...

// getValidInt.cpp

// http://developers-heaven.net/forum/index.php/topic,46.0.html

#include <iostream>
using namespace std;

int getValidInt( const char prompt[] )
{
    for( ; ; ) // an example of a C/C++ forever loop ... until 'return'
    {
        cout << prompt << flush;
        int testInt;
        cin >> testInt;
        if( cin.good() )
        {
            cin.sync(); // 'flush' cin stream as we go ...
            return testInt;
        }
        // else ...
        cin.clear(); // clear cin error flag(s) ...
        cin.sync();
        cout << "Invalid input! Integers only please ...\n" ;
    }
}

bool more()// defaults to 'true'/'yes' ... unless 'n' or 'N' entered
{
    cout << "More (y/n) ? " << flush;
    int reply = cin.get();
    cin.sync(); // 'flush' cin stream ...
    if( reply == 'n' || reply == 'N' ) return false;
    // else ...
    return true;
}


int main()
{
    int count = 0, sum = 0;

    do
    {
        int testInt = getValidInt( "Enter next integer to sum: " );
        sum += testInt;
        ++count;
    }
    while( more() ); // make sure cin stream is 'empty' before calling more()

    cout << "\nFor " << count << " numbers entered, sum was " << sum
         << " and average was " << float(sum)/count << endl;

    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.get(); // keep 'Window' open until 'Enter' key is pressed ...
}

Edited 2 Years Ago by David W

int main()
{
string getpassword();
string corrct(string password);
}
string getpassword(string password)
{
    cout<<"enter password";
    cin>>password;
    return password;
}
string corrct(string password)
{
    if(password!="pass")
    cout<<"wrong password";
    return password;


}

thank you david for your reply and thanks for the code snippet. I hardly understand the code,can you please provide me with a simpler code, i am totally a biginner tnq :)

You may like to take a look at this ...

// getPassWord.cpp //

#include <iostream>

using namespace std;

// passing in by 'ref' so value in calling scope is updated //
void getPassWord( string& password )
{
    cout << "Enter your pass word: " << flush;
    getline( cin, password );
}


int main()
{
    int count = 3;
    string password;
    while( count )
    {
        getPassWord( password );
        if( password == "pass" ) break;

        // else ... if reach here ...
         --count;
        if( count )
        {
            cout << "Wrong password ... try again ...\n"
                 << "You have " << count << " more tr"
                 << (count > 1 ? "ies.\n" : "y.\n" ) ;
        }
    }

    if( count ) cout << "\nYeh ... you got in ... \n";
    else cout << "\nYou are an intruder ... "
              << "NO access here for you today!\n";
}

Edited 2 Years Ago by David W: fixed posting problem ?

This article has been dead for over six months. Start a new discussion instead.