473,803 Members | 3,306 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Program to input two char password (3 tries)

10 New Member
Write a program that asks the user to give a two-character password(define d 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 10840
sicarie
4,677 Recognized Expert Moderator Specialist
Write a program that asks the user to give a two-character password(define d 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
Thana1995
10 New Member
i'm allowed to use while loops but i really don't know how to use them
Jan 10 '08 #3
sicarie
4,677 Recognized Expert Moderator Specialist
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
Thana1995
10 New Member
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 Recognized Expert Moderator Specialist
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_counterOfNumb erOfTries (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_flagForCorrec tPassword 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_counterOfNum berOfTries is less than 3 OR b_flagForCorrec tPassword is set to true)
{

Perform an action to allow us to change i_counterOfNumb erOfTries
Perform an action to allow us to change b_flagForCorrec tPassword

}

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
Thana1995
10 New Member
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_counterOfNumb erOfTries (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_flagForCorrec tPassword 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_counterOfNum berOfTries is less than 3 OR b_flagForCorrec tPassword is set to true)
{

Perform an action to allow us to change i_counterOfNumb erOfTries
Perform an action to allow us to change b_flagForCorrec tPassword

}

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_counterOfNumb erOfTries
b_flagForCorrec tPassword
i don't get wat this is
Jan 19 '08 #7
sicarie
4,677 Recognized Expert Moderator Specialist
i_counterOfNumb erOfTries
b_flagForCorrec tPassword
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
2000
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
4428
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: 8f404d5399b6eb816fe579381a0e2e6c First, does PHP automatically encrypt the password type fields and if so what method does it use and can I disable it?
1
1438
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 server ? Thanks a lot ..
8
3290
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 - Boost.Program_options class library
4
1643
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 the user's input is blocked. Thx in advanced
9
2639
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. However the truth is I already see some technical difficulties at the planning level. I wish there are some relatively simple solutions to my problems and here they are: 1. I want to make this game under one command line enviroment with some...
5
14465
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?? #include <stdio.h>
5
9166
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, but when someone tries to untick the box it prompts for a password. I don't have the slightest clue where to start and have been staring at a For Dummies book for a good few days now. Useful Info: Field I am trying to lock is called...
3
5123
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 that when typing in my password it changes it to a *?? for example i type my first letter in "s" then when i enter the next letter in the previous one changes to *?? Thanks in advance. Private Sub othrps_Click() 'Attached to On Click event of...
0
9700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10292
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7603
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.