| | |
can some one take a look at my program and help me
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 66
Reputation:
Solved Threads: 0
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)
#include "stdafx.h" #include <iostream> #include <ctype.h> #include <fstream> #include <string> using namespace std; int reverseDigit, integer, reverse; int main() { string integer; int cntr; cout <<"Please input an integer: "; cin >> integer; cntr = integer.size(); cntr--; cout <<"reverse = "; if (integer [0] == '-') { cout << "-"; } while (cntr > -1) { if (integer [cntr] == '-') cntr--; else cout << (integer[cntr--]); } cout << endl; return 0; }
Last edited by ~s.o.s~; Feb 19th, 2007 at 1:04 pm. Reason: Added code tags, learn to use them.
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...
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
c Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> #include <ctype.h> #include <fstream> #include <string> using namespace std; int reverseDigit, integer, reverse; int main() { string integer; int cntr,f=1; cout <<"Please input an integer: "; cin >> integer; cntr = integer.size(); cntr--; cout <<"reverse = "; if (integer [0] == '-') { cout << "-"; } while (cntr > -1) { if (integer [cntr] == '0'&&f=1) cntr--; else { cout << (integer[cntr--]); f=0; } } cout << endl; return 0; }
Last edited by WaltP; Feb 21st, 2007 at 3:38 am. Reason: Added Code Tags -- please use them yourself. Instructions in the stickys.
![]() |
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- What's the HARDEST program you've written? (Computer Science)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the C++ Forum
- Previous Thread: string help
- Next Thread: Help with using class as vector
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game generator getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






