need help in extremely simple c program 
Author Message
 need help in extremely simple c program

Quote:

> hi all,

> i am a c newbie.  i am learning c from a book called "C programming: a modern
> approach."  i am up to a lesson where it asks you to make a program that says:

> enter a two-digit number:36(manually entered)
> the reversal of this number is:63

> the code that i made for this is:

> /*reverses a 2 digit number*/

> # include <stdio.h>

> main()

int main( void ) /* get used to it */
Quote:
> {

> int first,/*two-digit numberentered*/j/*last digit*/,k/*first digit*/;

> printf("Enter a two digit number:  ");
> scanf("%1d%1d",&first );

scanf("%d", &first );
Quote:

> j = first % 10;

> k = first / 10;

> printf("The reverse of the number you entered is: %1d%1d \n", j, k);

printf("The reverse of the number you entered is: %d%d \n",
j, k);

Quote:

> return 0;

> }

Basically everything was right (well, workable) but your
format specifiers.
However -- DON'T use scanf, it's an inherently dangerous
function (see the FAQ for details). Also, instead of
printing it as two adjacent numbers, why not use the
expression: 10 * (first % 10) + (first / 10) ?

HTH,
--ag
--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account
for more info)

--
"I'd sooner fly another combat mission than ride the Cyclone
again" -- Joseph Heller



Wed, 05 Mar 2003 03:00:00 GMT  
 need help in extremely simple c program
hi all,

i am a c newbie.  i am learning c from a book called "C programming: a modern
approach."  i am up to a lesson where it asks you to make a program that says:

enter a two-digit number:36(manually entered)
the reversal of this number is:63

the code that i made for this is:

/*reverses a 2 digit number*/

# include <stdio.h>

main()
{

int first,/*two-digit numberentered*/j/*last digit*/,k/*first digit*/;

printf("Enter a two digit number:  ");
scanf("%1d%1d",&first );

j = first % 10;

k = first / 10;

printf("The reverse of the number you entered is: %1d%1d \n", j, k);

return 0;

Quote:
}



Thu, 06 Mar 2003 09:37:14 GMT  
 need help in extremely simple c program
sorry i just realized i didnt mention the problem... when i go to run the
program it prompts me to enter the numbers, but it never calculates it right

i thought first % 10
              63 % 10= 3, right?
k= 63 /10
        =6 right?

so it should print 36

thank you very much for your help.
                                        joe



Thu, 06 Mar 2003 09:55:28 GMT  
 need help in extremely simple c program

Quote:
>hi all,

>i am a c newbie.  i am learning c from a book called "C programming: a modern
>approach."  i am up to a lesson where it asks you to make a program that says:

>enter a two-digit number:36(manually entered)
>the reversal of this number is:63

>the code that i made for this is:

>/*reverses a 2 digit number*/

># include <stdio.h>

>main()
>{

>int first,/*two-digit numberentered*/j/*last digit*/,k/*first digit*/;

>printf("Enter a two digit number:  ");
>scanf("%1d%1d",&first );

>j = first % 10;

>k = first / 10;

>printf("The reverse of the number you entered is: %1d%1d \n", j, k);

>return 0;

>}

Joe, I hardly ever use scanf, but it looks like that line should be

scanf("%2d",&first);

since you are reading in only the one integer and then computing the
first and last digit.

Good luck,
Sam



Thu, 06 Mar 2003 10:32:54 GMT  
 need help in extremely simple c program

Also, in case no one else mentions it:

Quote:
>hi all,

>i am a c newbie.  i am learning c from a book called "C programming: a modern
>approach."  i am up to a lesson where it asks you to make a program that says:

>enter a two-digit number:36(manually entered)
>the reversal of this number is:63

>the code that i made for this is:

>/*reverses a 2 digit number*/

># include <stdio.h>

int main() /* the correct way to define it */
Quote:
>main()
>{

>int first,/*two-digit numberentered*/j/*last digit*/,k/*first digit*/;

>printf("Enter a two digit number:  ");
>scanf("%1d%1d",&first );

>j = first % 10;

>k = first / 10;

>printf("The reverse of the number you entered is: %1d%1d \n", j, k);

>return 0;

>}



Thu, 06 Mar 2003 10:34:55 GMT  
 need help in extremely simple c program

Quote:
>However -- DON'T use scanf, it's an inherently dangerous
>function

What should i use instead?


Thu, 06 Mar 2003 03:00:00 GMT  
 need help in extremely simple c program

Quote:

> >However -- DON'T use scanf, it's an inherently dangerous
> >function

> What should i use instead?

fgets


Thu, 06 Mar 2003 03:00:00 GMT  
 need help in extremely simple c program
JoeK282 a crit dans le message

Quote:
>hi all,

>i am a c newbie.  i am learning c from a book called "C programming: a
modern
>approach."  i am up to a lesson where it asks you to make a program that

says:

A hardly recommended book. Good choice.

Quote:
>enter a two-digit number:36(manually entered)
>the reversal of this number is:63

>the code that i made for this is:

>/*reverses a 2 digit number*/

># include <stdio.h>

>main()

Better to stick with he current definition of the language:

int main()
or
int main(void)

