Hey everyone. First post here. I am new to programming and C++ so please be gentle! I am approaching with respect of your expertise and I truly appreciate any responses you can give to help me down the path.
Currently I am reading Jumping into C++ by Alex Allain, and I am trying to work through some problems. Here is the code I have come up with for a username and password checker. I included my question in the comments. THANK YOU ahead of time for any direction you can provide. I am here to learn!!
// I am brand new to programming and C++. I am working my way
// through Jumping into C++ and I am trying to make sense of Chapters
// 3-5. In developing the username / password problem at the end of
// chapter 4, I have run into this question and it has me stumped.
// I am using Code Blocks for the IDE.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string username;
string password;
string choice;
cout << "Enter your username:\n";
cin >> username;
cout << "Enter your password:\n";
cin >> password;
while (password != "pass" || username != "tom")
// The program behaves correctly only if I use the OR operator.
// Why does the OR operator act like this in this case?
// I thought the correct solution would be the AND operator?
// While password is "fitz" and username is "tom", the expression is
// ( true || false ) which should evaluate to true, correct?
// ... which should then exit the loop and display the "You got it
// right" message. Right? But it doesn't.
// If I use the AND operator like this:
// while (password != "pass" && username != "tom")
// the program jumps out of the loop incorrectly.
// If username is "tom" and password is "fitz" then it exits the loop
// to the "You got it right" message.
// But shouldn't that evaluate to
// (true && false) which is false?
// Shouldn't it go back to the beginning of the while loop?
// If someone might help me understand what I am doing wrong, I would be
// grateful.
// Thank you!!
{
cout << "Uh no... that's not quite right. \n";
cout << "Would you like to try again?\n";
cin >> choice;
if (choice == "no"){
return 0;
}
else {
cout << "Enter your username\n";
cin >> username;
cout << "Enter your password\n";
cin >> password;
}
}
cout << "\nYou got it right. Come on in. \n";
// proceed to the rest of the program
// return is a good way to terminate the program.
return 0;
}
Looks like you misunderstand how while loops work.
They work like: whilecondition is true do actions
1 2
while(condintion)
actions
So if your condition evaluates to true, body of the loop gets executed, if it false, statements after loop actions are executed.
1 2 3
// But shouldn't that evaluate to
// (true && false) which is false?
// Shouldn't it go back to the beginning of the while loop?
↑false means that loop is skipped and the rest of program is executed
1 2 3 4
// While password is "fitz" and username is "tom", the expression is
// ( true || false ) which should evaluate to true, correct?
// ... which should then exit the loop and display the "You got it
// right" message. Right? But it doesn't.
↑True means that loop body is executed and condition is checked again
#include <iostream>
#include <string>
usingnamespace std;
int main () {
string password="<unknown>";
string username="<unknown>";
cout << "Enter Username\n";
cin >> username;
cout << "Enter Password\n";
cin >> password;
// while ((username != "Tom") && (password != "Pass"))
// If username entered is "Tom" and Password is "Fudge", then the result
// is False and True = False and exits the loop. This is not the desired
// result. You want to stay in the loop until the correct username and
// password are entered.
/* You must present a case where the condition is false
to make it exit the loop. Whenever it is True, it will run
the code in the loop.*/
while (username !="Tom" || password != "Pass")
// username Tom password Fudge
// results in False or True = True which executes the loop.
// This is the desired result.
// username Tom password Pass
// results in False or False = False which exits the loop
{
cout << "Not quite.\n";
cout << "Enter Username\n";
cin >> username;
cout << "Enter Password\n";
cin >> password;
}
// end of loop - exit point.
cout << "ID accepted. Welcome.";
return 0;
}