Thread: recalling data

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Then a little (or a lot) more information about the structure of the program would help.

  2. #17
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    Code:
    	system("cd desktop");
    	system("cls");
    	ifstream verpass ( "password.txt" );
    	if ( 
    	string pass;
    	cout << "Please Enter Your Passsword: ";
    	cin >> pass;
    	ofstream passfile ( "password.txt" );
    	passfile << pass << endl;
    	passfile.close();
    	system("cls");
    	string pass2;
    	cout << "Please enter it again: ";
    	cin >> pass2;
    	if ( pass != pass2 ) {
    		system("cls");
    		cout << "Password verification is incorrect!" << endl;
    		system("pause");
    	}
    	else {
    		system("cls");
    		cout << "Password verification is correct!" << endl;
    		system("pause");

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you're going to use system() so much, you may as well write a batch file!.

    > system("cd desktop");
    Is the most useless thing in the universe?

    Why?
    Your program is in directory foo
    You do system("cd desktop");
    A new process is created, and a cmd.exe is run
    The cd command is run in that new process (so it is now in foo/desktop)
    The new process exits.
    Your code is still in directory foo.

    Code:
    	ifstream verpass ( "password.txt" );
    	if ( 
    	string pass;
    	cout << "Please Enter Your Passsword: ";
    	cin >> pass;
    	ofstream passfile ( "password.txt" );
    1. Why are you opening the same file twice?
    2. What is that bare if ( doing?

    My advice is you start with a lot more reading, and then try "hello world" again, because it seems like you're just stumbling around in the dark hoping to find something useful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    ok ok... wait -- what im really asking is this --> how would i be able to verify that a text file is present or if it does not exist?

  5. #20
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The most portable (and maybe only portable) way to verify that a file exists is to open it for read. If the open fails, then the file does not exist.

  6. #21
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    oh duh! that makes sense! thanks!

    but one last problem --> what method would i use to verify whats in the textfile? I know you have to do a ifstream to the file but im having the problem of how i would compare the two strings.

    Code:
                    string passwordmade;
    		cout << "Please Enter Your Password: ";
    		cin >> passwordmade;
    		ifstream passwordconfirm ( "passwordverify.txt" );
    		if ( passwordmade ==
    and tthis is where i go blank

    (this is after the text file is confirmed and the program asks to verify the contents of the text file.)
    Last edited by Warhawk; 09-27-2005 at 08:28 PM.

  7. #22
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    do i have to declare a string that equals a string in the text file? kinda like in fstream when you use this:
    Code:
    ifstream passwrd ("password.txt");
    cout << str;
    passwrd.close();

  8. #23
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You could try something like this.

    Code:
    //declare string to hold file name
    string filename;
     
    //obtain file name
    cout << "enter file name/path" << endl;
    cin >> filename;
     
    //declare ifstream to read from file
    ifstream filein(filename.c_str());
     
    //see if file opened or not
    if(!filein)
    {
      //file didn't open
      cout << "unable to open " << filename << endl;
     
    //file called file name may not exit or you have 
    //incorrect path to file or whatever
    }
    else
    {
      //file did open
     
      //declare string to hold password from file
      string password = "empty";
     
      //read file data into password
      filename >> password;
     
      if(password == "empty")
      {
    	//file called filename existed but was empty
    	cout << "no password in " << filename << endl;
      }
      else
      {
    	//declare string for user input
    	string input;
     
    	 //ask user for input
    	cout << "enter password" << endl;
    	cin >> input;
     
    	//compare user input to password from file
    	if(password == input)
    	{
    	  cout << "proceed" << endl;
    	}
    	else
    	{
    	  cout << "try again" << endl;
    	}
      }
    }
    Last edited by elad; 09-29-2005 at 09:06 AM.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM