468,457 Members | 1,690 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Program to input two char password (3 tries)

Write a program that asks the user to give a two-character password(defined by constants within the program). the program should then test the validity of the password. if it is incorrect after three tries, the computer should give some nasty message and then stop.

this is what i have so far but i tried it and it not working so i need help
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. int main()
  5.  
  6. {
  7.     int password;
  8.     int password1;
  9.     cout<<"Please enter a two-character password."<<'\n';
  10.     cin>>password;
  11.     cout<<"please enter your password."<<'\n';
  12.     cin>>password1;
  13.     if (password==password1)
  14.     {
  15.         cout<<"Thank you for logging in."<<'\n'<<"Welcome to c++ programming."<<'\n';
  16.     }
  17.     else{
  18.         cout<<"Sorry you have entered invalid password please try again."<<'\n';
  19.         cin>> password1;
  20.         if (password==password1)
  21.     {
  22.         cout<<"Thank you for logging in."<<'\n'<<"Welcome to c++ programming."<<'\n';
  23.         }
  24.         else{
  25.         cout<<"Sorry you have entered invalid password please try again."<<'\n';
  26.         cin>> password1;
  27.         if (password==password1)
  28.     {
  29.         cout<<"Thank you for logging in."<<'\n'<<"Welcome to c++ programming."<<'\n';
  30.         }
  31.         else{
  32.         cout<<"Sorry you have entered invalid you will not be allowed into this account."<<'\n';
  33.     }
  34.                 }    
  35.     }
  36.     return 0;
  37. }
Jan 9 '08 #1
7 9356
sicarie
4,677 Expert Mod 4TB
Write a program that asks the user to give a two-character password(defined by constants within the program). the program should then test the validity of the password. if it is incorrect after three tries, the computer should give some nasty message and then stop.
Out of curiosity, did you look into using a 'while' loop? Then you could do it while i_numberOfTries is less than 3 or s_inputPW is equal to s_expectedPW, and then depending on the exit conditions, (if s_inputPW is equal to s_expectedPW, then print welcome, if i_numberOfTries is 3 or more and s_inputPW is not equal to s_expectedPW) then print your other message.

It's almost the same, but just seems easier logically than the if chains...
Jan 10 '08 #2
i'm allowed to use while loops but i really don't know how to use them
Jan 10 '08 #3
sicarie
4,677 Expert Mod 4TB
i'm allowed to use while loops but i really don't know how to use them
They're pretty simple, once you get the hang of them. Try reading through this, and see if you have any questions.
Jan 10 '08 #4
i still don't get this this i know how to use a while loop but i don't know how to use it with this
pls some help me i'm goign to fail this class big time
Jan 19 '08 #5
sicarie
4,677 Expert Mod 4TB
It's okay, you're not going to fail your class, you just need to keep everything straight on when to use what and what goes where. And that comes with practice.

So while loops have a basic format.

while (something is going on)
{

do this repeatable function, that impacts the while test above

}

Now, in your case what you want to do something, while something else isn't working. You want to prompt the user for a password while the attempts are under 3.

So your condition will be i_counterOfNumberOfTries (or some variable to hold the attempts someone has made).

However, there you run into a problem. What if someone puts in a correct password? So then you need to check for that too, so you need a b_flagForCorrectPassword or some sort of boolean (true/false) flag to show that either the password is correct or isn't. If you hadn't noticed, I like to use descriptive variable names, and if they are simple datatypes (boolean, float, int), I prefix the variable with that and an underscore, just so it's easier to read.

So right there, you have your two conditions, and then you want the loop to check those conditions so you can pseudocode that:

while (i_counterOfNumberOfTries is less than 3 OR b_flagForCorrectPassword is set to true)
{

Perform an action to allow us to change i_counterOfNumberOfTries
Perform an action to allow us to change b_flagForCorrectPassword

}

So, now it's your turn. Without coding, can you tell me what needs to be done for either of the two statements above?
Jan 19 '08 #6
It's okay, you're not going to fail your class, you just need to keep everything straight on when to use what and what goes where. And that comes with practice.

So while loops have a basic format.

while (something is going on)
{

do this repeatable function, that impacts the while test above

}

Now, in your case what you want to do something, while something else isn't working. You want to prompt the user for a password while the attempts are under 3.

So your condition will be i_counterOfNumberOfTries (or some variable to hold the attempts someone has made).

However, there you run into a problem. What if someone puts in a correct password? So then you need to check for that too, so you need a b_flagForCorrectPassword or some sort of boolean (true/false) flag to show that either the password is correct or isn't. If you hadn't noticed, I like to use descriptive variable names, and if they are simple datatypes (boolean, float, int), I prefix the variable with that and an underscore, just so it's easier to read.

So right there, you have your two conditions, and then you want the loop to check those conditions so you can pseudocode that:

while (i_counterOfNumberOfTries is less than 3 OR b_flagForCorrectPassword is set to true)
{

Perform an action to allow us to change i_counterOfNumberOfTries
Perform an action to allow us to change b_flagForCorrectPassword

}

So, now it's your turn. Without coding, can you tell me what needs to be done for either of the two statements above?
i_counterOfNumberOfTries
b_flagForCorrectPassword
i don't get wat this is
Jan 19 '08 #7
sicarie
4,677 Expert Mod 4TB
i_counterOfNumberOfTries
b_flagForCorrectPassword
i don't get wat this is
They are both variables. One will be an integer, specifically the number of attempts someone has made so far, and the other will be a value that only holds true or false, and shows if the person entered the correct password or not.
Jan 20 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by SAN CAZIANO | last post: by
8 posts views Thread by Greg Scharlemann | last post: by
8 posts views Thread by barcaroller | last post: by
5 posts views Thread by j.smith2c | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.