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
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;
}
}
}