Im kind of new to C++ and can some one give me an example of a password program?
I know its probably really simple but yeah i could use an example.
Im kind of new to C++ and can some one give me an example of a password program?
I know its probably really simple but yeah i could use an example.
Probably not what you expected.Code:#include <iostream> int main() { std::string szPass("tehpassword"); std::string szGuess(""); while(szGuess != szPass) { std::cin >> szGuess; } std::cout << "You finally guessed the password!" << std::endl; return 0; }
There are quite a few examples posted already - try a board search.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I made thi5 5imple one, i know it5 not really much of anything but yeah it doe5nt compile can you tell me what5 wrong with it?Code:#include <iostream> using namespace std; int main() { int a; cout<< "Password please: "; cin>> a; cin.ignore(); if(a == 159753 ){ cout<<"Correct hit enter.\n"} else{ Cout<<"Incorrect\n"; } cin.get(); }
Read the compiler error messages and figure it out.
1) There's no semicolon after your second cout statement.
2) The last cout you do has a capital 'c'. That's wrong, since C++ is case-sensitive.
How do I get it to repeat from the incorrect password to asking for a password again?
Put the whole reading and checking of input into a loop with the condition being that the input is not the password. See my example.
I made it like this.Code:#include <iostream> using namespace std; int main() { int a; cout<< "Password please: "; cin>> a; cin.ignore(); if(a == 159753 ){ cout<<"Correct hit enter.\n"; } while(a != 159753) { std::cin >> a; } else{ cout<<"Incorrect\n"; } cin.get(); }
It says nothing is wrong but when i run it the program opens and then if I hit any key it just shuts.
Whats wrong?
I recommend that you read C++ Loop Tutorial before venturing any further. Your code should more look like:
Code:while (a != 159753) { // ask the user to enter the password }
Alright I re-read the tutorial part and this is what I came up with
It still does the thing where whatever I type in when I hit enter it closes no matter if its right or wrong.Code:#include <iostream> int main() { int a; cout<< "Password please: "; cin>> a; cin.ignore(); if(a == 159753 ) { cout<<"Your are correct, proceed by hitting enter\n" cin.get(); } do { cout<<"Incorrect try again.\n"; } while ( a != 159753 )
What is wrong here?
Last edited by PublicAutopsy; 03-26-2007 at 02:53 AM.
1. You have created an infinite loop since you never ask the user to enter the password inside the loop, so a will always stay what the user entered in the first place and the condition of the while loop will never be met.
2. There's a missing ; after the while ()
3. There's a missing ; after the last cout
4. the else if part is cut off from the corresponding if by the loop, this doesn't work.
5. Here's your solution in pseudo-code
Be aware that I intentionally removed every C++ syntax, so you have to change my code a fair bit. Also, you don't need the IF condition anymore since the loop condition does that for you already.Code:password = "secret" do { print "Please enter your Password" read userPassword } while (userPassword IS NOT password)
This is what I have now, same problem persistsCode:#include <iostream> int main() { int a; ( a == 159753 ) do { cout<< "Password please: "; cin>> a; cin.ignore(); cout<<"Incorrect try again.\n"; } while ( a != 159753 ); else if ( a == 159753 ) { cout<<"You are correct, press enter.\n"; }
1. You removed the if, yet you left the condition.
2. You are already printing the incorrect message before even checking if what he enters is correct.
3. You still have the "else if" part without corresponding if.
4. Are you sure you're even trying ? You seem to just remove some random lines, without giving it the slightest thought. If you don't want to learn how to program, have your code written by someone else or find a program that does what you need. You can't code without using your brain.
Sorry its 2 am here, I have a headache and I havnt lept isn over 24 hours.
What im doing seems logical at the time lol guess not though.
Im not going to bed until I get this.
Do you think I should just start over with this program?
Here:
Good NightCode:#include <iostream> int main() { int a; int password = 159753; do { cout<< "Password please:"; cin>> a; cin.ignore(); } while ( a != password); cout<<"You are correct, press enter.\n"; }