473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Please Help me on this Program

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!!!!!
Jul 22 '05 #1
14 2473
1111111111 <eh***@hotmail.com> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #2
On 11 May 2004 11:55:53 -0700, eh***@hotmail.com (1111111111) wrote:
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

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
Leor Zolman wrote:
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?
Jul 22 '05 #4
1111111111 wrote:
I caNNOT figure out the solution!!!!!


Post what you have tried so far.
Jul 22 '05 #5
"1111111111" <eh***@hotmail.com> wrote in message
news:3f**************************@posting.google.c om...
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::ostream_iterator<char>
(std::cout, std::string(spaces, ' ').c_str()));

if(!input.empty())
std::cout << input[input.size() - 1];

std::cout << '\n';

return 0;
}
-Mike
Jul 22 '05 #6
On Tue, 11 May 2004 19:18:57 GMT, Aggro <sp**********@yahoo.com> wrote:
Leor Zolman wrote:
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?


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
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #7
On Tue, 11 May 2004 20:05:36 GMT in comp.lang.c++, Leor Zolman
<le**@bdsoft.com> wrote,
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.

Jul 22 '05 #8
David Harmon wrote:
On Tue, 11 May 2004 20:05:36 GMT in comp.lang.c++, Leor Zolman
<le**@bdsoft.com> wrote,
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.


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.
Jul 22 '05 #9
David Harmon wrote:

On Tue, 11 May 2004 20:05:36 GMT in comp.lang.c++, Leor Zolman
<le**@bdsoft.com> wrote,
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.


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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
David Harmon wrote:

On Tue, 11 May 2004 20:05:36 GMT in comp.lang.c++, Leor Zolman
<le**@bdsoft.com> wrote,
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.


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

--
Mabden
Jul 22 '05 #11
Karl Heinz Buchegger wrote:
:-)
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.
Jul 22 '05 #12
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;
}
Jul 22 '05 #13
1111111111 wrote:

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
Jul 22 '05 #14
Default User wrote:
#include <conio.h>


This is a non-standard header, you need it.

Errrr, you DON'T need it, of course.


Brian Rodenborn
Jul 22 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Steve Clay | last post by:
I have a small C program for a college course. It is meant to encrypt and decrypt lower case letters and leave spaces as spaces. I can't get it to run properly as I think I have a problem in the...
2
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
3
by: promiscuoustx | last post by:
I am trying to get my program to compile, but it will not complete. At line 79 it states, cannot convert 'float()()' to 'float' in assignment. Here is my code. #include <iostream> #include...
5
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
5
by: mohammaditraders | last post by:
Question # 1 Write a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions...
3
by: madhu542 | last post by:
Hi, My task is to design one program in c-language, and my collegue's is another program to design and do some other functionality. I need to execute my collegue's program with out directly...
5
by: tabani | last post by:
I wrote the program and its not giving me correct answer can any one help me with that please and specify my mistake please it will be highly appreciable... The error arrives from option 'a' it asks...
2
by: clouddragon | last post by:
Hi, i am in desperate need for any help regarding one of my assignments. I am to write a python program that lists the numbers that are composite from 1 to n(input) and write it to an external...
6
by: fido19 | last post by:
Once upon a time, there lived a chimpanzee called Luycha Bandor (aka Playboy Chimp). Luycha was unhappily married to Bunty Mona, a short but cute little lady chimp. Luycha was tall and handsome –...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.