943,618 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 540
  • C++ RSS
Jun 25th, 2009
-1

No error But wrong Output. Why??

Expand Post »
I want to compare my password with the stored password but it don't compare correctly. WHY?
Please someone correct it.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string.h>
  3. #include <conio.h>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. string my = "programming ";
  10. string pass = "";
  11. int s;
  12. int a;
  13.  
  14. cout << "Enter password: ";
  15.  
  16. do {
  17.  
  18. // backspace not allowed
  19. a = _getch();
  20.  
  21. pass += char(a);
  22.  
  23. cout << "*";
  24.  
  25. }while( a != 13 );
  26.  
  27. cout << endl << "Password is: " << pass << endl;
  28. cerr << " \n My: " << my << " \n ";
  29. //s = strlen(pass);
  30. //cerr << " Size: " << s;
  31. //if ( strcmp ( pass , my ) == 0 ) {
  32. if ( pass == my ) {
  33. cerr << "\n\n \t Password Comfirmed";
  34. } else {
  35. cerr << "\n\n \t Not Confirmed";
  36. }
  37. system("pause");
  38.  
  39. return 0;
  40.  
  41. }
Last edited by Ancient Dragon; Jun 25th, 2009 at 7:10 am. Reason: add code tags
Similar Threads
Reputation Points: -9
Solved Threads: 2
Light Poster
mirfan00 is offline Offline
29 posts
since May 2009
Jun 25th, 2009
0

Re: No error But wrong Output. Why??

You're a spamming freak who continues to flout the rules about using code tags
http://www.daniweb.com/forums/thread199634.html
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 25th, 2009
0

Re: No error But wrong Output. Why??

I would try using the compare function of the string class. I believe that your test pass == my is testing that your string objects are the same objects rather than whether their contents are the same. Also make sure you are using code tags when you post.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
rcollins is offline Offline
17 posts
since May 2009
Jun 25th, 2009
1

Re: No error But wrong Output. Why??

Try looking at your program again. Look at it closely when you type your password. I dont mean your typing it wrong, but when you press enter. Another star is added.. ex:
password: **** (hello)
password: hello ***** is wrong.. I'm sorry I cant explain it so well. You can't use enter just like you can't use backspace. I believe.
Sorry if that is wrong.
Last edited by valinux; Jun 25th, 2009 at 2:28 am.
Reputation Points: 23
Solved Threads: 0
Newbie Poster
valinux is offline Offline
13 posts
since Jun 2009
Jun 25th, 2009
0

Re: No error But wrong Output. Why??

This program is real rubbish, old style headers, conio, half of the code is commented out, system("pause") (don't use it), etc...
My recommendation is: migrate to standard C++ first, they really should make it a forum rule...

BTW, Does it really hurt you to use code tags:
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used
6) Even on the background of the box you actually typed your message in

(as already discovered by valinux):
If you still really want to fix your code, then you should add a check before adding the character to the variable, otherwise there will be an ENTER in the pass-string where you compare the real password to, always resulting in a Non Confirmed message, a simple check might look like this:
if( a != 13 ) pass += a;
Last edited by tux4life; Jun 25th, 2009 at 2:55 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jun 25th, 2009
0

Re: No error But wrong Output. Why??

Thanks tux4life
Last edited by valinux; Jun 25th, 2009 at 3:36 am.
Reputation Points: 23
Solved Threads: 0
Newbie Poster
valinux is offline Offline
13 posts
since Jun 2009
Jun 25th, 2009
0

Re: No error But wrong Output. Why??

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 25th, 2009
0

Re: No error But wrong Output. Why??

tux4life, I challenge you to write the same program with the same features and restrictions and show me how it's so much better than mirfan00's that he deserved all of that harassment. You're quick to mouth off, but can you back it up with your own perfection?

Quote ...
This program is real rubbish, old style headers, conio, half of the code is commented out, system("pause")
The only old style header is <string.h>, and even though it's officially deprecated, it's not going away any time soon because then compatibility with C would go out the window.

There's no way to replace typed characters with a star using standard code. To do that you have to use something that isn't portable, and conio is as good a choice as any for a toy program.

Only 3 lines out of over 40 are commented out. I didn't get good grades in grade school math classes, but I'm pretty sure 3 isn't even close to half of 40.

You forgot to say that <string> should be included because the code uses the standard C++ string class. And you forgot to say that '\r' is a portable replacement for 13. For all the complaining about old style headers, you forgot to say that the only code using them is commented out so both can be removed. I see you complaining about the same things over and over, and ignoring other things that are in the same league or worse. It kind of makes me think that you don't really know what you're talking about and just copy people who do.
Last edited by Tom Gunn; Jun 25th, 2009 at 10:45 am.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jun 25th, 2009
0

Re: No error But wrong Output. Why??

Click to Expand / Collapse  Quote originally posted by Tom Gunn ...
tux4life, I challenge you to write the same program with the same features and restrictions and show me how it's so much better than mirfan00's that he deserved all of that harassment. You're quick to mouth off, but can you back it up with your own perfection?


The only old style header is <string.h>, and even though it's officially deprecated, it's not going away any time soon because then compatibility with C would go out the window.

There's no way to replace typed characters with a star using standard code. To do that you have to use something that isn't portable, and conio is as good a choice as any for a toy program.

Only 3 lines out of over 40 are commented out. I didn't get good grades in grade school math classes, but I'm pretty sure 3 isn't even close to half of 40.

You forgot to say that <string> should be included because the code uses the standard C++ string class. And you forgot to say that '\r' is a portable replacement for 13. For all the complaining about old style headers, you forgot to say that the only code using them is commented out so both can be removed. I see you complaining about the same things over and over, and ignoring other things that are in the same league or worse. It kind of makes me think that you don't really know what you're talking about and just copy people who do.
Well, when I replied his code wasn't surrounded by code tags, well, you can see the result in my post
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Can I return Vector from Virtual Function
Next Thread in C++ Forum Timeline: Pointers to functions?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC