Please Help me on this Program

1

1111111111

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!!!!!
 
C

Christopher Benson-Manica

1111111111 said:
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!!!!!

If you've attempted the solution, post some code and you'll probably
get some help. If you haven't, I hope you like flipping greasy
burgers.
 
L

Leor Zolman

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

Being just slightly more charitable than Christopher ;-), here's some info
to help get you jump-started:

One way to get the component digits of an int is via use of the integer /
and % operators. Note that the last digit of any number n may be obtained
by the expression n % 10. If you then divide n by 10 and repeat until n is
zero, you'll have produced all the digits of the number in reverse order
(be careful how you deal with an input value of zero).
-leor
 
A

Aggro

Leor said:
One way to get the component digits of an int is via use of the integer /

Or you could just ask for a string, make sure that characters are digits
and there is required amount of them, and then do the rest of it. Might
be easier for a newbie?
 
M

Mike Wahler

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::eek: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
 
L

Leor Zolman

Or you could just ask for a string, make sure that characters are digits
and there is required amount of them, and then do the rest of it. Might
be easier for a newbie?

That would be a bit easier, but the problem description could be
interpreted as requiring the value to start out as an int. What I
described just happens to be what I consider the most "fun" way to do
it--especially when you use recursion to avoid having to collect the digits
up in an array in order to subsequently reverse them.
-leor
 
D

David Harmon

On Tue, 11 May 2004 20:05:36 GMT in comp.lang.c++, Leor Zolman
That would be a bit easier, but the problem description could be
interpreted as requiring the value to start out as an int. What I

Note that an int cannot be assumed to be able to hold 99999.
Would have to be long.
 
O

osmium

David said:
On Tue, 11 May 2004 20:05:36 GMT in comp.lang.c++, Leor Zolman


Note that an int cannot be assumed to be able to hold 99999.
Would have to be long.

The original problem said nothing about int, it spoke of integers. It was
clear to me that the exercise *required* Leor's way of doing it or some
equivalent.
 
K

Karl Heinz Buchegger

David said:
On Tue, 11 May 2004 20:05:36 GMT in comp.lang.c++, Leor Zolman


Note that an int cannot be assumed to be able to hold 99999.
Would have to be long.

:)
If I learned something from this group (and a.c.l.c-c++) then
it is: teachers seldome forumlate assignments with taking
all the details into account and clearifying them.
 
M

Mabden

Karl Heinz Buchegger said:
:)
If I learned something from this group (and a.c.l.c-c++) then
it is: teachers seldome forumlate assignments with taking
all the details into account and clearifying them.

I doubt ANYONE could satisfy the posters of the C / C++ newsgroups as to
details ;-)

I think the problem is simpler than people are making it. I'll bet the
teacher expects the class to read in a string via getch() or cin or
something and have them output each character, then 3 spaces, then the next
char, etc until they reach the \0 at the end. I would doubt the exercise
even needs to use ints (except to limit the string to 5 chars).
 
B

Bill Seurer

Karl said:
:)
If I learned something from this group (and a.c.l.c-c++) then
it is: teachers seldome forumlate assignments with taking
all the details into account and clearifying them.

Actually, that's goodness. When you look at a solution ask them WHY
they did something that way. Their answer can tell you a lot about
their skill or, in the case of students, whether they even did the
assignment or just got a solution frm somewhere.

And in The Real World you NEVER get problems with all the details
spelled out.
 
1

1111111111

Thanks for the input everyone!! Here is what i have so far. If I enter
11111 I get 1 for the output... How do I continue this pattern to make
it look like
1 1 1 1 1 (3spaces apart)?????

#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();

long integer;

cout<<"Please enter a 5 digit integer (from 11111 to 99999)"<<endl;
cin>>integer;

if ((integer>99999) || (integer<11111)) cout<<"The integer you entered
is not between 11111 and 99999"<<endl;

cout<<integer%10 THIS IS WHERE I AM STUCK!!
return 0;
}
 
D

Default User

1111111111 said:
Thanks for the input everyone!! Here is what i have so far. If I enter
11111 I get 1 for the output... How do I continue this pattern to make
it look like
1 1 1 1 1 (3spaces apart)?????

#include <iostream.h>

This should be:

<iostream>

using namespace std; // good enough for homework
#include <conio.h>

This is a non-standard header, you need it.
int main()
{
clrscr();

Why? Leave the damn screen alone.
long integer;

cout<<"Please enter a 5 digit integer (from 11111 to 99999)"<<endl;
cin>>integer;

if ((integer>99999) || (integer<11111)) cout<<"The integer you entered
is not between 11111 and 99999"<<endl;

cout<<integer%10 THIS IS WHERE I AM STUCK!!

Yes, I'd say so. What does the % operator do? What is its effect on an
integer? Did you run some tests to see, or look it up in your book? I'll
give you a hint, it doesn't give the FIRST digit of the number in this
case.



Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top