I was trying to create a program that would require the correct password to be put in in order for it to display something. I was able to make a program that would require the right password to display a certain thing. But the problem is that it can only be a one letter password :(
This the the program that I made:

    #include <iostream>

    using namespace std;

    int main()
    {
        char a;
        char b = 'i';
        cout<<"Enter password: \n";
        cin>> a;
        if ( a == b ) {
            cout<<"Congratulations, you guessed the password!\n";
        }
        if ( a != b ) {
        cout<<"FAIL! :P\n";
        }
    }

This is a very simple program (as you can see). I would appreciate all help, but I would like it if I didn't have to make it loop. I would also like it if you could help me adding a password function (I think that's what it's called). Thank you in advance!

You can use strings rather than chars.

If you do not want to read in spaces then you can use cin as your method of input. If you want to include spaces (something like a frist and last name) then you need to use getline(cin, str) for input.

#include <iostream>

using namespace std;

string guess;               // Strings are a variable type to store letters
string answer = "Boom";
int main()
{
    cout<<"Case sensitive :)\n\n";
    cout<<"Try to guess my awesome password!\n";
    cin>>guess;

    if(guess==answer){
    cout<<"\nWOOHOOO! You guessed it!\n";     //Checks if the guess is right
    }

    if(guess!=answer){
    cout<<"\nOh no! it appears you FAIL! Hahaha!\n";      //checks if the guess is wrong
    }
}

here, you could also make it with a switch statement but its more complex.

and a loop for a retry, but i dont know how to do that >_<

sorry, but im a noob too :P

EDIT: i know how to do a loop, but not to make it give you a retry option. :P

you can change it to a 2 player-type game like this:

#include <iostream>

using namespace std;

string guess;
string answer;
int main()
{

    cout<<"enter a password for the guesser to guess\n";
    cin>>answer;

cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

cout<<"now enter a guess\n\n\n\n\n\n\n";

cin>>guess;
    if(guess==answer){
    cout<<"\nWOOHOOO! You guessed it!\n";
    }

    if(guess!=answer){
    cout<<"\nOh no! it appears you FAIL! Hahaha!\n";
    }
}

Thank you for the answers! I read a little bit more about it and discovered that (What i think) my favorite way to do it is to have some while loops. This is my updated program (quite abit more complicated)

#include <iostream>

using namespace std;

int main()
{
    char a;
    char b = 'z';
    char c = 'c';
    char d = 'a';
    char e = 'm';
    char f;
    char g;
    char h;
    int itsatrap;
    int boom = 22300;
    while ( a != b ) {
    cout<<"Enter password: \n";
    cin>> a;
    if ( a == b ) {
        while ( f != c )
        cin>> f;
        if ( f == c ) {
            while ( g != d )
            cin>> g;
            if ( g == d ) {
                while ( h != e )
                cin>> h;
                if ( h == e ) {
                cout<<"Welcome back sir.\n";
                }
            }

        }

    }
    if ( a != b ) {
        cout<<"Incorrect password\n";
        cin>> itsatrap;
        if ( itsatrap = boom )
            cout<<"\n";
    }
  }
}

Thank you for the suggestions (I realize that a string would clean it up a bit, but i want it to be complicated looking to fool my friends if they try to figure it out :)

On line 40 your if statement is assigning not comparing.

You want to use == not just =

Thats all I pick up from just glancing at it, other than the fact that is it "complicated".

yes i realized that after i compiled it. i made it in a hurry (i accidently pressed undo one too many times) i have also made one with strings that works better. i discovered that my friends don't know how to use linux based operating systems so i didn't need to make it look complicated.

#include <iostream>

using namespace std;

string password = "iamironman365";
string enter;
string trapped;
string way2 = "22300";
int main()
{
    cout<<"Enter password: \n\n";
    cin>> enter;
    if ( enter == password ) {
        cout<<"\nWelcome back sir.\n";
    }
    if ( enter != password ) {
        cout<<"\nWrong password.\n\n";
        cin>> trapped;
        if ( trapped != way2 )
            while ( trapped != way2 )
            cout<<"You are now trapped in an infinate while loop.\n";
        if ( trapped == way2 )
                cout<<"\nWelcome back sir.\n";


    }

}

i added an infinate while loop. this one is also a little bit "cleaner" :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.