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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 66
Reputation: bigben09 is an unknown quantity at this point 
Solved Threads: 0
bigben09 bigben09 is offline Offline
Junior Poster in Training

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

 
0
  #1
Feb 19th, 2007
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,623
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #2
Feb 19th, 2007
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...
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 66
Reputation: bigben09 is an unknown quantity at this point 
Solved Threads: 0
bigben09 bigben09 is offline Offline
Junior Poster in Training

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

 
0
  #3
Feb 19th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
0
  #4
Feb 19th, 2007
For right now don't worry about it. Verification of input will probably be later in the course.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 66
Reputation: bigben09 is an unknown quantity at this point 
Solved Threads: 0
bigben09 bigben09 is offline Offline
Junior Poster in Training

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

 
0
  #5
Feb 19th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
0
  #6
Feb 21st, 2007
Originally Posted by bigben09 View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 7
Reputation: goutham_see is an unknown quantity at this point 
Solved Threads: 0
goutham_see goutham_see is offline Offline
Newbie Poster

Re: try this out

 
0
  #7
Feb 21st, 2007
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #8
Feb 21st, 2007
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC