Kinda Hard To Explain

This is a discussion on Kinda Hard To Explain within the C++ Programming forums, part of the General Programming Boards category; Hi, I have yet another question. I'm gonna take one of my programmes which I wrote years ago. (Simply because ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Kinda Hard To Explain

    Hi, I have yet another question.

    I'm gonna take one of my programmes which I wrote years ago. (Simply because of the simplicity of it.) It was a real basic "password" console app, which I now want to expand just for the heck of it. It was somethin' like this :

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        std::string password;
        
        cout<<"Please enter a valid password:\n\n";
        std::getline ( std::cin, password );
        
            while ( password != "solaris" )
            {
                cout<<"Invalid password.\nPlease try again:\n\n";
                std::getline ( std::cin, password );
            };
    Then obviously, I would write the rest of the program. But I noticed that all the official "login programmes," always have a change password option, so I wanted one too. I have an idea about what I'm gonna do, but I'm not sure about how to do it.

    First, I'm gonna need somewhere to store the current password. Right ? I've also gotta make sure that every time the user "changes the password," it overwrites the current password, as oppose to creating a new one.

    Any ideas ?
    Thanks again.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Use a string variable.

    Code:
    string password = "solaris";
    Now give the option to change the password variable.
    Sent from my iPadŽ

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    If you want something interesting to do later store the password in a file which is encrypted/decrypted between writes/reads.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by ahluka
    If you want something interesting to do later store the password in a file which is encrypted/decrypted between writes/reads.
    One step at a time.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    If I use the variable string method, every time the program is executed, the password will always start off as "solaris". Surely it would be better for the program to extract information from a certain file or folder. Which reminds me :

    Is there any way to do that ?

    Firstly, how would I tell the program to collect the info needed from the file ?

    Secondly, what type of file must it be ? Would a simple txt document be sufficient ?

    My brother informs me that it has something to do with "fstream." Is this correct ?

    Thanks in advance.

    P.S. : I know this is jumping quite a bit, but it may not be a bad idea.
    Last edited by Necrofear; 12-08-2005 at 12:54 PM.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Hi again.

    Is there an easy way to translate a txt document to ASCII ?
    Cheers.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Text documents are in ASCII. I think you misread something. The lesson meant that it didn't cover binary files, which are a different kind of file i/o.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    By the way, why are you using std::cout etc? You've already defined that you're using namespace std;

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    I did misread something. Thanks.

    And I tried it without the std::cout ect, an it came up with a bunch of errors. I put them in, and it was fixed. That was wierd to me too.

    By the way, what does this actually do :

    Code:
    ifstream b_file ( "Password.txt" );
    b_file>> password;
    Ta.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    It opens the file called Password.txt for reading. Then, assuming password is a string and the file opened successfully, it reads from the file all the characters up to the first space and stores them in the password variable.

    If the file failed to open or if it is empty, and error would occur that is not handled by that code.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Unhappy

    I know this thread is weeks old, but I still have one question to ask.

    Would this work, providing I have a document called "Password.txt ?"

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        string password;
        string Rpassword;
        
        ifstream b_file ( "Password.txt" );
        b_file>> Rpassword;
            
        cout<<"Enter your password:    ";
        getline ( std::cin, password );
        
        while ( password != Rpassword ){
            cout<<"\n\nThe password you entered is invalid.\nPlease enter a valid password:    ";
            getline ( std::cin, password );
        }    
        
        cin.get();
    }
    It does compile and everythin', but I've been doing some experimenting, and when it reads "Password.txt," it reads it as a blank document. And yes, I've checked that there are some characters in the file. (and there are.)

    Any help available ?
    Cheers.

    P.S. : Rpassword stands for real password. Just checkin'.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    That code looks fine and works for me. You might want to check b_file.is_open() to make sure it found the file correctly, or try giving the full path (e.g. "C:\\Password.txt") to make sure it is looking in the right place.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Success !!!

    Thanks for all your help !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read hard disk serial number
    By toufiq_raja in forum C Programming
    Replies: 4
    Last Post: 08-18-2005, 10:08 PM
  2. is this old hard drive any good?
    By Pyroteh in forum Tech Board
    Replies: 11
    Last Post: 07-16-2005, 08:34 PM
  3. Replies: 2
    Last Post: 07-06-2005, 07:11 PM
  4. Strange hard disk
    By GanglyLamb in forum Tech Board
    Replies: 20
    Last Post: 03-01-2003, 03:05 AM
  5. Failing Hard Drive
    By RoD in forum Tech Board
    Replies: 10
    Last Post: 02-18-2003, 07:08 AM

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