Thread: Beginner needs help with password program

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Unhappy Beginner needs help with password program

    The problem I am having is that my program is not recognizing my password and when I enter the wrong password three times the program prints the "Wrong, try again" message before it prints the "See system administrator" message. Any help would be appreciated. My code is as follows:

    // Password Program

    #include<iostream.h>

    int main()
    {
    const int MAX=80;
    char password[MAX];
    int count=0;
    cout<<endl;
    cout<<"Enter a password to gain access to the Calendar Program:";
    cout<<endl;

    while (count<3)
    {
    cin.getline(password,MAX,'\n');

    if (password=="secret")
    count=99;
    else
    {
    cout<<"\nWrong password, please try again.\n";
    count++;
    }
    }

    if (count==99)
    cout<<"Welcome to the Calendar Program";

    if (count==3)
    cout<<endl;
    cout<<"\nSee your system administrator";
    cout<<endl;
    cout<<endl;
    cout<<endl;
    return 0;
    }

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    //if (password=="secret")

    this wont work, you'll have to use strcmp() in string.h

    basic structure --
    Code:
    if( strcmp(password,"secret")==0 )
     cout<<"ACCESS GRANTED";
    else
     cout<<"ACCESS DENIED";
    -

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Or you could use strings from <string> include file and you could use the == operator.

  4. #4
    I don't know if it is possible, but overloading the == operator for char* may do the trick... just have the explicit and implicit operators be compared by strcmp() in the overload function
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  5. #5
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    if (count==3)
    cout<<endl;
    cout<<"\nSee your system administrator";
    cout<<endl;
    cout<<endl;
    cout<<endl;
    return 0;

    Wont you need brackets for this if statement?
    I believe that it will only do the cout << endl; there but im not sure....

  6. #6
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299

    Basic c++ syntax

    Wont you need brackets for this if statement?
    I believe that it will only do the cout << endl; there but im not sure....
    http://www.cprogramming.com/tutorial/lesson2.html
    The structure of an if statement is as follows:
    if (TRUE)
    Do whatever follows on the next line.

    To have more than one statement execute after an if statement that evaluates to true, use brackets.

    For example:
    if (TRUE)
    {
    Do everything between the brackets.
    }
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    Here is a simple solution:

    #include<iostream.h>
    #include<string>
    int main()
    {
    const int MAX=6;
    string password;
    int count=0;
    cout<<endl;
    cout<<"Enter a password to gain access to the Calendar Program:";
    cout<<endl;

    do
    {
    cin>>password;
    cout<<"\n";
    if (password=="secret")
    {
    count=99;

    }else
    {
    cout<<"\nWrong password, please try again.\n";
    count++;
    }
    }
    while (count<3);

    if (count==99)
    cout<<"Welcome to the Calendar Program";

    if (count==3)
    cout<<endl;
    cout<<"\nSee your system administrator";
    cout<<endl;
    cout<<endl;
    cout<<endl;
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner program question.
    By Athelias in forum C++ Programming
    Replies: 4
    Last Post: 07-21-2005, 05:36 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Password recovery program..
    By ExDigit in forum Windows Programming
    Replies: 1
    Last Post: 01-12-2002, 09:45 AM
  4. Password program problem.
    By netboy in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 09:54 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM