need help at password and username looping

Sep 11, 2013 at 9:43am
can i have a sample? i dont know how to insert words in a program
sorry im just a beginner
:D

Sep 11, 2013 at 11:41am
You should first read up on <string> http://www.cplusplus.com/reference/string/ and then you will know how to accept user input in the form of words and what tools are available to manipulate and compare the text.
Sep 11, 2013 at 5:54pm
closed account (LzwkoG1T)
For basics, let the user input a password. Presume that you know the password to be " ABC ".
so its a basic if ( password == ABC ), grant access.
Sep 12, 2013 at 3:03am
so in putting variables it should br like...
int password == abc???
Sep 12, 2013 at 4:22am
closed account (LzwkoG1T)
No, that was just a rough example.
what you can do , is delcare a string which will be the password, and compare the user input to that string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string password = "password";

	string pwd;
	cin >> pwd;
	if( pwd == password )
		cout << "Correct" << endl;

	cin.get();
	cin.ignore();
	return 0;

}

Sep 12, 2013 at 4:23am
closed account (LzwkoG1T)
What's happening in the example? ^

We declare the password to be "password"
We ask the user to enter his/her password
if the user-entered password is "password" , grant access.
Sep 12, 2013 at 4:52am
could i have that in a loop program???
Sep 12, 2013 at 4:58am
closed account (LzwkoG1T)
Like for the user to re-enter the password till he gets it right?

something like this:

while( userEnteredPassword != ActualPassword )
{

cin >> userPassword;

}

You can use the above code itself.
1
2
3
4
5
while( pwd != password )
{
  cout << "try again" << endl;
  cin >> pwd;
}
Topic archived. No new replies allowed.