943,678 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1376
  • C++ RSS
Feb 19th, 2007
0

can some one take a look at my program and help me

Expand Post »
Hey i need with this program it works, but i cant figure out how to make it not print out zeros when it is reversed. for example when the user types in 123000 it should print out 321. but my program prints out 000321 what can i do to make it print out 321.
C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctype.h>
  4. #include <fstream>
  5. #include <string>
  6. using namespace std;
  7. int reverseDigit, integer, reverse;
  8.  
  9. int main()
  10. {
  11.  
  12. string integer;
  13. int cntr;
  14. cout <<"Please input an integer: ";
  15. cin >> integer;
  16.  
  17. cntr = integer.size();
  18. cntr--;
  19. cout <<"reverse = ";
  20. if (integer [0] == '-')
  21. {
  22. cout << "-";
  23. }
  24. while (cntr > -1)
  25. {
  26. if (integer [cntr] == '-')
  27. cntr--;
  28. else
  29. cout << (integer[cntr--]);
  30. }
  31. cout << endl;
  32. return 0;
  33.  
  34. }
Last edited by ~s.o.s~; Feb 19th, 2007 at 1:04 pm. Reason: Added code tags, learn to use them.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bigben09 is offline Offline
66 posts
since Feb 2007
Feb 19th, 2007
0

Re: can some one take a look at my program and help me

Don't use global variables. Its a bad programming practice considering the job can be very well done using local ones.

As far as your method is considered its a bit unclean but still if you want to strip off the leading zeroes check one more condition which keeps skipping zero as long as it doesn't encounter a non zero valid digit.

Btw, I see no validation considering that the user is allowed to enter even alphabets and punctuations and get away with them...
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Feb 19th, 2007
0

Re: can some one take a look at my program and help me

how would i make it only eccept #'s and were would i put it in the program. also this is my first computer class so i am new to this so im not sure what u are talking about
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bigben09 is offline Offline
66 posts
since Feb 2007
Feb 19th, 2007
0

Re: can some one take a look at my program and help me

For right now don't worry about it. Verification of input will probably be later in the course.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Feb 19th, 2007
0

Re: can some one take a look at my program and help me

ok but does anyone know how i would make it not pass a zero if the first number is zero after it been reverset. for example 1230 shoul be printed out as 321 and not 0321.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bigben09 is offline Offline
66 posts
since Feb 2007
Feb 21st, 2007
0

Re: can some one take a look at my program and help me

Click to Expand / Collapse  Quote originally posted by bigben09 ...
ok but does anyone know how i would make it not pass a zero if the first number is zero after it been reverset. for example 1230 shoul be printed out as 321 and not 0321.
Sure. When you start outputting the values, check for a 0. If there is one, don't output it. Keep testing until you don't find a zero. Once you output that first non-zero, stop testing and just output to the end.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Feb 21st, 2007
0

Re: try this out

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctype.h>
  4. #include <fstream>
  5. #include <string>
  6. using namespace std;
  7. int reverseDigit, integer, reverse;
  8.  
  9. int main()
  10. {
  11.  
  12. string integer;
  13. int cntr,f=1;
  14. cout <<"Please input an integer: ";
  15. cin >> integer;
  16.  
  17. cntr = integer.size();
  18. cntr--;
  19. cout <<"reverse = ";
  20. if (integer [0] == '-')
  21. {
  22. cout << "-";
  23. }
  24. while (cntr > -1)
  25. {
  26. if (integer [cntr] == '0'&&f=1)
  27. cntr--;
  28. else
  29. {
  30. cout << (integer[cntr--]);
  31. f=0;
  32. }
  33. }
  34. cout << endl;
  35. return 0;
  36.  
  37. }
Last edited by WaltP; Feb 21st, 2007 at 3:38 am. Reason: Added Code Tags -- please use them yourself. Instructions in the stickys.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
goutham_see is offline Offline
7 posts
since Feb 2007
Feb 21st, 2007
0

Re: can some one take a look at my program and help me

Little typo I noticed in your code:
if (integer [cntr] == '0'&&f=1)
Last edited by John A; Feb 21st, 2007 at 8:19 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

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: string help
Next Thread in C++ Forum Timeline: Help with using class as vector





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


Follow us on Twitter


© 2011 DaniWeb® LLC