Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #69887 > unrolled thread

A finalized program

Started byAlla _ <modelling.data@gmail.com>
First post2015-09-08 04:13 -0700
Last post2015-09-11 14:19 +0100
Articles 11 on this page of 51 — 11 participants

Back to article view | Back to comp.lang.c


Contents

  A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 04:13 -0700
    Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 04:19 -0700
      Re: A finalized program fir <profesor.fir@gmail.com> - 2015-09-08 04:30 -0700
      Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 14:45 +0100
        Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 08:29 -0700
        Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 08:52 -0700
          Re: A finalized program Martin Shobe <martin.shobe@yahoo.com> - 2015-09-08 13:01 -0500
          Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 21:00 +0100
            Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 13:33 -0700
              Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-09 00:28 +0100
    Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 14:26 +0100
      Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 06:42 -0700
        Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 20:50 +0100
          Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 01:47 -0700
    Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-08 13:41 +0000
      Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 01:31 -0700
        Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-10 10:39 +0000
          Re: A finalized program David Thompson <dave.thompson2@verizon.net> - 2015-09-20 18:35 -0400
      Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 01:33 -0700
      Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 10:16 -0700
        Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-10 20:08 +0200
          Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:05 -0700
            Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:17 +0100
              Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:19 +0100
              Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:28 -0700
              Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:29 -0700
          Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:19 -0700
            Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:23 +0100
              Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:33 -0700
                Re: A finalized program Keith Thompson <kst-u@mib.org> - 2015-09-11 14:11 -0700
                  Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 21:39 +0000
            Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 12:49 +0200
        Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 23:31 +0100
          Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:15 -0700
        Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-10 22:37 +0000
          Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 23:47 +0100
            Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 10:18 +0000
          Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 01:07 -0700
          Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:14 -0700
            Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 10:10 +0000
              Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:19 -0700
    Re: A finalized program Barry Schwarz <schwarzb@dqel.com> - 2015-09-08 15:53 -0700
    Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:27 -0700
      Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 14:05 +0200
    Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:42 -0700
      Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 14:15 +0200
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 05:50 -0700
          Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 15:00 +0200
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 05:50 -0700
      Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-11 13:46 +0100
        Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 14:19 +0100

Page 3 of 3 — ← Prev page 1 2 [3]


#70234

FromAlla _ <modelling.data@gmail.com>
Date2015-09-11 04:19 -0700
Message-ID<a263544d-fb3e-43f9-889d-ce267353997d@googlegroups.com>
In reply to#70229
On Friday, September 11, 2015 at 2:11:11 PM UTC+4, Jens Thoms Toerring wrote:
> Alla _ <modelling.data@gmail.com> wrote:
> > After I have added this test, the part that checks for 
> > letters and other than digits characters does work, but
> > all the rest doesn't. 
> > The program without this test part shows correct results
> > in detecting the card issuer. When test is added, the main
> > part stops working correctly: when I enter a valid card
> > number, the program doesn't give any answer; when I then 
> > enter any letter, it does produce the very last statement
> > "Invalid" or nada :)
> > ex: 
> 
> > Please, enter your card number:  
> > 378282246310005
> > q
> > INVALID
> 
> > another ex:
> > Please, enter your card number:  
> > 378282246310005
> > f
> 
> > Here is the program:
> 
> > #include <stdio.h>
> > #include <string.h>
> 
> > int main(void)
> > {
> >     char card_number[17];
> >     int i, number, digit, sum;
> >     int sum1 = 0, sum2 = 0, sum3 = 0;
> >     
> >     printf ("Please, enter your card number:  \n");
> >     
> >     while (!(scanf ("%16[0-9]", card_number) == 1 && getchar() == '\n'))
> >     {
> >         printf ("This is not a card number; please, try again\n");
> >         while (getchar() != '\n');
> >     }
> >     
> >     if (scanf ("%16[0-9]", card_number) == 1)
> 
> You already have read in the card number in the while loop
> (which tested if it was a proper number at all) but you now
> ask for it again (without even prompting the user), so it's
> not that much of a surprise that it doesn't seem to work
> anymore if you don't enter the number a second time;-) Re-
> move that line and see what happens.
> 
>                         Regards, Jens
> -- 
>   \   Jens Thoms Toerring  ___      jt@toerring.de
>    \__________________________      http://toerring.de

indeed :) Thank you! I see now :) sorry!

[toc] | [prev] | [next] | [standalone]


#69993

FromBarry Schwarz <schwarzb@dqel.com>
Date2015-09-08 15:53 -0700
Message-ID<holuua5epq07fknibhjgq2c8koq8amqrr5@4ax.com>
In reply to#69887
On Tue, 8 Sep 2015 04:13:49 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:

>Hello!
>
>#include <stdio.h>
>#include <string.h>
>
>int main(void)
>{
>    char card_number[17];
>    int i, number, digit, sum;
>    int sum1 = 0, sum2 = 0, sum3 = 0;
>    
>    printf ("Please, enter your card number:  \n");
>    
>    if (scanf ("%s", card_number) == 1)

Your previous version had "16s" to prevent buffer overrun.  This is a
step backwards.

>    {        
>        for (i = (strlen(card_number) - 2); i >= 0; i -= 2)
>        {
>            number = (card_number[i] - '0') * 2;

Since scsnf with %s gives you no control of the input, you might want
to verify that card_number[i] is between '0' and '9' (or equivalently
that number is between 0 and 18).
>            
>            if (number >= 0 && number <= 9)

Under what condition do you expect the first test to ever be false?

>            {
>                sum1 += number;
>            }
>            
>            if (number > 9)
>            {
>                while (number > 0)
>                {
>                    digit = number % 10;
>                    sum2 += digit;
>                    number /= 10;
>                }
>            }

You have tested number for 0 to 9 and 10+.  What should you do if
number is negative (for example, if the user entered a dash)

>        }
>        
>        for (i = (strlen(card_number) - 1); i >= 0; i -= 2)
>        {
>            sum3 += (card_number[i] - '0');
>        }
>        
>        sum = sum1 + sum2 + sum3;
>        
>        if (sum % 10 == 0)
>        {
>            if (strlen(card_number) == 15)
>               printf ("It's a valid AMEX\n");
>            
>            else if (strlen(card_number) == 16)
>               printf ("It's a valid MASTERCARD or VISA\n");
>            
>            else if (strlen(card_number) == 13)
>               printf ("It's a valid VISA\n");
>            
>            else
>                printf ("Card of an unknown card issuer\n");
>        }
>        
>        else
>            printf ("Sorry, the card number failed the test\n");
>    }
>    
>    printf ("\n");
>    return 0;
>}
>
>II version with switch statement at the end (though
>switch statement doesn't completely serve the purpose):
>
>#include <stdio.h>
>#include <string.h>
>
>int main(void)
>{
>    char card_number[17];
>    int i, number, digit, sum;
>    int sum1 = 0, sum2 = 0, sum3 = 0;
>    
>    printf ("Please, enter your card number:  \n");
>    
>    if (scanf ("%s", card_number) == 1)
>    {        
>        for (i = (strlen(card_number) - 2); i >= 0; i -= 2)
>        {
>            number = (card_number[i] - '0') * 2;
>            
>            if (number >= 0 && number <= 9)
>            {
>                sum1 += number;
>            }
>            
>            if (number > 9)
>            {
>                while (number > 0)
>                {
>                    digit = number % 10;
>                    sum2 += digit;
>                    number /= 10;
>                }
>            }
>        }
>        
>        for (i = (strlen(card_number) - 1); i >= 0; i -= 2)
>        {
>            sum3 += (card_number[i] - '0');
>        }
>        
>        sum = sum1 + sum2 + sum3;
>        
>        if (sum % 10 == 0)
>        {
>            switch (strlen(card_number))
>            {
>                case 15:
>                    printf ("It's a valid AMEX\n");
>                    break;
>                case 16:
>                    printf ("It's a valid MASTERCARD or VISA\n");
>                    break;
>                case 13:
>                    printf ("It's a valid VISA\n");
>                    break;
>                default:

What happened to the message about an unknown issuer that the other
version has?

>                    break;
>            }  
>        }
>        
>        else
>            printf ("Sorry, the card number failed the test\n");
>    }
>    
>    printf ("\n");
>    return 0;
>}

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#70235

FromAlla _ <modelling.data@gmail.com>
Date2015-09-11 04:27 -0700
Message-ID<5c4b007c-6413-4168-b27c-c06a5a42f17f@googlegroups.com>
In reply to#69887
still a lot to improve (summation, functions (!),
simplification), but here is a better version,
based on your helpful comments:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char card_number[17];
    int i, number, digit, sum;
    int sum1 = 0, sum2 = 0, sum3 = 0;
    
    printf ("Please, enter your card number:  \n");
    
    while (!(scanf ("%16[0-9]", card_number) == 1 && getchar() == '\n'))
    {
        printf ("This is not a card number; please, try again\n");
        while (getchar() != '\n');
    }
    
    size_t length = strlen(card_number);
        
    for (i = length - 2; i >= 0; i -= 2)
    {
        number = (card_number[i] - '0') * 2;
            
        if (number >= 0 && number <= 9)
        {
           sum1 += number;
        }
            
        if (number > 9)
        {
           while (number > 0)
           {
                 digit = number % 10;
                 sum2 += digit;
                 number /= 10;
           }
        }
    }
        
    for (i = length - 1; i >= 0; i -= 2)
    {
        sum3 += (card_number[i] - '0');
    }
        
    sum = sum1 + sum2 + sum3;
        
    if (sum % 10 == 0)
    {
       if (length == 15 && card_number[0] == '3' && (card_number[1] == '4' || card_number[1] == '7'))
          printf ("AMEX");
            
       else if (length == 16 && card_number[0] == '5')
       {
          if (card_number[1] == '1' || card_number[1] == '2' || card_number[1] == '3' || card_number[1] == '4' || card_number[1] == '5')
             printf ("MASTERCARD");
           
          else
             printf ("INVALID");
        }
            
        else if (length == 16 && card_number[0] == '4')
            printf ("VISA");
            
        else
            printf ("Card of an unknown card issuer\n");
    }
        
    else
        printf ("INVALID");


    printf ("\n");
    return 0;
}

