Coding help

Started by
24 comments, last by Rhaal 19 years, 8 months ago
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.

#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;
}


Advertisement
You nearly have it. First, you dont need to be including stdlib.h, but you do need to include string (no .h).

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
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
Thanks :)
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.
- A momentary maniac with casual delusions.
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.
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
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 Rhaal
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. 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)
- A momentary maniac with casual delusions.
Quote:Original post by Rhaal
Quote:Original post by Rhaal
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. 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.

This topic is closed to new replies.

Advertisement