how to store a password
Jan 3, 2009 at 9:15am UTC
Well i am still confused on how to make a program save info u typed. I dont no how to make it save it then require u to type the same thing again to continue if any1 culd help me with this code..its just practice
This code always goes to bad input when i try and enter the password.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>
#include <windows.h>
using namespace std;
void setcolor(unsigned short color) //The function that you'll use to
{ //set the colour
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
int main()
{
char name[14];
char pass[20];
string pass_set[20];
char yesno[1];
char *p;
loop:
cout<<" Enter your name: " ;
cin.getline ( name, 14 );
cout<<" keep name " << name << "? y / n " ;
cin>> yesno;
cin.ignore();
if (strcmp(yesno,"y" )==0) {
cout<<" Ok now enter a password: " ;
cin.getline ( pass, 20 );
cout<<"Remember your password!\n" ;
}
else if (strcmp(yesno,"n" )==0) {
cout<<" Ok chose a different name\n" ;
goto loop;
}
else {
cout<<" bad input " ;
return 0;
}
loop1:
cout<<" Ok " << name <<" lets begin.\n" ;
cout<<" Please enter your password: " ;
cin>> pass;
if (strcmp(pass,"pass" )==0) {
cout<<" correct " ;
}
else {
cout<<" bad input" ;
return 0;
}
}
Jan 4, 2009 at 7:28pm UTC
Firstly, you should move away from using C style char arrays and the goto whenever possible.
Here is a link to a guide on using strings and validating input from the console.
http://www.cplusplus.com/forum/articles/6046/
After that, you might want to read some info on C++ file I/O. This'll give you tutorials on how to save/load information to/from a file.
Topic archived. No new replies allowed.