Newbie Question: using Switch and Printf

N

~neil~

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~
 
D

Dag Viken

~neil~ said:
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;
}
 
O

osmium

~neil~ said:
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.
 
N

~neil~

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~
 
D

Darrell Grainger

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.
 
G

Gregory Pietsch

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
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top