Just for your info, there's an example of how to obtain a password from the user here.
Just for your info, there's an example of how to obtain a password from the user here.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Sorry guys...looks like 24 bits to me. Maybe 3 bytes. Could be more.
This looks compiler-specific to me.Originally Posted by C++Child
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
Actually, it's 3 bytes (prize of a ++rep goes to pianorain!).yeah it's 4 bit pasword!!! zero is null duh!!mmm sorry a little distraction
mmm code tags?
How?? I read the post but I didnt got it. =(
To use code tags, put [ code ] before your code and [ /code ] after your code (without the spaces, I'm just putting the spaces so that it doesn't think I'm actually using code tags ). Read the link in Salem's signature for more info.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Well, statistically speaking, it'll most likely be less than the equivalent of a 3-byte (24-bit) password. You are likely to have only 62 possible entries in those three spaces (a-z, A-Z, and 0-9), assuming you let the user type it in, instead of the maximum possible of 256 (2^8). So, instead of there being 256^3 (2^24) possible keys, there are 62^3. So, doing a bit of rounding so I don't need to pull out a calculator (62 ~ 64), we see that there are in fact ~64^3=2^18 possibilities, so what you have there is, in effect, an 18 bit (or 2.25 byte) password.
Sorry, I couldn't help myself.
Well, technically you CAN input direct ASCII values for the password. Hold ALT and press 1,2,3 and then release ALT, and you'll end up with the character corresponding to ASCII 123. I've done that for several of my DOS programs before I realized that you could get the same effect from outputting (char)123 Although, on Windows there seems to be some oddity where alt-023 would give a different result from alt-23... Of course, if the user sets his/her own password, it should be no problem anyways.
Sorry, I couldn't help myself. ^2
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
ok now Im testing the code tags (thank you toysoldier
I hope this worksCode:#include <iostream> #include <cstdlib> #include<conio.h> using namespace std; int main() { char m[3]={'t','o','y'}; //to save original password. char n[3]={'0','0','0'}; //initialize your entering password. cout<<"\n" cout<<"\nPlease enter 3 bit password: "<<flush; n[0]=getch(); //enter word ,but no display it cout<<"*"<<flush; //when enter word, display “*” n[1]=getch(); cout<<"*"<<flush; n[2]=getch(); cout<<"*"<<flush; if(m[0]==n[0]&&m[1]==n[1]&&m[2]==n[2]) //stupid code! { cout<<"password correct,welcome!"<<endl; } else { cout<<"error."<<endl; } system("pause"); //pause running window. return 0; }
Have I crossed the line?
Gee, how did you know my real name is toysoldier?
**EDIT**
And congrats on your first successful code tag
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
>>Read the link in Salem's signature for more info.
I guess you mean mine?!
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Sorry. I swear, it was a typo! *edits post in a lame attempt to cover up*
**EDIT**
Foiled. The edit button's gone on my post
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
There's an easier way to compare strings in cstring. Use strcmp(). strcmp() returns a value of false (0) if both strings are the same. so say if(!strcmp(m[],n[])Code:if(m[0]==n[0]&&m[1]==n[1]&&m[2]==n[2]) //stupid code!
Child who knows C++Using Borland C/C++ Compiler 5.5 (Command Line Version)
>>so say if(!strcmp(m[],n[])
Or the equivalent but arguably less clearCode:if (strcmp(m, n) == 0)
However, in this case because the arrays are not terminated by a null character they are not technically strings of any kind. As such, strcmp will cause undefined behavior because it will test beyond the boundaries of the array.Code:if (!strcmp(m, n))
Even if the arrays were valid C-style strings you would still be wise to consider the overhead of a library function call over three simple equality tests. In some cases the difference may be enough to argue strongly in favor of the inline code.
mmm ok thx everybody, also Hunter thx,and toysoldier, I owe u one, thx 4 the patience
sorry If anyone get mad, Im sorry
well practicing the the tips you gave,
I just want to put how it looks 4 the newbies like me, Ive learned a lot!!!
andCode:#include <iostream> #include <cstdlib> // original #include<stdlib.h> #include <conio.h> using namespace std; int main( ) { char m [4] = "toy"; char n [4] ="000"; cout<<" \n enter 3 bit password:"<<flush; n [0] = getch( ); // getch( ); cout<<" * "<<flush; n[1] = getch( ); cout<< " * "<<flush; n[2] = getch( ); cout<< " * " <<flush; if (strcmp( m , n ) = = 0 ) { cout<<" password correcta, bienvenido"<<endl; } else { cout<<"error "<<endl; } system ("pause"); return 0; }
ok guys tell me , what is the diference between the endl,flush like appears belowCode:#include <iostream> #include <cstdlib> #include <conio.h> using namespace std; int main( ) { char m [4] = "toy"; char n [4] ="000"; cout<<" \n enter password:"<<flush; n [0] = getch( ); cout<<" * "<<flush; n[1] = getch( ); cout<< " * "<<flush; n[2] = getch( ); cout<< " * " <<flush; if (strcmp( m , n ) = = 0 ) { cout<<" password correcta, bienvenido"<<endl; } else { cout<<"error "<<endl; } system ("pause"); return 0; }
thx in adv.Code:cout<<"whatever"<<endl; cout<<"whatever"<<flush; cout<<"whatever";
love abyss
Have I crossed the line?
this is the princeton's way!!!
Code:#include <iostream> #include <cstdlib> #include <conio.h> using namespace std; int main( ) { char m [4] = "toy"; char n [4] ="000"; cout<<" \n enter password:"<<flush; n [0] = getch( ); cout<<" * "<<flush; n[1] = getch( ); cout<< " * "<<flush; n[2] = getch( ); cout<< " * " <<flush; if (!strcmp( m , n ) ) { cout<<"this is the password , welcome"<<endl; } else { cout<<"error "<<endl; } system ("pause"); return 0; }
Have I crossed the line?
>>what is the diference between the endl,flush
endl will print a newline and flush the stream, flush will not print a newline and flush the stream.
>>this is the princeton's way!!!
Well, it is not the way I would do it, but that will suffice. I would do something more like this if asked to use conio.h.
Code:#include <cstdlib> #include <fstream> #include <iostream> #include <string> #include <vector> #include <conio.h> namespace { const std::string default_file("input.txt"); } std::string decrypt(const std::string& pass); void get_pass(std::string& buffer); bool valid_pass(const std::string& buffer, const std::vector<std::string>& passwords); int main(int argc, char *argv[]) { std::string buffer; std::vector<std::string> passwords; std::string pass_file(default_file.c_str()); // Acquire filename and open file if (argc > 1) { pass_file.assign(argv[1]); } std::ifstream in(pass_file.c_str()); if (!in.is_open()) { std::cerr << "Error opening password file" << std::endl; return EXIT_FAILURE; } // Load passwords into memory while (std::getline(in, buffer)) { passwords.push_back(decrypt(buffer)); } buffer.clear(); // Get user password and validate get_pass(buffer); if (valid_pass(buffer, passwords)) { std::cout << "Valid password" << std::endl; } else { std::cerr << "Invalid password" << std::endl; } return EXIT_SUCCESS; } std::string decrypt(const std::string& pass) { // Placeholder for a password encryption scheme return pass; } void get_pass(std::string& buffer) { int ch; while ((ch = getch()) != '\r') { if (ch == '\b') { // Handle backspacing intelligently buffer.erase(buffer.end() - 1); cprintf("\b \b"); } else { buffer.push_back(static_cast<char>(ch)); putch('*'); } } putch('\n'); } bool valid_pass(const std::string& buffer, const std::vector<std::string>& passwords) { std::vector<std::string>::const_iterator it = passwords.begin(); const std::vector<std::string>::const_iterator end = passwords.end(); for (; it != end; ++it) { if (buffer == *it) { return true; } } return false; }