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.

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

}
5
Contributors
7
Replies
8
Views
10 Years
Discussion Span
Last Post by John A
0

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

0

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

0

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.

0

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.

0
#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;
 
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.