Thread: Putting a password on the prog

  1. #16
    Registered User Inferno's Avatar
    Join Date
    Nov 2003
    Posts
    24
    ok Thanks Salem.... Also I have one more question about the Password for any of you.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      char Password[100] 
      cout<<"Please enter the 3 digit password";
      cin>>Password;
      if ( Password == 123 )
      cout<<"You've enter the correct password Congratulation!";
      
      system("PAUSE");	
      return 0;
    }
    There is nothing saying nething about a password in this which causes the the code when compiled to give an error. So anyway what I was wondering is what am I supposed to put the PW "123" as i tried putting it as int 123 up near the char but that didnt work(I only assumed i should do this becuase i thought it might need somewhere to start to actually run in the program)

  2. #17
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> if ( Password == 123 )

    c'mon now, you know you can't compare a char array like that! use strcmp() with char array (not int!) parameters. std::strings do have an overloaded operator == though, so you can use one like this:

    >> if ( Password == "123" )
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #18
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @Inferno & Sebastiani :

    haha, it's running ok after make a little correction!

    Code:
    #include <iostream>
    #include <cstring>   //add this for use string type
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      char Password[100];          //add ';' 
    
      cout<<"Please enter the 3 digit password: ";
      cin>>Password;
      
      if ((string) Password == "123" )  //converter array to string type
      cout<<"\nYou've enter the correct password Congratulation!";
      
      cout<<endl<<endl;
      system("PAUSE");  
      return 0;
    }

  4. #19
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Try this... much better than your attempt to cast to a string type (the proper string header is <string>).
    Code:
    #include <iostream>
    #include <string>   //add this for use string type
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      string Password;
    
      cout<<"Please enter the 3 digit password: ";
      cin>>Password;
      
      if (Password == "123")
        cout<<"\nYou've enter the correct password Congratulations!";
      
      cout<<endl<<endl;
      system("PAUSE");  
      return 0;
    }

  5. #20
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ jlou :

    Thanks for your help, I always think <cstring> is right head for string,
    now I know <string> is alright.

  6. #21
    Registered User Inferno's Avatar
    Join Date
    Nov 2003
    Posts
    24
    Thanks very much all of you for your help

  7. #22
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    Try my optimize version. Fixed

    Try to use my optimize version.
    It is likely C version, but anyway more optimize about space and memory.

    Regards.

    Now I've fixed event when input data buffer longer than expected.

    Thanks.
    Regards.

    Code:
    #include <iostream>
    
    using namespace std;
    char def_pass[]="iloveyou";
    
    int main()
    {
    		static int i =	strlen(def_pass)+1;
    		char* pPassword = new char[i];
    		cout << "Please enter the password:\n";
    		//cin  >> pPassword; // ok, maybe error
    		fgets (&pPassword[0], i, stdin) ;  // replacement		
    if (strcmp(pPassword,def_pass)!=0)
    			{
    			cerr << "Incorrect password.";
    			return 1;// exit program with error code = 1
    			}	
    		cout << "Correct password.";
    		delete pPassword;
    		//
    		//program begin here!
    
    		//
    		return 0;
    }
    Last edited by phoenixodin2; 09-18-2004 at 11:18 AM.

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This may crash if the password entered is longer than the real one.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #24
    Registered User
    Join Date
    Sep 2004
    Posts
    5
    thanks, CornedBee

    I've fixed.

    You see?

    It is OK, now?

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, it's one way to do it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading a password from a file.
    By medeshago in forum C Programming
    Replies: 15
    Last Post: 12-21-2008, 07:20 AM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 03:42 AM
  3. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  4. password prog
    By ihsir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-06-2002, 06:39 AM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM