-4

I'm new to programming and so I bought a book to help me learn C++, in the book it asks me to do a practice assignment where I must create a program that allows me to take input of multiple usernames and passwords, match them, and also allow someone who incorrectly enters information to try entering it again. I've wrote the following program in order to do so. If anyone can also shed any light on how to make the program allow for retry of username/password entry without ending the program that would also be greatly appreciated. Thank you!

#include <iostream>
#include <string>
using namespace std;

int main()
{
string username1="Varun";
string username2="Agne";
string username3="Geetha";             
string username4="Mohan"; //Usernames for accounts
string pass1="PassworD";
string pass2="PASSWORD"; //Passwords for accounts
string pass3="password";
string pass4="Password";
string Uattempt; //User input for username
string Pattempt; //User input for password


cout << "Please enter your username.\n";
cin >> Uattempt;
cout << "Please enter your password \n";   //Asks for username and password entry by user
cin >> Pattempt;

if(Uattempt!=username1 || username2 || username3 || username4)
{
    cout << "Access Denied. Incorrect username or password. Please retry.\n";  //If username does not match possible entries, program will ask to retry
}
if(Pattempt !=(pass1||pass2)&&(pass3||pass4)
{
    cout << "Access Denied. Incorrect username or password. Please retry.\n";  //If password does not match possible entries, program will ask to retry
}
if (Uattempt&&Pattempt==username1&&pass1||username2&&pass2||username3&&pass3||username4&&pass4)
{
    cout << "Access Granted. Welcome " + <<Uattempt<< + ".\n";
}
else
{
    return 0;
}
}
  • I'd start with things more basic: `Uattempt!=username1 || username2 || username3 || username4` is not how you compare multiple conditions against a common term. – WhozCraig Dec 19 '16 at 06:44
  • @WhozCraig That's reason I put that duplicate – Danh Dec 19 '16 at 06:45

3 Answers3

0

Use

if(Uattempt != username1 && Uattempt != username2 && Uattempt != username3 && Uattempt != username4)

instead of

if(Uattempt!=username1 || username2 || username3 || username4)
msc
  • 33,420
  • 29
  • 119
  • 214
  • It should be `&&` instead of `||`. Your first line of code would always return `true` – Danh Dec 19 '16 at 06:46
0

std::string does not define || operator.

Change the following:

if(Uattempt!=username1 || username2 || username3 || username4) ...

to:

if (Uattempt!=username1 || Uattempt!=username2 || Uattempt!=username3 || Uattempt!=username4) ...
geipel
  • 395
  • 1
  • 3
  • 9
0

Consider using a map of usernames and passwords if you plan on getting a lot of them:

std::map<string, string> userandpass{
  {"user1", "pass1"},
  {"user2", "pass2"}};

string Uattempt, Pattempt;
std::cout << "Please enter your username.\n";
std::cin >> Uattempt;
std::cout << "Please enter your password \n";   //Asks for username and password entry by user
std::cin >> Pattempt;

auto userIt = userandpass.find(Uattempt);
if (userIt == userandpass.end())
{
  std::cout << "Access Denied.";
  return;
}
string &password = userIt->second;
if (password  != Pattempt)
{
  std::cout << "Access Denied.";
  return;
}
std::cout << "Access Granted.";
Lanting
  • 3,060
  • 12
  • 28