Well, I decided to learn the C++. In my age(62) is really hard. I try to expand the simple "password" and got stuck. Going bold from scratching my hairs. My problem:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string password;
string name;
cout << "Hello stranger! What is your name: ";
cin>> name;
cout << "Hi " << name << "\n";
cout << "System protected. Please - enter your password: " << "\n";
cin>> password;
if ( password == "xxyy" )
cout << "Access allowed. Hello Bobby. So, You are the guy that wrote this stupid program.";
else
{
if ( password != "xxyy" )
cout << " You out of luck" << "\n";
cout << "Enter your password: " << "\n";
cin>> password;
if (password != "xxyy" )
cout << "Mess up again" << "\n";
cout << "Enter your password: " << "\n";
cin >> password;
if (password != "xxyy" )
cout << "three times mess up. Goodbye." <<"\n";
return 0;
cin.get();
}
}
The program does compile and run, but not the way I want it. If I put the password correct in the first - then I get correct answer, but if I put incorrect first and correct second, I get wrong answer. Please, help me understand of what I am doing wrong.
Thanks.
You have an else statement after checking if it is correct so that means it can only be wrong after. Which means this is unnecessary if ( password != "xxyy" ) on the first line after the else. The reason you never get correct is because you have no way to go back to the correct result. Basically you should give them 3 tries first then output if they were accessed or not.
#include <iostream> //std::cout , std::getline, std::cin, std::endl
#include <string> //std::string
int main()
{
std::string name = "";
std::cout << "Please enter your name: ";
std::getline(std::cin, name); //this allows for first and last or anything their name is really
//it reads the entire line if you use getline after std::cin you will have to ignore the newline left in buffer
unsignedconst attempts = 3; //the user is allowed 3 attempts
std::string const password = "xxyy";
std::string attemptedPassword = "";
bool correctPassword = false;
for(unsigned attempt = 0; attempt < attempts; ++atempt) //give them their 3 tries if necessary
{
std::cout << "Please enter the password: ";
std::getline(std::cin, attemptedPassword); //again reads in entire line
//if you want only letters and numbers you will have to parse this line
//otherwise anything is considered valid
if(attemptedPassword == password)
{
correctPassword = true;
break; //password is valid
//so we need to exit the loop
//this could be avoided if you modify the actual loop condition
//and replace attempt < attempts with:
//attempt < attemps && !correctPassword
//the ! operator flips the boolean so if it was true it is false.
//which means run while attemp is less than attemps and the password is wrong
}
}
if(correctPassword)
{
std::cout << "Access Granted!" << std::endl;
}
else
{
std::cout << "Access Denied!" << std::endl;
}
return 0;
}
Thank you for the correction, but I got more confuse with the bool. Why correctPassword = false and then correctPassword = true. I had no idea that two IF statements are allowed. I will have to read more about. The other question that I have: Could I have different print (message) after each fail attempt or will have to be one only?.
Thanks again. Sorry for the "slow" response. My wi-fi is "acting up".
You could still have different messages. Giblit may have a different suggestion, but one way to do it is with an array of strings.
This line, in the declarations before the for loop, creates an array of three strings and initializes each element of the array to a different message.
Array elements are numbered starting at [0] and going up to the size of the array less one.
So in other words...
message[0] = "Strike 1"
message[1] = "Strike 2"
message[2] = "Strike 3 - You're out!"
(or whatever other text you choose)
The number of the array element corresponds to the value of attempt (0, 1 or 2) in the for loop. So in the last line of the for loop, this line outputs the message corresponding to the current attempt if they haven't input the correct password.
I got more confuse with the bool. Why correctPassword = false and then correctPassword = true.
The reason I initialized it to false was because when the program starts they didn't enter a correct answer (or any at that point) so you don't want to assume they have it correct already. Then when ever they enter it correct you want to change it from false to true telling the program "Hey, I entered the correct password!" If they never answered it correct it will stay false. Then later on if(correctPassword) if statements check if a condition is true or false. True is anything but 0 and false is 0. So when I check I am checking if the correctPassword boolean is true or false and act accordingly.
Well, I will have no need for my comb again. I pull out my last hair, but made it work the way I want to. I took the "long road" and probably it will be perfect example of bad coding.
Thank all for help. Got better understanding of how logic works in C++.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string password = "xxyy";
string apassword;
cout << "Hello stranger. What is your name: " << "\n";
cin >> name;
cout << "Hi" <<name<< "\n";
cout << "System protected. Please - enter your password: " <<"\n";
cin>> apassword;
if (apassword == password)
{
cout << "Access allowed. So, you are the guy that wrote this stupid program." << "\n";
return 0;
cin.get();
}
else
{
cout << "Wrong password. Enter again: " << "\n";
}
cin >> apassword;
if (apassword == password)
{
cout << "Access allowed. So, you are the guy that wrote this stupid program." << "\n";
return 0;
cin.get();
}
else
{
cout << "Wrong password. Enter again: " << "\n";
}
cin >> apassword;
if (apassword == password)
{
cout << "Access allowed. So, you are the guy that wrote this stupid program." << "\n";
return 0;
cin.get();
}
else
{
cout << "One more time. Enter your password: " << "\n";
}
cin >> apassword;
if (apassword == password)
{
cout << "Access allowed. So, you are the guy that wrote this stupid program." << "\n";
return 0;
cin.get();
}
else
{