1111111111 said:
Write a program that will do the following;
Ask user to enter a FIVE DIGIT integer (from 11111 to 99999)
Separate the number into its individual digits and
Print the digits separated from one another by three spaces each.
I caNNOT figure out the solution!!!!!
There isn't a single solution, there are many possible.
Here's one (which I doubt your instructor would accept
as your own work). Perhaps you can glean some ideas
from it.
#include <algorithm>
#include <iostream>
#include <locale>
#include <string>
bool all_nzdigits(const std::string& s)
{
const static std::locale loc;
std::string::const_iterator it(s.begin());
std::string::const_iterator en(s.end());
while(it != en && std::isdigit(*it, loc) && *it - '0')
++it;
return it == en;
}
bool valid(const std::string& s, std::string::size_type sz)
{
return s.size() == sz && all_nzdigits(s);
}
int main()
{
std::string input;
const unsigned int digits(5);
const unsigned int spaces(3);
do
{
std::cout << "Enter " << digits << "-digit number: ";
std::getline(std::cin, input);
} while(!valid(input, digits));
std::copy(input.begin(), input.end() - !input.empty(),
std:
stream_iterator<char>
(std::cout, std::string(spaces, ' ').c_str()));
if(!input.empty())
std::cout << input[input.size() - 1];
std::cout << '\n';
return 0;
}
-Mike