445,918 Members | 2,246 Online
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

 P: n/a 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 Replies

 P: n/a 1111111111 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

 P: n/a 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 andPrint 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

 P: n/a 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

 P: n/a 1111111111 wrote: I caNNOT figure out the solution!!!!! Post what you have tried so far. Jul 22 '05 #5

 P: n/a "1111111111" 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 #include #include #include 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 (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

 P: n/a On Tue, 11 May 2004 19:18:57 GMT, Aggro 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 digitsand there is required amount of them, and then do the rest of it. Mightbe 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

 P: n/a On Tue, 11 May 2004 20:05:36 GMT in comp.lang.c++, Leor Zolman wrote,That would be a bit easier, but the problem description could beinterpreted 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

 P: n/a David Harmon wrote: On Tue, 11 May 2004 20:05:36 GMT in comp.lang.c++, Leor Zolman wrote,That would be a bit easier, but the problem description could beinterpreted 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

 P: n/a David Harmon wrote: On Tue, 11 May 2004 20:05:36 GMT in comp.lang.c++, Leor Zolman wrote,That would be a bit easier, but the problem description could beinterpreted 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

 P: n/a "Karl Heinz Buchegger" 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 wrote,That would be a bit easier, but the problem description could beinterpreted 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

 P: n/a 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

 P: n/a 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 #include int main() { clrscr(); long integer; cout<<"Please enter a 5 digit integer (from 11111 to 99999)"<>integer; if ((integer>99999) || (integer<11111)) cout<<"The integer you entered is not between 11111 and 99999"<

 P: n/a 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 This should be: using namespace std; // good enough for homework #include 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)"<>integer; if ((integer>99999) || (integer<11111)) cout<<"The integer you entered is not between 11111 and 99999"<

 P: n/a Default User wrote: #include This is a non-standard header, you need it. Errrr, you DON'T need it, of course. Brian Rodenborn Jul 22 '05 #15

### This discussion thread is closed

Replies have been disabled for this discussion.