472,791 Members | 1,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

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 10618
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

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

Similar topics

2
by: SAN CAZIANO | last post by:
how can I have on the right of an input field the number of the char that we can put on it (when it's length is changed this number must changed itself: perhaps onkeypress event)???
8
by: Greg Scharlemann | last post by:
I've got a simple registration script that has an input field of type password. When I retrieve what is typed in the password field via: $_REQUEST; I always get the same encoded string:...
1
by: moi | last post by:
Hello, I have make a login form with an active directory membership. The trouble is that Internet Explorer may keep in memory the login/password of the user. Is there a way to disable it from the...
8
by: barcaroller | last post by:
Is there a recommended method of parsing program input parameters/options in C++ programs? The three methods that I know of are: - C's getopt() and getopt_long() - GNU C++ GetOpt class -...
4
by: chezz | last post by:
Hey i know its possible to block the users input from the keyboard and mouse, and to unblock but i was wondering how to check for users input and to compare it against a password (validate it) while...
9
by: bu0461 | last post by:
Hi this is my first post! I'm currently learning C programming language and have just finished structure section. At this point I think I'm able to program some very basic text command line games....
5
by: j.smith2c | last post by:
Hi everyone, I wanted to run the following loop, and enter y everytime (I expected that it will ask me to enter y for 5 time), but just after 3 times program terminates. what is the error?? ...
5
greeni91
by: greeni91 | last post by:
Hi Guys, I am looking for a way to bring up an input box to insert a password to unlock a field in a form. I am trying to do it so that if you tick a tick box, "ONTick" then the field will lock,...
3
by: INeedHelpPlease | last post by:
I have successfully created a password input box on a command button however when you type in the password it is all shown until you press enter. Is there any way that i can change the below code so...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.