Hello
I've been learning C for over the last couple months ON MY OWN, and
I'm trying to solve this program.
How do you decode a number in each case in a switch from 0 to 9 which
will save the text of each number? EX. case 7: will output "seven" in
the Printf statement. Problem is it's in a loop, each number is
extracted from the user input(scanf statement) like 2057 % 10 will
extract the 7, then 5, then 0, then 2 in each pass of the loop. The
remaining number(s)like 205 after the extraction is put in a "int
variable" called "hold"
The program output looks like this:
Enter a number > 2057
The digits you entered:
Seven,Five,Zero,Two
press any key to continue........
I tried bringing out the text using #define ON "one".This is NOT
homework you guys! I have a book "Learning C Step By Step" by Jean
Paul Corriveau, which is the toughest program I've come accross so
far.I can extract each number in each case for the switch,
however I'm confused on what code to use in each case for the
output(printf)??
The while loop I construsted is infinite, I use CTRL-C to escape.
I suspect the progam uses character array(s) or strings, but I have
limited knowledge of "C"
If your wondering, I was going to take "C" at my college this year
starting in september,there are NO summer classes offered for this
course,it's $1250 bucks for 300 hours of in class instruction.
Any help with the code would be great?
Sincerely
~neil~
|
|
0
|
|
|
|
Reply
|
neilwrites2 (32)
|
7/31/2004 5:58:19 PM |
|
"~neil~" <neilwrites2@hotmail.com> wrote in message
news:494c25bb.0407310958.2e5e8071@posting.google.com...
> Hello
>
> I've been learning C for over the last couple months ON MY OWN, and
> I'm trying to solve this program.
>
> How do you decode a number in each case in a switch from 0 to 9 which
> will save the text of each number? EX. case 7: will output "seven" in
> the Printf statement. Problem is it's in a loop, each number is
> extracted from the user input(scanf statement) like 2057 % 10 will
> extract the 7, then 5, then 0, then 2 in each pass of the loop. The
> remaining number(s)like 205 after the extraction is put in a "int
> variable" called "hold"
>
> The program output looks like this:
>
> Enter a number > 2057
> The digits you entered:
> Seven,Five,Zero,Two
>
> press any key to continue........
>
You do not need switch - try this:
#include <stdio.h>
char* strdigit[] = { "Zero", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine" };
int main(int args, char* argv)
{
int num;
printf("Enter a number > ");
scanf("%d", &num);
printf("The digits you entered:\n");
do
{
printf("%s", strdigit[num%10]);
num /= 10;
if (num != 0)
printf(",");
} while (num != 0);
return 0;
}
>
> I tried bringing out the text using #define ON "one".This is NOT
> homework you guys! I have a book "Learning C Step By Step" by Jean
> Paul Corriveau, which is the toughest program I've come accross so
> far.I can extract each number in each case for the switch,
> however I'm confused on what code to use in each case for the
> output(printf)??
>
> The while loop I construsted is infinite, I use CTRL-C to escape.
> I suspect the progam uses character array(s) or strings, but I have
> limited knowledge of "C"
>
> If your wondering, I was going to take "C" at my college this year
> starting in september,there are NO summer classes offered for this
> course,it's $1250 bucks for 300 hours of in class instruction.
>
> Any help with the code would be great?
>
> Sincerely
> ~neil~
|
|
0
|
|
|
|
Reply
|
dag.viken (21)
|
7/31/2004 6:31:16 PM
|
|
~neil~ writes:
> I've been learning C for over the last couple months ON MY OWN, and
> I'm trying to solve this program.
>
> How do you decode a number in each case in a switch from 0 to 9 which
> will save the text of each number? EX. case 7: will output "seven" in
> the Printf statement. Problem is it's in a loop, each number is
> extracted from the user input(scanf statement) like 2057 % 10 will
> extract the 7, then 5, then 0, then 2 in each pass of the loop. The
> remaining number(s)like 205 after the extraction is put in a "int
> variable" called "hold"
>
> The program output looks like this:
>
> Enter a number > 2057
> The digits you entered:
> Seven,Five,Zero,Two
>
> press any key to continue........
>
>
> I tried bringing out the text using #define ON "one".This is NOT
> homework you guys! I have a book "Learning C Step By Step" by Jean
> Paul Corriveau, which is the toughest program I've come accross so
> far.I can extract each number in each case for the switch,
> however I'm confused on what code to use in each case for the
> output(printf)??
>
> The while loop I construsted is infinite, I use CTRL-C to escape.
> I suspect the progam uses character array(s) or strings, but I have
> limited knowledge of "C"
>
> If your wondering, I was going to take "C" at my college this year
> starting in september,there are NO summer classes offered for this
> course,it's $1250 bucks for 300 hours of in class instruction.
>
> Any help with the code would be great?
I think it is a mistake to use a switch for this unless an instructor
absolutely demand you use one. Consider this:
// precondition: n is in the range 0..9
char* dig_to_english(int n)
{
static char* txt[] = {"zero", "one, "two"};
return txt[n];
}
You could extract the essence of that and put it in a switch, of course.
|
|
0
|
|
|
|
Reply
|
r124c4u1022 (2252)
|
7/31/2004 6:34:18 PM
|
|
"Dag Viken" <dag.viken@earthlink.net> wrote in message news:<UzROc.4083>
You do not need switch - try this:
#include <stdio.h>
char* strdigit[] = { "Zero", "One", "Two", "Three", "Four", "Five",
"Six",
"Seven", "Eight", "Nine" };
int main(int args, char* argv)
{
int num;
printf("Enter a number > ");
scanf("%d", &num);
printf("The digits you entered:\n");
do
{
printf("%s", strdigit[num%10]);
num /= 10;
if (num != 0)
printf(",");
} while (num != 0);
return 0;
}
Dag Viken.........pretty good code!! There must be an error in the
text, cause I was stuck on this problem for awhile, I've solved all
them on my own up until now. I will try your program, the author
doesn't address using char arrays or pointers, just advanced ways of
using while loop in this chapter for teaching.
thanks ok
~neil~
|
|
0
|
|
|
|
Reply
|
neilwrites2 (32)
|
8/1/2004 2:13:45 AM
|
|
Thanx. I am happy if it works for you. Let me know of any problems.
Dag
"~neil~" <neilwrites2@hotmail.com> wrote in message
news:494c25bb.0407311813.7643cf4b@posting.google.com...
> "Dag Viken" <dag.viken@earthlink.net> wrote in message news:<UzROc.4083>
> You do not need switch - try this:
>
> #include <stdio.h>
> char* strdigit[] = { "Zero", "One", "Two", "Three", "Four", "Five",
> "Six",
> "Seven", "Eight", "Nine" };
>
> int main(int args, char* argv)
> {
> int num;
> printf("Enter a number > ");
> scanf("%d", &num);
> printf("The digits you entered:\n");
> do
> {
> printf("%s", strdigit[num%10]);
> num /= 10;
> if (num != 0)
> printf(",");
> } while (num != 0);
>
> return 0;
> }
>
> Dag Viken.........pretty good code!! There must be an error in the
> text, cause I was stuck on this problem for awhile, I've solved all
> them on my own up until now. I will try your program, the author
> doesn't address using char arrays or pointers, just advanced ways of
> using while loop in this chapter for teaching.
>
> thanks ok
> ~neil~
|
|
0
|
|
|
|
Reply
|
dag.viken (21)
|
8/1/2004 2:38:37 AM
|
|
On Sat, 31 Jul 2004, ~neil~ wrote:
> Hello
>
> I've been learning C for over the last couple months ON MY OWN, and
> I'm trying to solve this program.
>
> How do you decode a number in each case in a switch from 0 to 9 which
> will save the text of each number? EX. case 7: will output "seven" in
> the Printf statement. Problem is it's in a loop, each number is
> extracted from the user input(scanf statement) like 2057 % 10 will
> extract the 7, then 5, then 0, then 2 in each pass of the loop. The
> remaining number(s)like 205 after the extraction is put in a "int
> variable" called "hold"
The hard part would be given the number 2057, how do you break it down
into single digits. The trick is that number%10 will give you the single
digit. Thus:
int number = 2057;
int value = number % 10; /* value will be 7 */
int hold = number;
So, how do we change hold from 2057 to 205? That is, how do you drop the
last digit. The trick is called integer arithmetic. If I take hold/10 it
is 205.7 BUT an int cannot hold anything but integers. So it will drop the
0.7 to give you 205.
> The program output looks like this:
>
> Enter a number > 2057
> The digits you entered:
> Seven,Five,Zero,Two
>
> press any key to continue........
>
>
> I tried bringing out the text using #define ON "one".This is NOT
> homework you guys! I have a book "Learning C Step By Step" by Jean
> Paul Corriveau, which is the toughest program I've come accross so
> far.I can extract each number in each case for the switch,
> however I'm confused on what code to use in each case for the
> output(printf)??
>
> The while loop I construsted is infinite, I use CTRL-C to escape.
> I suspect the progam uses character array(s) or strings, but I have
> limited knowledge of "C"
>
> If your wondering, I was going to take "C" at my college this year
> starting in september,there are NO summer classes offered for this
> course,it's $1250 bucks for 300 hours of in class instruction.
>
> Any help with the code would be great?
>
> Sincerely
> ~neil~
>
--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vice.president@whitehouse.gov
|
|
0
|
|
|
|
Reply
|
darrell13 (357)
|
8/2/2004 4:37:27 PM
|
|
neilwrites2@hotmail.com (~neil~) wrote in message news:<494c25bb.0407310958.2e5e8071@posting.google.com>...
> Enter a number > 2057
> The digits you entered:
> Seven,Five,Zero,Two
Not quite. The digits entered are: Two,Zero,Five,Seven.
|
|
0
|
|
|
|
Reply
|
k_amir7 (107)
|
8/3/2004 5:05:41 AM
|
|
neilwrites2@hotmail.com (~neil~) wrote in message news:<494c25bb.0407310958.2e5e8071@posting.google.com>...
> Hello
>
> I've been learning C for over the last couple months ON MY OWN, and
> I'm trying to solve this program.
>
> How do you decode a number in each case in a switch from 0 to 9 which
> will save the text of each number? EX. case 7: will output "seven" in
> the Printf statement. Problem is it's in a loop, each number is
> extracted from the user input(scanf statement) like 2057 % 10 will
> extract the 7, then 5, then 0, then 2 in each pass of the loop. The
> remaining number(s)like 205 after the extraction is put in a "int
> variable" called "hold"
>
> The program output looks like this:
>
> Enter a number > 2057
> The digits you entered:
> Seven,Five,Zero,Two
>
> press any key to continue........
>
>
> I tried bringing out the text using #define ON "one".This is NOT
> homework you guys! I have a book "Learning C Step By Step" by Jean
> Paul Corriveau, which is the toughest program I've come accross so
> far.I can extract each number in each case for the switch,
> however I'm confused on what code to use in each case for the
> output(printf)??
>
> The while loop I construsted is infinite, I use CTRL-C to escape.
> I suspect the progam uses character array(s) or strings, but I have
> limited knowledge of "C"
>
> If your wondering, I was going to take "C" at my college this year
> starting in september,there are NO summer classes offered for this
> course,it's $1250 bucks for 300 hours of in class instruction.
>
> Any help with the code would be great?
>
> Sincerely
> ~neil~
#include <stdio.h>
#define x(z) v z 012U
#define w(z) p(x(z))
#define f(q) fputs(q,stdout)
void p(unsigned v)
{
static char **d={"Zero","One","Two","Three","Four",
"Five","Six","Seven","Eight","Nine"};
if(x(<))f(v[d]);else{w(/);f(",");w(%);}
}
Gregory Pietsch
|
|
0
|
|
|
|
Reply
|
GKP1 (83)
|
8/3/2004 11:08:18 PM
|
|
|
7 Replies
33 Views
(page loaded in 0.122 seconds)
Similiar Articles: Converting integers to Roman Numerals using switchcase and printf ...I read the OP's question as how to use a switch statement but not have output occur ... comp.sys.hp.hpux Converting integers to Roman Numerals using switchcase and printf ... Drawing Text Using OpenGL - comp.graphics.api.openglHello, I'm using GLUT to draw 2D text to screen ... Post Question | Groups ... Just switch to an ortho projection (2D) before drawing ... comp.compilers.lcc - page 17Newbie Question on Struct 4 55 (11/24/2003 9:58:07 PM) hi all, why doesn't this compile ... snippet: unsigned char buffer[] = "$$$$$"; for (int i=0; i<8; i++) printf ... Simple error : method format(String, Object[]) is not applicable ...Hi all,I am a java newbie and I am using eclipse to write this ... This is a string %s", j); > System.out.printf ... Some text processing questions - comp.lang.vhdl Hello ... Missing Charsets - comp.unix.solaris... ascii conversion in gawk, can't it be acheived using printf("%d",c) ? Am I missing ... newbie: awk not ... questions/2008-October/020235.html Charsets ... cut list question ... HP UX 11.00 and VNC - comp.sys.hp.hpuxHi, here am I with another question (in facto, I'am newbie full of questions): I've installed ... problems, and the other was that if you _really_ want to, you _can_ switch ... glGetString problem - comp.graphics.api.openglNewbie Question: QT Opengl example is not working! - comp.graphics ..... to complie and ... fix the problem ... actually, looks like it returns a byte string: printf ... break in switch and while - comp.lang.c++.moderated... inside of the default case of a switch, but the switch ... On our C compiler, with appropriate cout->printf ... Your questions may be answered by <http://womble ... Closing a database - comp.databases.mysqlI am very much a green newbie in the world of MySQL and thus I have what I expect is a simple question but have looked to ... Another way is to switch to another database ... Press any key to continue... - comp.unix.programmer*/ switch_buffering(0); printf("Press any key to continue "); getchar(); /* Restore ... is missing, Windows XP Support, Windows XP technical support questions ... Newbie C programming question - "Press any key to continue"... am trying to develop a simple menu using switch/case ... Newbie C programming question - "Press any key to continue" ... > printf("\t\t DATABASE MANAGEMENT SYSTEM\n\n Newbie question - What's wrong with my switch?Newbie Question: using Switch and Printf ~neil~ C Programming: 7: 08-03-2004 11:08 PM: Newbie Question - switch statement: Greg Scharlemann: Javascript: 1: 12-04-2003 03:28 PM 7/23/2012 2:06:38 AM
|