I am trying to make a simple program that asks the user for their name and ID. If their name matches one of the names in the array spectreNames, then they will be prompted for their ID. If their ID matches the one for their name, they have access (bool accessGranted = true).
My issue is with asking the user for the name. I tried to make it so they would be continuously prompted for their name until they enter one of the names in spectreNames, and no matter how many tries it took them, once they got the right name they would be asked for their ID. However, this is what is happening:
1. If they enter the right name the first time, the code continues normally.
2. If they make a mistake but then get the name right, the code stops. It does not go on to ask for their ID or do anything else.
This same phenomena occurs with the ID number. If:
1. User enters a correct name the first time
2. Enters an incorrect ID number
3. Then enters a correct ID number
the program stops instead of printing the final message.
Any help is greatly appreciated!
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream>
// SPECTRE TERMINAL V 2.0
using namespace std;
int main()
{
// Starter variables.
string spectreNames[3] = {"Erin Shepard" , "Talva Minker" , "Sapar Uvan"};
int spectreID[3] = {123, 456, 789}; // Made simple IDs for testing purposes.
string userInputName = "default";
int userInputID = 0;
string spectreNameApproved;
bool accessGranted = false;
// Initial message
cout << "Welcome to the Spectre Terminal." << endl;
cout << "This terminal is property of the Citadel Council, and it is against the law ";
cout << "to tamper with, disable, or to attempt to use this terminal without spectre status." << endl;
cout << "Please enter your name, Spectre: ";
getline(cin, userInputName);
if(userInputName == spectreNames[0] ^ userInputName == spectreNames[1] ^ userInputName == spectreNames[2]) {
// the following three lines specify WHICH spectre the user is logging in as by defining the variable spectreNameApproved.
if(userInputName == spectreNames[0]) spectreNameApproved = spectreNames[0];
if(userInputName == spectreNames[1]) spectreNameApproved = spectreNames[1];
if(userInputName == spectreNames[2]) spectreNameApproved = spectreNames[2];
/* In this code block, you will ask for the user's ID number and then
check it with the other ID numbers. Depending on which spectreNameApproved
the user has obtained will determine the ID they require. */
cout << "Please enter your Spectre Identification Number: ";
cin >> userInputID;
if(userInputID == spectreID[0] or userInputID == spectreID[1] or userInputID == spectreID[2]) {
// Makes sure the right ID is matched with the approved spectre name (spectreNameApproved).
if(spectreNameApproved == spectreNames[0] and userInputID == spectreID[0]) accessGranted = true;
if(spectreNameApproved == spectreNames[1] and userInputID == spectreID[1]) accessGranted = true;
if(spectreNameApproved == spectreNames[2] and userInputID == spectreID[2]) accessGranted = true;
// Let the user know they have access
cout << "Welcome, " << spectreNameApproved << ". You now have access to this terminal for the next thirty minutes." << endl;
cout << "C==I==T==A==D==E==L====C==O==U==N==C==I==L====S==P==E==C==T==R==E==S" << endl;
}
else { // if the ID does not match the name.
while(userInputID != spectreID[0] and userInputID != spectreID[1] and userInputID != spectreID[2]) {
cout << "Incorrect ID. Please try again. ";
cin >> userInputID;
}
}
}
else { // If the name entered is not a spectre's name.
while(userInputName != spectreNames[0] and userInputName != spectreNames[1] and userInputName != spectreNames[2]) {
cout << "Incorrect name. Please try again: ";
getline(cin, userInputName);
}
}
return 0;
}
|