Sign in to follow this  
KingRage

Coding help

Recommended Posts

KingRage    100
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
Alan Kemp    772
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
KingRage    100
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
Rhaal    754
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
Raloth    379
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
KingRage    100
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
Rhaal    754
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
KingRage    100
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
Lacutis    301
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
Thunder_Hawk    314
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.


This is a common logical mistake. An alternative (that makes things a bit clearer) is to always save the ! for last (i.e. factor it out).

while (!(strPw == strLp && strName == strLu))

Share this post


Link to post
Share on other sites
Rhaal    754
Quote:
Original post by KingRage
No you're wrong. It works now and that's how you do it.


I was in the middle of explaining a lot of things and even typing up a great pattern for you to use as far as login/password systems. It had minimal variables, unlike your method to create another string just for trying again (which limits to one retry). Yes, I was in the middle of this when I read your reply and I just simply closed notepad.

If you want to get help around here, that's not the way to act. Lets say that I WAS wrong.... thats still not the way to act.

Share this post


Link to post
Share on other sites
KingRage    100
Quote:
Original post by Rhaal
Quote:
Original post by KingRage
No you're wrong. It works now and that's how you do it.


I was in the middle of explaining a lot of things and even typing up a great pattern for you to use as far as login/password systems. It had minimal variables, unlike your method to create another string just for trying again (which limits to one retry). Yes, I was in the middle of this when I read your reply and I just simply closed notepad.

If you want to get help around here, that's not the way to act. Lets say that I WAS wrong.... thats still not the way to act.

SOrry I came around a bit harsh. I just didn't really understand what you meant and it seemed to be working well without it. So sorry man didn't mean to be harsh.

Share this post


Link to post
Share on other sites
KingRage    100
Quote:
Original post by Lacutis
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.

I have no clue what you're talking about lol :S Please explain more.

Share this post


Link to post
Share on other sites
Rhaal    754
Quote:
Original post by KingRage
Quote:
Original post by Lacutis
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.

I have no clue what you're talking about lol :S Please explain more.


If a variable is not assigned a value, how can you compare it? That's what we both mean. Follow me on this one:

int i;

That tells the compiler to allocate enough storage to hold an int.

int i;
i = 5;

That tells the compiler to allocate enough storage to hold an int, and sets the value at that storage address to 5.

The first method simply allocated the storage without giving it a value. If you try to read in "i" then the compiler does it's best to translate whatever is at that memory address (from previous running programs) to an int. It could be any garbage value. Let's just say it interprets it as "856".

while (i != 5)

will most likely always be true since you never assigned 5 to i. In your case it's strRe, not i.

while(strPw != strRe)

but you never said what strRe is. Thus, comparing strRe to anything should only be done after strRe is assigned a value.

Share this post


Link to post
Share on other sites
Matt Apple    200
This piece of code...

cout <<"Create a password: ";

cin >> strPw;

while(strPw != strRe)
{
cout <<"Please re-enter your password: ";

cin >> strRe;

cout << endl;
}




Would be better like this....

do
{
cout << "Create Password: ";
cin >> strPw;
cout << "Please re-enter your password: ";
cin >> strRe;
if (strPw != strRe)
cout << "Passwords don't match! Try Again." << endl;
}while (strPw != strRe);




If you want a loop to execute at least once then 'do while' is better than 'while' if for no other reason than it is self-documenting(because the test is at the bottom of the loop it is obvious that it is always meant to run at least once).

Also by putting the "Create Password" bit of code inside the loop you avoid problems if the user mistypes when initially creating his password. Otherwise the user could mistype the initial password and then end up in an endless loop trying to match their initial mispelling during the "re-enter password" phase.

Also the folks on this thread are right. It is bad form to test the value of a variable before initializing it. It might work in a given instance but ultimately it is hit-or-miss and you'll be teaching yourself bad programming habits.

Share this post


Link to post
Share on other sites
KingRage    100
Quote:
Original post by Rhaal
Quote:
Original post by KingRage
Quote:
Original post by Lacutis
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.

I have no clue what you're talking about lol :S Please explain more.


If a variable is not assigned a value, how can you compare it? That's what we both mean. Follow me on this one:

int i;

That tells the compiler to allocate enough storage to hold an int.

int i;
i = 5;

That tells the compiler to allocate enough storage to hold an int, and sets the value at that storage address to 5.

The first method simply allocated the storage without giving it a value. If you try to read in "i" then the compiler does it's best to translate whatever is at that memory address (from previous running programs) to an int. It could be any garbage value. Let's just say it interprets it as "856".

while (i != 5)

will most likely always be true since you never assigned 5 to i. In your case it's strRe, not i.

while(strPw != strRe)

but you never said what strRe is. Thus, comparing strRe to anything should only be done after strRe is assigned a value.

But I'm asking them for strRe because I want it to match strPw, so basically wut ur syaing is my way works but it would be better to do it the other way because it is better habit?

Share this post


Link to post
Share on other sites
Lacutis    301
Actually I think it's closer to say:
"Your way works in this instance by pure chance. It's very possible that your code could not work when compiled with a different compiler or for a different platform, or depending on whats in the memory allocated to your test variable at the time it's created."

Code like that is how people get random bugs into their program that are extremely hard to debug because they are caused by undefined behaviour. You could run your program 1000 times and have it work all 1000, or the 557th time it may bypass that whole while loop and you would have no clue why.

Some compilers also initialize variables differently depending on if it's being run under a debugger or not, which causes lots of fun problems trying to debug a program.

Suffice it to say, don't rely on undefined behaviour, its undefined for a reason and should not be relied on.

Share this post


Link to post
Share on other sites
Rhaal    754

#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)
//rest of code.....



So when are you asking for strRe in the above code?

Share this post


Link to post
Share on other sites
Rhaal    754
Also, if you explain here exactly what you're trying to design, I'd be more than happy to show a good method based off of your design. That way you can see that you're close, and still see what needed to be changed. Also, how far in C++ knowledge are you? If you don't know functions or pointers I won't use them.

Edit: PM == Not the way!

[Edited by - Rhaal on August 3, 2004 7:47:03 PM]

Share this post


Link to post
Share on other sites
KingRage    100
I'm just learning C++ so I was just trying to think of something that wasn't pointless to practice on. So I decided to use this. I still don't understand what I need to do :S

Share this post


Link to post
Share on other sites
Rhaal    754
Quote:
Original post by KingRage
I'm just learning C++ so I was just trying to think of something that wasn't pointless to practice on. So I decided to use this. I still don't understand what I need to do :S


What you need to do as far as assigning or initializing before comparing? Sorry if I went on a tangent above and wasn't clear, but here's the breakdown. You have this line of code:

while(strPw != strRe)


Look at every single line of code above that line. There are no lines that give strRe a value. How can you know that strPw is notequal to strRe if you don't know what strRe is? I don't know what it is supposed to be or I could help you further. (Because strRe is not a very definitive name). Ponder this:


string string1;
string string2;

string1 = "Hey";

while(string1 != string2) { /* Do stuff */ }


So we know that string1 is "Hey". You tell me. Is string2 equal to string1?

If you said "I don't know because I don't know what string2 is!" then you've got it.

Share this post


Link to post
Share on other sites
Matt Apple    200
Quote:
Original post by Lacutis
"Your way works in this instance by pure chance. It's very possible that your code could not work when compiled with a different compiler or for a different platform, or depending on whats in the memory allocated to your test variable at the time it's created."


Perhaps it would be illustrative if KingRage inserted...

cout << strRe << endl;

into his code somewhere before...

cin >> strRe;

...just to show him what he is comparing strPw to.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this