Newbie Question: using Switch and Printf

  • Follow


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:













7/23/2012 2:06:38 AM


Reply: