Reputation: 31
Regardless of what I type in the if-statement, the code doesn't work like my professor wants it to.
My TA said it is a syntax error but did not provide much more background information for me.
"The syntax you used is incorrect. If you are comparing the initials to abc for example, you would say if(initials == "abc"). Likewise, do the same for each condition."
I don't really get what he means.
If I change my if-statement to add quotation marks, it always denies access. If I leave it as is, it always grants access.
Here is my code:
#include <iostream>
#include <string>
using namespace std;
int main(){
cout<<"Gain entry with your user information and password." <<endl;
cout<<"For security purposes, the information will not be echoed to the screen." <<endl;
cout<<"Please enter your initials: " <<endl;
string initials;
cin>>initials;
cout<<"Please enter your age: " <<endl;
int age;
cin>>age;
cout<<"Please enter your password: " <<endl;
string password;
if ( password == initials,age) {
cout<<"Access granted!" <<endl;
}
else {
cout<<"Access denied!" <<endl;
}
return 0;
}
Here is what it says:
==================== YOUR OUTPUT =====================
0001: Gain~entry~with~your~user~information~and~password.
0002: For~security~purposes,~the~information~will~not~be~echoed~to~the~screen.
0003: Please~enter~your~initials:
0004: Please~enter~your~age:
0005: Please~enter~your~password:
0006: Access~granted!
=================== MISMATCH FOUND ON LINE 0006: ===================
ACTUAL : Access~granted!
EXPECTED: Access~denied!
======================================================
Adjust your program and re-run to test.
Test 2 failed
ccc_0bc38e1b3b_10207@runweb5:~$ ^C
ccc_0bc38e1b3b_10207@runweb5:~$
Upvotes: 1
Views: 113
Reputation: 31
int main(){
cout<<"Gain entry with your user information and password." <<endl;
cout<<"For security purposes, the information will not be echoed to the screen." <<endl;
cout<<"Please enter your initials: " <<endl;
string initials;
cin>>initials;
cout<<"Please enter your age: " <<endl;
int age;
cin>>age;
cout<<"Please enter your password: " <<endl;
string password;
cin>>password;
if ((initials == "abc") && (age == 21) && (password == "qwerty")) {
cout<<"Access granted!" <<endl;
}
else {
cout<<"Access denied!" <<endl;
}
return 0;
}
This is what ended up working. I didn't realize that they wanted literally "abc" to be the initials, I thought it was an example. Once my TA clarified (and I was taught I couldn't use a comma) this is the code that ended up working.
Upvotes: 2
Reputation: 577
The password variable has never been initialized so its actually an empty string since it default constructed. (You was comparing the initials with empty string)
Modify these lines :
cout<<"Please enter your password: " <<endl;
string password;
to be like this so it initializes the password variable.
cout<<"Please enter your password: " <<endl;
string password;
cin >> password;
Edit: You also mentioned that you want to compare the password to the initials + age
// to_string will convert the integer (age) to a string so we can concatenate it with initials
string correct_pass = initials + std::to_string(age);
if ( password == correct_pass) {
cout<<"Access granted!" <<endl;
}else {
cout<<"Access denied!" <<endl;
}
Upvotes: 2
Reputation: 2018
As per your question why your code always return access granted, this is the behavior of a comma (,) operator in C/C++ inside an if statement. Essentially, how you wrote it, it’s two parts: 1. String comparison of password with initials value 2. Evaluation of the age variable
Now, the comma in an “if” statement behaves so that it ignores the first expression, the one before comma, and evaluates the second expression to decide in “if” clause.
As you, probably always type “age” different than zero, the evaluated expression is always true and the “access is granted”. Just for fun, try your original code and enter “age” as zero and it should go to “access denied”.
Upvotes: 2