Jump to content
  • Advertisement
Sign in to follow this  
KingRage

Coding help

This topic is 5069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!