Basic password code

Im trying to match user input to a set password (running a loop to repeat the question after failure)

My problems are that I dont know how to check for specific user letter input and Im not exactly sure how to format it into the loop

(its my first post on this site so tell me if I should change my post formatting)
closed account (48T7M4Gy)
My problems are:
1. how to check for specific user letter input
2. how to format it into the loop

Solve those two and you're in business.

To start, try writing a small program to do 1.
Last edited on
I have no clue how to set the first one though and cant find any examples on how to do it
(the second one is less of an issue)
closed account (48T7M4Gy)
cin
Im trying to get something along the lines of
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
using namespace std;
int main() {

	cout << "Please enter the password";
	if (cin = "password")
		cout << "Access granted"
	else
		cout<<"Imput failure";
}


using a loop though
Last edited on
closed account (48T7M4Gy)
First, you need == not just = in your if statement. Do you remember that one??
closed account (48T7M4Gy)
Then you could use a bool

1
2
3
4
5
granted = false
while ( granted == false )
{
   blah blah
}
how would I trigger the bool to be true if the user imputed the correct thing in that case?
and yes, I spaced on the double equals, I was messing about in vb last
Last edited on
closed account (48T7M4Gy)
Hint: granted would become true if the password entered by the user matched the password recorded on the system for that user.

Make sense?
That makes sense
I just wouldn't know how to set the password to then compare them (the user here wouldn't be setting the password, its just a basic setup where I set it in code and the user has to input it to be granted access access to nothing in this case, the code ends there)
closed account (48T7M4Gy)
What you can do is create a "system" password. So you'd have:

1
2
3
4
5
6
string password = "open123"; // this is the password stored on the system
string userPassword = "";

blah, blah
cin >> userPassword;
blah blah 
that's really useful to know
I think I have it about right but Im missing something really simple I think
its telling me the that no operator exists for ==

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {

 string password = "thepassword";
  string userPassword = "";
  bool entry;
  cout << "Please enter the password";
  cin >> entry;
  if (password == entry)
   cout << "Acess granted";
  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main()
{
    const std::string expected_password = "autumn_in_new_york" ;
    std::string user_entry ;

    while( expected_password !=  user_entry )
    {
        std::cout << "password? " ;
        std::cin >> user_entry ;
        if(expected_password != user_entry ) std::cout << "invalid password. try again\n" ;
    }

    std::cout << "access granted!\n" ;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

int main() {

 string password = "thepassword";
 string userPassword = "";
 
 bool granted = false;
  
 while ( granted == false )
 {
    cout << "Please enter the password: ";
    cin >> userPassword;
    if ( password == userPassword)
    {
        granted = true;
        cout << "Acess granted";
    }
  }  
}


There you go OP you were pretty close and there's more than one way of skinning a cat.
Last edited on
closed account (48T7M4Gy)
@OP BTW the reason you were getting the == error is because at your line 12 password is a string and entry is a bool. == is expecting the same type both sides.
Last edited on
Thanks a ton for the help, I got the program running now and the info will be useful in the future.
closed account (48T7M4Gy)
:)
Topic archived. No new replies allowed.