This program is supposed to determine if a number input is a palindrome or not.
The problem I'm having is that I am supposed to add leading zeros in front of an input that has less than 5 digits as explained below:
Numbers with fewer than 5 digits are automtically "padded" with leading zeros. So if you enter 0, that gets treated as 00000, and it's a palindrome. But 1 gets treated as 00001, and it's not. 100 is, because that's 00100.
I've included pseudo code for this part but for some reason my brain is blanking on how to turn this into an actual statement. If you can provide me with an example or even just a hint, I'd really appreciate it. Thanks
1 2 3 4
//pseudo code for adding leading zeros
//determine how many characters have been input into array (length)
//create new array that adds (5-length) zeros in front of the input (this is the part I don't understand how to do)
//send the new array into the palindrome checker portion of the program.
#include <cstring>
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
//Declare variables
char num[4];
bool palindrome=true;
do
{
cout << "Enter a five or less digit number or enter 'Q' to quit." << endl;
cin >> num;
if (num[0] == 'Q' || num[0] == 'q')
break;
//if user does not quit
int length = strlen(num);
int(length/2);
if (length > 0)
{
for(int i = 0; i < length ;i++)
{
if(num[i]!= num[length-1-i])
palindrome=false;
}
}
if(palindrome==true)
{
cout << "The number " << num << " is a palindrome" << endl;
}
else
{
cout << "The number " << num << " is not a palindrome" << endl;
}
}while (/*need condition?*/);
}
Also, note that the current code is not complete. I will be finishing it at a later time. Thanks again!
For one your array holds only four characters to make it hold 5 declare it as suchchar num[5];.
To address your problem though simply have a separate string of 5 zeroes that you replace with the input char t[5] = {'0', '0', '0', '0', '0'};. Then simply replace the characters starting from 5 - the length of the input. I believe that should work.
#include <iostream>
#include <string>
int main()
{
std::cout << "enter a non-negative integer with five or fewer digits: " ;
int number ;
if( std::cin >> number ) // if the user entered an integer
{
if( number < 0 ) std::cout << "you entered a negative number\n" ;
elseif( number > 99999 ) std::cout << "that number has more than five digits\n" ;
else // non-negative integer with five or fewer digits
{
// http://en.cppreference.com/w/cpp/string/basic_string/to_string
std::string str_number = std::to_string(number) ; // convert the number to a string
// add leading zeroes till there are five digits
while( str_number.size() < 5 ) str_number = '0' + str_number ;
// TODO: check if the str_number is a palindrome
// TODO: print result
}
}
else std::cout << "you did not enter an integer\n" ;
}
// http://en.cppreference.com/w/cpp/string/basic_string/to_string
std::string str_number = std::to_string(number) ; // convert the number to a string
// add leading zeroes till there are five digits
while( str_number.size() < 5 ) str_number = '0' + str_number ;
with this
1 2 3 4 5 6 7 8
// convert the number to a string with leading zeroes added till there are five digits
std::ostringstream stm ; // a stream that writes into a string
// right justified (default), in a field of width 5, padded with '0'
stm << std::setfill('0') << std::setw(5) << number ;
const std::string str_number = stm.str() ; // get a copy of the string that was written into