username and password

this is my code; am a beginner, i want the is program to ask for username (raymond)and password (1234), which has bin done. but i want the instance were if the wrong username is wrongly inputted you shouldnt be getting the next question for a password..... but most importantly with (if the password is correct at the first stage it should terminate) and if not correct should give u another chance for inputation.

would be very grateful

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

int password;
password = 1234;


string username;
string raymond ;

username = raymond;


cout << "Enter username:";
cin >> raymond;

cout <<"\n";

cout << "Enter password:";
cin >> password;

cout <<"\n";



if (password == 1234)
cout << "Access Granted...";


if (password != 1234)
cout << "Access Denied.....Invalid Username/Password:";

return 0;
Try using a loop based on the answer you get
I'm kind of new to C++ too but I tried helping you and this is waht i came up with and what im stuck on.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>

using namespace std; 	//introduces namespace std
int main( void )
{

int password;
password = 1234;


string username;
username = 'Raymond';
string userinput;


do
	{
         
    cout << "Enter username:";
	cin >> userinput;
	
 if (userinput == username);
 {
	cout <<"\n";

	cout << "Enter password:";
	cin >> password;
	
	
    }
 	}
	
	while (userinput == username);


   if (password == 1234)
    cout << "Access Granted...";


    if (password != 1234)
    cout << "Access Denied.....Invalid Username/Password:";
    
    
return 0;
}
Here's something that might work for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string username;
string password;
do {
  std::cout << "username: ";
  getline(std::cin, username);
  if (username == "Raymond") {
    std::cout << "password: ";
    getline(std::cin, password);
    if (password != "1234") {
      std::cout << "invalid password. try again." << std::endl;
    }
  } else {
    std::cout << "invalid username. try again." << std::endl;
  }
} while (password != "1234");


~psault
RE: Psault; i useed your approach and it came through. thanks a million. what i need now is; Instead of the password showing numbers i rather make it write in asterisk (******)
I personally think it would be better to make it where you put in the username and whether it be right or wrong it still asks for the password. That way its more realistic. For example:
Enter Username: bob
Enter Password: 1234
Wrong Username or Password

instead of:
Enter Username: bob
Wrong Username
Topic archived. No new replies allowed.