Thread: Problem

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

    Question Problem

    hey,

    The following code is suppossed to allow someone to type in a username and password and it would log in. I ran into a big problem though that I can't figure out what the problem is. It's probably something simple but I can't figure it out.

    Everything works right up until the part where I put comments (//HERE). It doesn't seem to work, it always skips the if statement even if the passwords do not mach...


    Code:
    void login()
    {
         //Test Variables
         char testaccname[50];
         char testaccpass[50];
         char buffer[50];
         
         //Display Title
         cout<<endl <<endl <<"Login\n";
         cout<<"---------------------------\n";
         
         //Get Account Name
         accname:
         cout<<"Account Name: ";
         cin.getline ( testaccname, 50 );
         
         //Check To See If Account Name Exists
         ifstream test_file ( testaccname );
         if (! test_file.is_open() ) {
              cout<<"Invalid Account Name!\n" <<endl;
              goto accname;
         }
         
         //Get Password From File
         test_file.getline( buffer, 50 );
         test_file.getline( buffer, 50 );
         accpass:
         cout<<"Account Password: ";
         cin.getline( testaccpass, 50 );
         
    
         //HERE
         if (! buffer == testaccpass ) {          
             cout<<"Invalid Password!\n" <<endl;
             goto accpass;  
         }
         //HERE
    
    
         cout<<"Login Success!\n";
         getch();
         return;
    }
    Thanks.
    Anthony

  2. #2
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Use strcmp(), not ==.

    strcmp(c-string_1, c-string_2) will return 0 if the two strings are equal.

  3. #3
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Code:
    void login()
    {
         //Test Variables
         char testaccname[50];
         char testaccpass[50];
         char buffer[50];
         
         //Display Title
         cout<<endl <<endl <<"Login\n";
         cout<<"---------------------------\n";
         
         //Get Account Name
         accname:
         cout<<"Account Name: ";
         cin.getline ( testaccname, 50 );
         
         //Check To See If Account Name Exists
         ifstream test_file ( testaccname );
         if (! test_file.is_open() ) {
              cout<<"Invalid Account Name!\n" <<endl;
              goto accname;
         }
         
         //Get Password From File
         test_file.getline( buffer, 50 );
         test_file.getline( buffer, 50 );
         accpass:
         cout<<"Account Password: ";
         cin.getline( testaccpass, 50 );
         
    
         //HERE
          if (!strcmp(buffer, testaccpass) ==0){          
             cout<<"Invalid Password!\n" <<endl;
             goto accpass;  
         }
         //HERE
    
    
         cout<<"Login Success!\n";
         getch();
         return;
    }
    Also i dont know who's been teaching you but your use of goto is a bit overboard considering there are better way's of doing it.
    If you cant write a simple 20 line program without 'goto' your going to have problems when you start writing larger programs.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    if (!strcmp(buffer, testaccpass) ==0)
    Not quite... One of the following would work:
    Code:
    if (!strcmp(buffer, testaccpass))
    ... or ...
    Code:
    if (strcmp(buffer, testaccpass) == 0)

    *edit*
    And cgod is correct: gotos are uncalled for here. Look up 'for' 'while' and 'do... while' loops.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Zach L.
    Code:
    if (strcmp(buffer, testaccpass) == 0)
    It returns 0 if true. So if strcmp() == 0, then the strings are the same. You'd want !=, not ==, in that statement.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What you are overlooking is or may not know is: all array names are pointers. If you don't know what pointers are, they are variables that store addresses that indicate where a value is located in memory. In your case, there is a string of characters at the address stored in the pointer(i.e. the array name). In your program, you have three variables:

    char testaccname[50];
    char testaccpass[50];
    char buffer[50];

    and each one of them is located at a different address in memory. So, when you do this:
    Code:
    //HERE
         if (! buffer == testaccpass )
    you are saying: if the address of buffer is equal to the address of testaccpass, then perform the following block of statements, else skip them. Since the addresses aren't equal, the block is skipped.
    Last edited by 7stud; 03-13-2005 at 02:00 AM.

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    very easy to crack

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    It returns 0 if true. So if strcmp() == 0
    Thats my point.

    *edit*
    Okay, apparently I'm illiterate, and I had been up for ~17 hrs before posting that. I didn't see that you were actually testing for inequality. My apologies.

    *edit*
    As a side note, for clarity, I'd use one of the following forms of syntax:
    Code:
    if (strcmp(buffer, testaccpass) != 0)
    or
    Code:
    if (strcmp(buffer, testaccpass))
    but that is just a personal style issue (I don't like combining the logical negation and arithmetic equality tests, but of course, that syntax does work).

    Cheers
    Last edited by Zach L.; 03-13-2005 at 08:52 AM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM