passgate program

This is a discussion on passgate program within the C++ Programming forums, part of the General Programming Boards category; I just began learning C++ about a week ago and have ran into a very strange problem. I was writing ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    Question passgate program

    I just began learning C++ about a week ago and have ran into a very strange problem. I was writing a database program and was adding password protection to it...only problem was, it didn't work. lol. So I made a VERY simple passgate to see what my problem was. About 2 hours later, I'm still working on it and making no real progress. Its kinda embarrasing to ask because its such a simple program, but I just am not seeing the solution.

    Code:
    #include <iostream.h>
    
    int passcheck(char password[100]);
    
    
    int passcheck(char password[100])
    {    
         char correct[100] = "swordfish";
         if (password == correct)
           return 1;
         else
             cout << "\nWhat you typed: " << password;
             cout << "\nWhat you should have typed: " << correct;
           return 0;
    }
    
    
    
    int main()
    {
        char password[100];
        cout << "\nPassword: ";
        cin >> password;
        passcheck(password);
        cout << "\nExiting...";
        return 0;
    }
    OUTPUT:
    Password:
    What you typed: swordfish
    What you should have typed: swordfish
    Exiting...


    Maybe its just me, but I find it odd that it says I've typed in the right password, but that its somehow wrong. Any help would be appreciated.

  2. #2
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    You can't compare to char arrays like this.
    Take a look at C++ style strings. It will be much easier.
    http://www.cprogramming.com/tutorial/string.html
    Also, put the indented statements which are part of the conditional in "{}" brackets
    Code:
    if (something)
    {
      do something
    }
    else
    {
      do something else
    }
    C++ is not indentation sensitive, so in your above code, the second statement after the else would have gotten executed no matter what.
    Programming Your Mom. http://www.dandongs.com/

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    You should use c++ strings really, but your prog should work if you change the line
    Code:
    if( password == correct )
    to
    Code:
    if( !strcmp( password, correct ) )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21