comparing strings

This is a discussion on comparing strings within the C++ Programming forums, part of the General Programming Boards category; i may be doing this wrong. im trying to open two files, assign the text inside to the variables logginname ...

  1. #1
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114

    comparing strings

    i may be doing this wrong. im trying to open two files, assign the text inside to the variables logginname and pass. Then the users input is set to valun and valpw. then i try to compare them with an if statement.

    Code:
    void login()
              {
              char valun[256];
              char valpw[256];
              ifstream a_file ("bin/liun.dat");
              a_file>> loginname;
              a_file.close();
              ifstream b_file ("bin/lipw.dat");
              b_file>> pass;
              b_file.close();
              cout<<"\n\n------------------------------------------------\n\n";
              cout<<"Please enter your username:";
              cin>> valun;
              cout<<"\nPlease enter your password:";
              cin>>valpw;
              if (valun == loginname && valpw == pass){
                        cout<<"\n\nVALIDATING....\n\n\n";
                        cout<<"ACTION CODE [757365722065786973747300] USER EXISTS!";
                        }
                        else {
                             cout<<"VALIDATING....\n\n\n";
                             cout<<"INTERNAL ERROR [75736572206E6F6E206578697374656E7400]";
                             cin.get();
                             return;
                             }
                             cout<<"\n\n----------------------------------------------------\n\n";
                             cout<<"PRESS ENTER TO CONTINUE TO CONTROL CENTER...\n";
                             cin.get();
                             desktop();
                   }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    What you're doing is comparing addresses, not string contents. To compare strings represented by null terminated arrays, you need something like the strcmp function in the <cstring> header:
    Code:
    #include <cstring>
    
    char a[] = "test";
    char b[] = "test";
    
    if (std::strcmp(a, b) == 0) {
      // Equal
    }
    else {
      // Not equal
    }
    My best code is written with the delete key.

  3. #3
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    Code:
    if (std::strcmp (valun, loginname) == 0 && std::strcmp (valpw, pass) == 0{
    generates and error!
    Last edited by Frantic-; 12-12-2004 at 10:22 AM.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It compares the two strings...

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The form of strcmp is as follows:
    int std::strcmp(const char* a, const char* b)

    The return (r) value is one of the following:
    r < 0 -> a is 'less than' b
    r == 0 -> a equals b (they are identical strings)
    r > 0 -> a is 'greater than' b
    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.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by Frantic-
    Code:
    if (std::strcmp (valun, loginname) == 0 && std::strcmp (valpw, pass) == 0{
    generates and error!
    Your missing a closing paren.
    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.

  7. #7
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    i know, i didnt copy the }.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You he said a closing ) not }

    Code:
    if (std::strcmp (valun, loginname) == 0 && std::strcmp (valpw, pass) == 0){

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 09:44 PM
  3. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 09:20 AM
  4. comparing strings
    By infinitum in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2003, 12:10 PM
  5. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 10:41 PM

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