Thread: creating a simple program to accept a password.

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Question creating a simple program to accept a password.

    I just started learning c++ a few days ago and have come across a problem I can't seem to find a solution to. I figure one of you people with lots of experience well go "oh yah that's easy put such and such here and wala!" Anyway I am trying to make a program that asks you for a password and if you get the password right it says something and if you get it wrong it says somethings else. I have applications for this later down the road, but for right now I am starting simple.
    Here is what I have so far...
    #include <iostream.h>
    #include <string.h>
    int main()
    {
    char t[5];
    cout<<"enter password: ";
    cin.getline(t, 5, '\n');
    if (t=="trey")
    {
    cout<<"you are a god"<<endl;
    }
    else
    {
    cout<<"you got it wrong! Haha!"<<endl;
    }
    cout<<"you entered: "<<endl<<t<<endl;
    cout<<"the answer was trey"<<endl;
    cin.get();

    return 0;
    }

    I know I need to do something in line 8, but what I know not...If someone could help me I would greatly appreciate it.
    Thanks in advance,
    Trey

  2. #2
    Unregistered
    Guest

  3. #3
    Unregistered
    Guest
    ok i read it...how would i aplly it to my program?

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    ok ok ok...i think i figured it out...thanks alot man...this one's been bugging me for several days...lol

  5. #5
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    nit picking

    howdy,
    i went to the tut and started messing with it. it has an error - easy to spot though.

    if(!strcmp(str1,str2)

    should be

    if(!strcmp(srt1, str2))

    hope this isnt being to picky.
    M.R.

  6. #6
    Registered User skyline's Avatar
    Join Date
    Dec 2001
    Posts
    49
    then again, there is always the programmer-friendly version:

    Code:
    #include <iostream> 
    #include <string> 
    
    int main() 
    {
        string s1, s2("trey");
    
        cout << "enter password: ";
        cin >> s1;
    
        if(s1 == s2) { 
    	cout << "you are a god" << endl; 
        } 
        else {
    	cout << "you got it wrong! Haha!" << endl; 
        }
        
        cout << "you entered: " << endl <<s1 << endl; 
        cout << "the answer was " << s2 << endl;
        
        return 0; 
    }

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    password in cosole hidden with '*' as user types

    Here is a way to do it with asteriks. This will hide the password with asteriks as the user types it in.



    Code:
    #include<iostream>
    #include<conio.h>
    
    using namespace std;
    
    void GetPassword( char* entry );
    
    void main( void )
    {
    	
    	char password[10] = "admin";
    	char entry[10]    = "";
    	
    
    	cout << "\nEnter password: " << flush;
    
    
    	GetPassword( entry );
    
    	cout << "\nEntry....." << entry 
    		 << "\nPassword.." << password
    		 << "\nStatus...." << (strcmp(entry, password) == 0 ? " passed " : " failed ") << endl;
    
    
    }
    
    void GetPassword( char *entry )
    {
    	int i = 0;
    	char ch;
    
    	while( 1 )
    	{
    		while( !kbhit() ); //** pause until kentered
    
    		ch = entry[ i++ ] = (char)getch();
    
    		putchar( '*' );
    
    		if( ch == 13 ) //does not like '\n or '\0'
    		{
    			entry[i-1] = '\0';
    			break;
    		}
    		else if( i == 9 )
    		{
    			entry[i] = '\0';
    			break;
    		}
    	}
    }
    zMan

  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    does the string class do away with the need for strcmp?

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    nope

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    does the string class do away with the need for strcmp?
    yes because it has overloaded operators and its own set of compare member functions
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    is there a way to choose a char in the middle of a string(like temp[3])?

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    yes you can subscript a string with operator [] or you can use member fuction at()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 02-10-2009, 02:14 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM