No error But wrong Output. Why??

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 27
Reputation: mirfan00 has a little shameless behaviour in the past 
Solved Threads: 2
mirfan00 mirfan00 is offline Offline
Light Poster

No error But wrong Output. Why??

 
-1
  #1
Jun 25th, 2009
I want to compare my password with the stored password but it don't compare correctly. WHY?
Please someone correct it.
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: No error But wrong Output. Why??

 
0
  #2
Jun 25th, 2009
You're a spamming freak who continues to flout the rules about using code tags
http://www.daniweb.com/forums/thread199634.html
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: rcollins is an unknown quantity at this point 
Solved Threads: 3
rcollins rcollins is offline Offline
Newbie Poster

Re: No error But wrong Output. Why??

 
0
  #3
Jun 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 13
Reputation: valinux is an unknown quantity at this point 
Solved Threads: 0
valinux valinux is offline Offline
Newbie Poster

Re: No error But wrong Output. Why??

 
1
  #4
Jun 25th, 2009
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.
"Learn It. Run it. Tinker with it."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,970
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: No error But wrong Output. Why??

 
0
  #5
Jun 25th, 2009
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 13
Reputation: valinux is an unknown quantity at this point 
Solved Threads: 0
valinux valinux is offline Offline
Newbie Poster

Re: No error But wrong Output. Why??

 
0
  #6
Jun 25th, 2009
Thanks tux4life
Last edited by valinux; Jun 25th, 2009 at 3:36 am.
"Learn It. Run it. Tinker with it."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: No error But wrong Output. Why??

 
0
  #7
Jun 25th, 2009
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: No error But wrong Output. Why??

 
0
  #8
Jun 25th, 2009
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?

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.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,970
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: No error But wrong Output. Why??

 
0
  #9
Jun 25th, 2009
Originally Posted by Tom Gunn View Post
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
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 376 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC