Password

This is a discussion on Password within the C++ Programming forums, part of the General Programming Boards category; Hello, I am working on a program that requires a password. I am using: Code: #include<iostream.h> #include<conio.h> #include<fstream.h> #include<ctype.h> #include<stdio.h> ...

  1. #1
    KJ_Magic
    Guest

    Password

    Hello,

    I am working on a program that requires a password. I am using:
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<fstream.h>
    #include<ctype.h>
    #include<stdio.h>
    #include<string.h>
    
    int main()
    
    {
    	int ch;
    	char pword[BUFSIZ];
    	int p=0;
    	puts ("Enter Password: ");
    
    	while ((ch = getch()) != EOF 
              && ch != '\n' 
              && ch != '\r' 
              && p < sizeof(pword) - 1)
    	{
    		if (isalnum(ch))
        {
    		  putchar('*');
    		  pword[p++] = ch;
        }
    to have to user imput the password.

    The problem is i cant verify the password, i have tried: if else, while, and switch case.

    Can some one please tell me how to verify the password or if there is a better way to ask for imput.
    Thanks,
    -KJ Magic

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    How about using the std::string type to store your variable? I believe that class has an overloaded == you could use to verify a password.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    KJ_Magic
    Guest
    i dont mean to ask to many questions, but how would you use std::string?

  4. #4
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    Like so:
    Code:
    string pword; // instead of char pword[BUFSIZ]
    // p < sizeof(pword) - 1  <--- don't need this since the string size isn't predeclared
    pword += ch; // instead of pword[p++] = ch;

  5. #5
    KJ_Magic
    Guest
    i did this
    Code:
    int ch;
    	string pword;
    	int p=0;
    	puts ("Enter Password: ");
    
    	while ((ch = getch()) != EOF 
              && ch != '\n' 
              && ch != '\r' 
              
    	{
    		if (isalnum(ch))
        {
    		  putchar('*');
    		  pword += ch;
        }
    but i get the errors:
    C:\Windows\Desktop\Memorized.cpp(13) : error C2065: 'string' : undeclared identifier
    C:\Windows\Desktop\Memorized.cpp(13) : error C2146: syntax error : missing ';' before identifier 'pword'
    C:\Windows\Desktop\Memorized.cpp(13) : error C2065: 'pword' : undeclared identifier
    C:\Windows\Desktop\Memorized.cpp(21) : error C2143: syntax error : missing ')' before '{'

    -KJ Magic

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    2 Things:

    1) Add #include <string> to the top of your header file and put using namespace std; after all of your includes

    2) You are missing a ) after the comparison for /r in your while loop.

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    If I´m not mistaken you should include the cstring.h.

    #include <string.h>
    #include <iostream.h>


    int main(int argc, char *argv[]){

    string name;

    name = "I Love Fries";

    cout << name;

    return 0;

    }
    Nothing more to tell about me...
    Happy day =)

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    you don't need to use command line arguments for this case.
    C++ can hurt.

  9. #9
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    gustavosserra, try something like-
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string hello="hello";
    	cout << hello;
    	return 0;
    }
    and it compiles fine. But try it with cstring-
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
    	string hello="hello";
    	cout << hello;
    	return 0;
    }
    Compiling...
    hello.cpp
    c:\documents and settings\owner\desktop\hello.cpp(8) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no
    acceptable conversion)
    Error executing cl.exe.

    hello.obj - 1 error(s), 0 warning(s)

  10. #10
    KJ_Magic
    Guest
    ok now i am really confused!
    Code:
    {
    ipass:
    	int ch;
    	char pword[59];
    	int p=0;
    	puts ("Enter Password: ");
    
    	while ((ch = getch()) != EOF 
              && ch != '\n' 
              && ch != '\r')
    		if (isalnum(ch))
        {
    		  putchar('*');
    		  pword =+ ch;
    		}
    
    	if(!strcmpi("iwinipass", pword))
    	{
    		cout<<"Invalid password! Please renter password.\n";
    goto ipass;
    	}
    gives me:
    C:\Windows\Desktop\Memorized.cpp(27) : error C2440: '=' : cannot convert from 'int' to 'char [59]'
    There are no conversions to array types, although there are conversions to references or pointers to arrays
    Error executing cl.exe.

    but if i change char pword too string pword i get the errors:
    C:\Windows\Desktop\Memorized.cpp(30) : error C2664: 'strcmpi' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    hmmm

  12. #12

  13. #13
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    strcmpi() accepts 2 char pointers as parameters. Call it this way:
    Code:
    strcmpi("blablabla", pword.c_str());
    By the way, strcmp() and strcmpi() return 0 if the strings are the same.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @KJ_Magic:
    - Don't use goto's, control the code with a loop of some sort (for or while)

    >char pword[59];
    >pword =+ ch;
    You can't modify pword, as it's an array, and therefore const.

    - Don't forget to \0 terminate the string.

    Read the FAQ (link already posted) for an example of working code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    A very simple password program:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
    
    	string Input = "", Password = "whatever";
    
    	while(!(Input == Password)) {
    
    		cout <<"Enter password: " <<flush;
    		getline(cin, Input);
    	}
    
    	return 0;	
    }
    This thing is sooo simple that it's not really even useful. But it should give you the general idea. This should compile without error with MSVC.

Page 1 of 2 12 LastLast
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, 06:20 AM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 02: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 prompt in unix w/o \b
    By rafe in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 08:54 AM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM

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