#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
string strName;
string strPw;
string strRe;
string strLu;
string strLp;
cout <<"Create a username: ";
cin >> strName;
cout <<"Create a password: ";
cin >> strPw;
while(strPw != strRe)
{
cout <<"Please re-enter your password: ";
cin >> strRe;
cout << endl;
}
cout << "Succesfully created username and password" << endl << endl << endl;
--->while(strPw != strLp) && (strName != strLu)
{
--->cout <<"Please login below"<< endl endl;
cout <<"Username: ";
cin >> strLu;
cout << endl;
cout <<"Password: ";
cin >> strLp;
cout <<endl;
}
cout <<"Thank you for logging in!">>endl;
system("pause");
return 0;
}
Coding help
What is wrong with lines 33 and 35? Also is there an easier way I could write a program like this, I seem to have maybe too many variables.
You nearly have it. First, you dont need to be including stdlib.h, but you do need to include string (no .h).
You need an extra set of brackets, like this:
You are missing a << between the two endl's.
Finally, on this line:
That >> should be a <<
Alan
while(strPw != strLp) && (strName != strLu)
You need an extra set of brackets, like this:
while((strPw != strLp) && (strName != strLu))
cout <<"Please login below"<< endl endl;
You are missing a << between the two endl's.
Finally, on this line:
cout <<"Thank you for logging in!">>endl;
That >> should be a <<
Alan
Hmm, for some reason it will accept wrong answers, is that because the variable changes when I enter different information?... I'm confused, but if you run a working one like this below, it will accept a wrong password on the login part if you have entered the password correctly but the username incorrectly the time before.
#include <iostream>#include <stdlib.h>#include <string>using namespace std;int main(){string strName;string strPw;string strRe;string strLu;string strLp;cout <<"Create a username: ";cin >> strName;cout <<"Create a password: ";cin >> strPw;while(strPw != strRe){cout <<"Please re-enter your password: ";cin >> strRe;cout << endl;}cout << "Succesfully created username and password" << endl << endl << endl;while((strPw != strLp) && (strName != strLu)){cout <<"Please login below"<< endl <<endl;cout <<"Username: ";cin >> strLu;cout << endl;cout <<"Password: ";cin >> strLp;cout <<endl;}cout <<"Thank you for logging in!"<<endl;system("pause");return 0;}
Quote:Original post by KingRage
Hmm, for some reason it will accept wrong answers, is that because the variable changes when I enter different information?... I'm confused, but if you run a working one like this below, it will accept a wrong password on the login part if you have entered the password correctly but the username incorrectly the time before.
*** Source Snippet Removed ***
You never initialized strRe before comparing it in the while loop.
Change the
while((strPw != strLp) && (strName != strLu))
to
while((strPw != strLp) || (strName != strLu))
When you use &&, you continue the loop if both of them are incorrect. If only one is incorrect then it exits the loop. true && false is false, true || false is true, which is what you want.
while((strPw != strLp) && (strName != strLu))
to
while((strPw != strLp) || (strName != strLu))
When you use &&, you continue the loop if both of them are incorrect. If only one is incorrect then it exits the loop. true && false is false, true || false is true, which is what you want.
Quote:Original post by Raloth
Change the
while((strPw != strLp) && (strName != strLu))
to
while((strPw != strLp) || (strName != strLu))
When you use &&, you continue the loop if both of them are incorrect. If only one is incorrect then it exits the loop. true && false is false, true || false is true, which is what you want.
I just figured that out before you posted this, thanks tho :P
Quote:Original post by RhaalQuote:Original post by KingRage
Hmm, for some reason it will accept wrong answers, is that because the variable changes when I enter different information?... I'm confused, but if you run a working one like this below, it will accept a wrong password on the login part if you have entered the password correctly but the username incorrectly the time before.
*** Source Snippet Removed ***
You never initialized strRe before comparing it in the while loop. strPw is assigned a value which you're trying to compare against strRe. strRe can be ANYTHING that was left over in the memory area allocated for it. Most likely while(strPW != strRe) will always be true.
while(strPw != strRe)
Quote:Original post by RhaalQuote:Original post by RhaalQuote:Original post by KingRage
Hmm, for some reason it will accept wrong answers, is that because the variable changes when I enter different information?... I'm confused, but if you run a working one like this below, it will accept a wrong password on the login part if you have entered the password correctly but the username incorrectly the time before.
*** Source Snippet Removed ***
You never initialized strRe before comparing it in the while loop. strPw is assigned a value which you're trying to compare against strRe. strRe can be ANYTHING that was left over in the memory area allocated for it. Most likely while(strPW != strRe) will always be true.
*** Source Snippet Removed ***
No you're wrong. It works now and that's how you do it.
Actually, you are wrong. Everything he said is true.
Your problem may have been the && instead of the || but it is generally accepted as bad practice to use variables in a test before they are properly initialized because you have no way of knowing what they contain. It's undefined behaviour.
Your problem may have been the && instead of the || but it is generally accepted as bad practice to use variables in a test before they are properly initialized because you have no way of knowing what they contain. It's undefined behaviour.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement