Thread: question about comparing text in a file...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    question about comparing text in a file...

    How would I get my program to compare user imput to text stored in a file? Like checking to see if the user entered the correct password.

    Thanks for the help.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Open the file, read the password, close the file, read input from user and strcmp() it.

  3. #3
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Read the password from the file, store it in a string and then compare it to the user input.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    No matter what I enter in for the password it prints out that i have entered it in wrong, What did I do wrong, here is my code.


    Code:
    #include <fstream.h>
    #include <iostream.h>
    
    int main()
    {
      int end;
      char password[21];
      char realpass[21];
      cout<<"Password:\n";
      cin.get(password , 20);
      ifstream a_file ( "password.txt" );
    
      a_file>> realpass;
      if (password != realpass)
    
      { cout<<"You have wrongly entered the password \n"; }
    
      else
      { cout<< "you have correctly entered the password\n"; }
      cin >> end;
     return 0; }

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Use strcmp(), and you don't need the curly things for only one statement.

    Example:
    Code:
    if( strcmp(bleh, blah))
         cout << "oh my word";
    else
         cout << "more word action";
    EDIT: Added code tags.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    i used strcmp but it still says i have entered in the wrong code no matter what i put in

    Code:
    #include <fstream.h>
    #include <iostream.h>
    
    int main()
    {
      int end;
      char password[21];
      char realpass[21];
      cout<<"Password:\n";
      cin.get(password , 20);
      ifstream a_file ( "password.txt" );
    
      a_file>> realpass;
      if (strcmp(password , realpass))
    
       cout<<"You have wrongly entered the password \n";
    
      else
       cout<< "you have correctly entered the password\n";
      cin >> end;
     return 0; }

  7. #7
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Since it's C++, remove the .h on your headers. You'd probably want to be using namespace std too.
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    I did mention strcmp() in my last post but I was too lazy to edit it so you should use strings instead of character arrays. #include <string> and change cin.get(password, 20) to getline(cin, password), with password being a string instead of char[21].
    After that it should be fine, even the != doesn't need to be changed.

    [edit]
    "i used strcmp but it still says i have entered in the wrong code no matter what i put in"
    Input strings gets the newline character written to them if they fit. So if a user types "somepass" it gets stored as "somepass\n". This could be your problem if the one read from the file doesn't end with a newline character.
    You could try outputting the two strings with puts() and see if they actually differ, just remove them when you're done. It's possible that the string you read from the file isn't correct either.
    Last edited by OnionKnight; 03-19-2006 at 11:05 AM.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    ok i am confused, what is the difference in a string and a character array, sorry if it is a stupid question.

  9. #9
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Simply put a string is a character array that changes it size when needed so you aren't restricted to a set like 21 in your program.
    The string class is also the C++ way of handling text but it is of course still legal and ok to use character arrays if you want.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    'string' is the C++ string type. Essentially a way to make your life much, much easier in C++.
    Don't use char[] or strcmp unless you want to make life difficult (or if you're programming in C rather than C++)
    eg,
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
       const string password = "mypass";
       string input;
       cin >> input;
       if (password == input)
          cout << "Password match";
    }
    Much easier to read and understand!

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    Quote Originally Posted by Bench82
    'string' is the C++ string type. Essentially a way to make your life much, much easier in C++.
    Don't use char[] or strcmp unless you want to make life difficult (or if you're programming in C rather than C++)
    eg,
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
       const string password = "mypass";
       string input;
       cin >> input;
       if (password == input)
          cout << "Password match";
    }
    Much easier to read and understand!
    how woul i get this to check and see if the password the user entered matched teh first line of the file named list.txt?

    Was i doing that right in my previous post?

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by laar
    No matter what I enter in for the password it prints out that i have entered it in wrong, What did I do wrong, here is my code.
    Argggghhhh!!! (this to the other helpful posts)
    What you did wrong is not find out what was actually read from the file/user.

    This is always the easiest way to find out what is wrong. It works most of the time:
    After you read password, display it:
    Code:
    cout << "[" << password << "]" << endl;
    Surrounding the input with [ and ] lets you see of the newline is in the value or not.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    Quote Originally Posted by WaltP
    Argggghhhh!!! (this to the other helpful posts)
    What you did wrong is not find out what was actually read from the file/user.

    This is always the easiest way to find out what is wrong. It works most of the time:
    After you read password, display it:
    Code:
    cout << "[" << password << "]" << endl;
    Surrounding the input with [ and ] lets you see of the newline is in the value or not.
    thank you, turns out i needed to put C://password.txt instead of password.txt but it works now

    thank you for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 11:45 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM