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.

#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;

}

Recommended Answers

All 7 Replies

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...

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

For right now don't worry about it. Verification of input will probably be later in the course.

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.

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.

#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;
 
}

Little typo I noticed in your code:

if (integer [cntr] == '0'&&f=1)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.