Question: Hi, I have a proble with my C++ program. The program should display the leading zeros but if I put 005, it only displays the
Hi, I have a proble with my C++ program. The program should display the leading zeros but if I put 005, it only displays the reverse number 5 not 500. It will appreciated the solution. Thanks

This is the code:
#include
using std::cout;
using std::cin; //Allows the program to perform input and output
using std::endl;
/ecessary to convert character 'buffer' to integer 'num' with 'atoi'
#include
//function templates
int reverseNum(int);
int main()
{
//print my name and this assignment's title
cout
cout
cout
cout
cout
cout
//Program starts here:
//variables to store the input in the buffer and integers necessary to store the number from the user
//and display leading zeros if the user puts a digit with zeros
char buf[100];
int num, num1, len, temp, len1, ret;
while (true)
{
//prompt user for input
//the user puts a series of numbers less than 6 digits to do the reverse
//and they are stored in the buffer, to later convert it to integer and sotre the numbers
//in the variable called 'num'
cout
cin >> buf; num = atoi(buf);
//calculate length
len = 0;
while (buf[len] != '\0') //determines where in the 'buffer' variable the null character determinate
len++; //the end of C-style strings and calculate the length of the digits gathered from the user
if (len 6) //if length is
{
cout
continue; //if the length of input is more than 6 digits, the program displays
} //"Length of input should be 1 to 6 digits" and the program continues
//quit program if user entered a q
if (buf[0] == 'q' || buf[0] == 'Q')
break;
cout
ret = reverseNum(num); //ret gets the return type from 'reverseNum' function
temp = ret; //the temp variable gets the information stored in the 'ret variable'
len1 = 0; //'len1' variable stores 0 to display correctly the leading zeros
//with the loop from below
while (temp > 0) //find the length of the reversed number
{
temp = temp / 10; //formula necessary to display the leading zeros if the user puts
len1++; //a series of digits with leading zeros or has zero on the last character
}
//the program display the reversed number with leading zeros
cout
for (int i = 0; i
cout
//the last character
//display the reverse number after the leading zeros are displayed in the same line
//to show the leading zeros, otherwise it will display only the leading zeros
cout
}
}
//function integer 'reverseNum' necessary to do the reverse of the function
//and necessary to display the zeros of the user entered numbers that are
//leading with zeros
int reverseNum(int number)
{
int reversedNum = 0;
int reminder;
int zeros = 0;
bool firsttime = true;
while (number > 0)
{
reminder = number % 10;
//If reminder is 0 and the first non zero number is not yet encountered
if (reminder == 0 && firsttime)
{
zeros++;
}
//If first non zero digit is encounreted, zeros encountered later shouldn't be counted
else
{
firsttime = false;
}
reversedNum = reversedNum * 10 + reminder; //formula to display the reverse number
number /= 10; //the number gathered from the user is
//mutiplied by 10 and added with the reminder.
} //later the 'number' is divided by ten and added
//everything to 'reversedNum'
return reversedNum; //the 'reversedNum' is returned to returned to the
//main function with the calculations properly calculated
//and stored
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