Quote:
>{

>int first,/*two-digit numberentered*/j/*last digit*/,k/*first digit*/;

This is ugly. Try that:

int first; /* two-digit numberentered */
int j;     /* last digit */
int k;     /* first digit */

For obvious reasons, it is not recommended to declare more than one variable
per line.

Quote:
>printf("Enter a two digit number:  ");

A '\n' or a fflush(stdout) is missing here.

Quote:
>scanf("%1d%1d",&first );

This is odd. You have 2 formatters and one variable. You really want only
one formatter. I am not sure of the '1' in "%1d" (note:scanf() if *not* a
function for beginners). In a first approach, I would try "%d".

scanf("%d",&first );

Quote:
>j = first % 10;

>k = first / 10;

Good idea.

Quote:
>printf("The reverse of the number you entered is: %1d%1d \n", j, k);

>return 0;

>}

Sounds correct to me, but for safety, I would avoid to use scanf(), and
prefer fgets(), followed by the correct conversion function (strtol() for
example). Read the C-FAQ about it.

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc
C-tips: http://jackklein.home.att.net



Thu, 06 Mar 2003 03:00:00 GMT  
 need help in extremely simple c program

Quote:

> >However -- DON'T use scanf, it's an inherently dangerous
> >function

> What should i use instead?

fgets()

see the faq, there is a nice pointer to it at the bottom of my sig . . .

BTW:
int main() not main()

--

Comp-unix-programmer: FAQ
URL: http://www.erlenstar.demon.co.uk/unix/faq_toc.html
URL: http://www.whitefang.com/unix/faq_toc.html

comp.lang.c FAQ:
 <http://www.eskimo.com/~scs/C-faq/top.html>.



Thu, 06 Mar 2003 03:00:00 GMT  
 need help in extremely simple c program

: JoeK282 a crit dans le message

:>hi all,
:>
:>i am a c newbie.  i am learning c from a book called "C programming: a
: modern
:>approach."  i am up to a lesson where it asks you to make a program that
: says:

: A hardly recommended book. Good choice.

-hs-, I know English is not your first language. So I will ask you this:
do you mean that this book is very much recommended, or do you mean that
this book is hardly a recommended one (ie. there is not much chance of
it being recommended)?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"Show me a good mouser and I'll show you a cat with bad breath."
   - Garfield



Thu, 06 Mar 2003 03:00:00 GMT  
 need help in extremely simple c program

Quote:

>JoeK282 a crit dans le message

>>hi all,

>>i am a c newbie.  i am learning c from a book called "C programming: a
>modern
>>approach."  i am up to a lesson where it asks you to make a program that
>says:

>A hardly recommended book. Good choice.

why, this is not a good book?


Thu, 06 Mar 2003 03:00:00 GMT  
 need help in extremely simple c program
Joona I Palaste a crit dans le message

Quote:
>: A hardly recommended book. Good choice.

>-hs-, I know English is not your first language. So I will ask you this:
>do you mean that this book is very much recommended, or do you mean that
>this book is hardly a recommended one (ie. there is not much chance of
>it being recommended)?

I meant "is very much recommended". Sorry for beeing unclear. Thanks for
improving my English.

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc
C-tips: http://jackklein.home.att.net



Thu, 06 Mar 2003 03:00:00 GMT  
 need help in extremely simple c program
JoeK282 a crit dans le message

Quote:
>>A hardly recommended book. Good choice.

>why, this is not a good book?

I was unclear, sorry. I meant 'well recommended'.

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc
C-tips: http://jackklein.home.att.net



Thu, 06 Mar 2003 03:00:00 GMT  
 need help in extremely simple c program

Quote:


> : A hardly recommended book. Good choice.

> -hs-, I know English is not your first language. So I will ask you this:
> do you mean that this book is very much recommended, or do you mean that
> this book is hardly a recommended one (ie. there is not much chance of
> it being recommended)?

"A hardly recommended book."  is indeed ambiguous, but the poster
resolved the ambiguity with the next sentence "Good choice."


Thu, 06 Mar 2003 03:00:00 GMT  
 need help in extremely simple c program
Quote:

> Joona I Palaste a crit dans le message

> >: A hardly recommended book. Good choice.

> >-hs-, I know English is not your first language. So I will ask you this:
> >do you mean that this book is very much recommended, or do you mean that
> >this book is hardly a recommended one (ie. there is not much chance of
> >it being recommended)?

> I meant "is very much recommended". Sorry for beeing unclear. Thanks for
> improving my English.

> --
> -hs-    Tabs out, spaces in.
> CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
> ISO-C Library: http://www.dinkum.com/htm_cl
> FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc
> C-tips: http://jackklein.home.att.net

Did you mean to say "heartily recommended"?
[hey, your English exceeds my virtually non-existant French
by several orders of magnitude]

--ag

--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account
for more info)

--
"I'd sooner fly another combat mission than ride the Cyclone
again" -- Joseph Heller



Thu, 06 Mar 2003 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. HELP:Extremely Simple problem -- Thanks!!

2. @@@@@@ EXTREMELY URGENT / HELP NEEDED!!!

3. Need some help in writing a simple C program

4. Extremely short -extremely complicated

5. Need some help in writing a simple C program

6. Need Help on simple ISAM like program

7. need help debugging simple c program

8. Need some help with tweeking a simple program

9. Help on a simple calculator program which needs Trigo, Arithmethic, log functions..[Apologies for repeat mail]

10. Need help with simple C Program

11. Simple BC+31 program - help needed

12. !Need help with simple C program

 

 
Powered by phpBB® Forum Software