[toc] | [prev] | [next] | [standalone]


#70237

Frombartekltg <bartekltg@gmail.com>
Date2015-09-11 14:05 +0200
Message-ID<msug1h$5i7$1@node1.news.atman.pl>
In reply to#70235
On 11.09.2015 13:27, Alla _ wrote:
> still a lot to improve (summation, functions (!),

!!! ;-)

> simplification), but here is a better version,
> based on your helpful comments:
>
> #include <stdio.h>
> #include <string.h>
>
> int main(void)
> {
>      char card_number[17];
>      int i, number, digit, sum;
>      int sum1 = 0, sum2 = 0, sum3 = 0;
>
>      printf ("Please, enter your card number:  \n");
>
>      while (!(scanf ("%16[0-9]", card_number) == 1 && getchar() == '\n'))
>      {
>          printf ("This is not a card number; please, try again\n");
>          while (getchar() != '\n');
>      }
>
>      size_t length = strlen(card_number);
>
>      for (i = length - 2; i >= 0; i -= 2)
>      {
>          number = (card_number[i] - '0') * 2;
>
>          if (number >= 0 && number <= 9)
>          {
>             sum1 += number;
>          }
>
>          if (number > 9)
>          {
>             while (number > 0)
>             {
>                   digit = number % 10;
>                   sum2 += digit;
>                   number /= 10;
>             }

Is this loop necessary? The number has only two digits.
              sum += number%10 + number/10;
if (number>9) is also unnecessary, it will work with all numbers<100.

>          }
>      }
>
>      for (i = length - 1; i >= 0; i -= 2)
>      {
>          sum3 += (card_number[i] - '0');
>      }

An alternative (not better just different), all in one loop.

Instead of booth loops:

sum =0;
for (int i=1;i<=lenght;i++)
   sum+= digit_value( card_number[length - i], i );

and before main() we need this:

int digit_value( char c, int index ) {
    int res = (c-'0')*(2-index%2 );
    return res%10 + res/10;
} // (2-index%2 ) equals 1 for odd values of index, and 2 for even.


>      if (sum % 10 == 0)
>      {
>         if (length == 15 && card_number[0] == '3' && (card_number[1] == '4' || card_number[1] == '7'))
>            printf ("AMEX");
>
>         else if (length == 16 && card_number[0] == '5')
>         {
>            if (card_number[1] == '1' || card_number[1] == '2' || card_number[1] == '3' || card_number[1] == '4' || card_number[1] == '5')

if (card_number[1] >= '1' && card_number[1] <= '5')

Or, even better, use an integer to keep whole 'first two digits' number.

bartekltg

[toc] | [prev] | [next] | [standalone]


#70236

FromAlla _ <modelling.data@gmail.com>
Date2015-09-11 04:42 -0700
Message-ID<76d2dbff-c516-4e0e-8420-7a7914d7a8f4@googlegroups.com>
In reply to#69887
I have checked the style of this program,
and the result showed an error message:

"expected new line before ";"
 
while (!(scanf ("%16[0-9]", card_number) == 1 && getchar() == '\n'))
    {
        printf ("This is not a card number; please, try again\n");
        while (getchar() != '\n'); /* here, before this ";"*/
    }

What type of new line should be there? This line looks complete, 
especially combined with above lines. I am confused.

[toc] | [prev] | [next] | [standalone]


#70238

Frombartekltg <bartekltg@gmail.com>
Date2015-09-11 14:15 +0200
Message-ID<msugkm$j37$1@node2.news.atman.pl>
In reply to#70236
On 11.09.2015 13:42, Alla _ wrote:
> I have checked the style of this program,
> and the result showed an error message:
>
> "expected new line before ";"

Google never has heard such error message,
0 hits for "expected new line before"
It is very stranege!

>
> while (!(scanf ("%16[0-9]", card_number) == 1 && getchar() == '\n'))
>      {
>          printf ("This is not a card number; please, try again\n");
>          while (getchar() != '\n'); /* here, before this ";"*/
>      }

Looks good. Work on my compiler.


> What type of new line should be there? This line looks complete,

No idea. Need more data;> You should copy exactly
error message. With error message always is a linu
number, where error occurred. According to compiler, real
error often is above it. Mark a line compiler pointed
and add all necessary surroundings.


Information about you compiler also is usefull
(gcc, clang, VS, intel)

> especially combined with above lines. I am confused.

bartekltg



[toc] | [prev] | [next] | [standalone]


#70241

FromAlla _ <modelling.data@gmail.com>
Date2015-09-11 05:50 -0700
Message-ID<1e4d6d38-3e35-4a83-ab21-62a6f1c2546f@googlegroups.com>
In reply to#70238
On Friday, September 11, 2015 at 4:15:29 PM UTC+4, bartekltg wrote:
> On 11.09.2015 13:42, Alla _ wrote:
> > I have checked the style of this program,
> > and the result showed an error message:
> >
> > "expected new line before ";"
> 
> Google never has heard such error message,
> 0 hits for "expected new line before"
> It is very stranege!
> 
> >
> > while (!(scanf ("%16[0-9]", card_number) == 1 && getchar() == '\n'))
> >      {
> >          printf ("This is not a card number; please, try again\n");
> >          while (getchar() != '\n'); /* here, before this ";"*/
> >      }
> 
> Looks good. Work on my compiler.
> 
> 
> > What type of new line should be there? This line looks complete,
> 
> No idea. Need more data;> You should copy exactly
> error message. With error message always is a linu
> number, where error occurred. According to compiler, real
> error often is above it. Mark a line compiler pointed
> and add all necessary surroundings.
> 
> 
> Information about you compiler also is usefull
> (gcc, clang, VS, intel)
> 
> > especially combined with above lines. I am confused.
> 
> bartekltg

You see, it's a CS50 courses special style checker
created to help students learn correct style. But in
this case, I honestly do not see any error in the style;
that's why I decided to ask :)
My compiler is happy with the code, no errors are produced.

[toc] | [prev] | [next] | [standalone]


#70243

Frombartekltg <bartekltg@gmail.com>
Date2015-09-11 15:00 +0200
Message-ID<msuj8l$ljk$1@node2.news.atman.pl>
In reply to#70241
On 11.09.2015 14:50, Alla _ wrote:
> On Friday, September 11, 2015 at 4:15:29 PM UTC+4, bartekltg wrote:
>> On 11.09.2015 13:42, Alla _ wrote:
>>> I have checked the style of this program,
>>> and the result showed an error message:

>
> You see, it's a CS50 courses special style checker
> created to help students learn correct style. But in

Oh. I missed 'style checker' part ;]

bartekltg

[toc] | [prev] | [next] | [standalone]


#70242

FromAlla _ <modelling.data@gmail.com>
Date2015-09-11 05:50 -0700
Message-ID<4dc8bdf0-e79b-481f-8aa7-d9cbcb162e52@googlegroups.com>
In reply to#70238
On Friday, September 11, 2015 at 4:15:29 PM UTC+4, bartekltg wrote:
> On 11.09.2015 13:42, Alla _ wrote:
> > I have checked the style of this program,
> > and the result showed an error message:
> >
> > "expected new line before ";"
> 
> Google never has heard such error message,
> 0 hits for "expected new line before"
> It is very stranege!
> 
> >
> > while (!(scanf ("%16[0-9]", card_number) == 1 && getchar() == '\n'))
> >      {
> >          printf ("This is not a card number; please, try again\n");
> >          while (getchar() != '\n'); /* here, before this ";"*/
> >      }
> 
> Looks good. Work on my compiler.
> 
> 
> > What type of new line should be there? This line looks complete,
> 
> No idea. Need more data;> You should copy exactly
> error message. With error message always is a linu
> number, where error occurred. According to compiler, real
> error often is above it. Mark a line compiler pointed
> and add all necessary surroundings.
> 
> 
> Information about you compiler also is usefull
> (gcc, clang, VS, intel)
> 
> > especially combined with above lines. I am confused.
> 
> bartekltg

You see, it's a CS50 courses special style checker
created to help students learn correct style. But in
this case, I honestly do not see any error in the style;
that's why I decided to ask :)
My compiler is happy with the code, no errors are produced.

[toc] | [prev] | [next] | [standalone]


#70240

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-09-11 13:46 +0100
Message-ID<87lhcdgm3d.fsf@bsb.me.uk>
In reply to#70236
Alla _ <modelling.data@gmail.com> writes:

> I have checked the style of this program,
> and the result showed an error message:
>
> "expected new line before ";"
>  
> while (!(scanf ("%16[0-9]", card_number) == 1 && getchar() == '\n'))
>     {
>         printf ("This is not a card number; please, try again\n");
>         while (getchar() != '\n'); /* here, before this ";"*/
>     }

There are situation where that won't work or will confuse the user, but
I don't think the details matter here.

I've said it before but I feel I want to have another go: don't spend
time on this.  Prompting the user and reading interactive input in a
robust and clear way is extraordinarily difficult and you will never do
it again once you start "real" programming.  I have not written a
program that does this in the last 35 years -- it's a complex skill that
is of no value.

Read the data using " %16[0-9]" (note the space) and tell the user if
the length is wrong.  If you like, add a %c to read the next character
to check if it's '\n'.  That's it.  Check that you've got data of the
right length and process it.  No loops and no trying to clear down the
input buffer.  Make it simple because that will mean you can move on to
more complex program that do interesting things rather than wrestle with
a problem this is so transient.  Write more programs, rather then
spending so long on this one aspect!

Richard has said don't use scanf, but that's bad advice!!  The correct
advice is don't use scanf for complex things -- it *will* drive you mad.
But for simple input tasks in beginners' programs it is ideal.  He
re-wrote your example using fgets and saying how easy it was.  He just
had to write write a dozen or so lines of code to do what %[0-9] does!
And he has to use cast, which he hates!  What's more, fgets has it's own
problems which can confuse users in interactive programs.

In short, there are loads of interesting things to lean that you will
use time and time again (command-line argument processing is one) but
this isn't one of them!

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#70244

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-09-11 14:19 +0100
Message-ID<SxAIx.423866$vg2.379766@fx34.am4>
In reply to#70240
On 11/09/2015 13:46, Ben Bacarisse wrote:

<snip>

> Richard has said don't use scanf, but that's bad advice!!

Dem's fitin' woids! I'll see you in the playground at lunchtime.

> The correct
> advice is don't use scanf for complex things -- it *will* drive you mad.

That's certainly true...

> But for simple input tasks in beginners' programs it is ideal.

...but that isn't.

> He
> re-wrote your example using fgets and saying how easy it was.  He just
> had to write write a dozen or so lines of code to do what %[0-9] does!

Yes, but they were easy lines - easy to write, easy to read, easy to 
understand, and easy to get right. Surely you cannot have overlooked 
just how much grief scanf has caused just in this one thread?

> And he has to use cast, which he hates!

Actually, if I hate anything about casts, it is only that they are 
wrongly used. I could easily have avoided the cast, like this:

   unsigned char uc = d;
   if(isdigit(c))

but that would have been pointless. Casts are useful, provided one 
understands what they are for and when it is appropriate to use them.


> What's more, fgets has it's own
> problems which can confuse users in interactive programs.

Okay, that's worth talking about. What problems are those?

> In short, there are loads of interesting things to lean that you will
> use time and time again (command-line argument processing is one) but
> this isn't one of them!

Actually, I'd more or less agree with your main point that interactive 
programs on the teletype model are rare nowadays, *but* it's still handy 
to know how to do it, partly because there are times when you just want 
to hack out a quickie interactive program without going to all the 
hassle of a full-blown GUI app, but mostly because the skills you learn 
in doing so are transferable to file processing.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c


csiph-web