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


Groups > comp.lang.c > #70041

Re: Improving the program, issues with logical operators, incorrect output

From bartekltg <bartekltg@gmail.com>
Newsgroups comp.lang.c
Subject Re: Improving the program, issues with logical operators, incorrect output
Date 2015-09-09 15:42 +0200
Organization ATMAN - ATM S.A.
Message-ID <mspcvi$rcq$1@node1.news.atman.pl> (permalink)
References <cd2ea66c-053e-4ba5-8704-211f2dd5609b@googlegroups.com>

Show all headers | View raw


On 08.09.2015 15:47, Alla _ wrote:
> Hello!
>
> Here is what the program should do, and
> below is the program.

A very general suggestion:

In the beginning there was Chaos.
Then Dijkstra said, "Let there be structured programming"
and there was a little less Chaos.

And structured programming is not only about lop
and conditional structures (implemented in all
non-ancient language), but also about 'subroutines',
better known in C as functions.

If something looks like independent task, is should be packed
into a function. If something is used many times, or
just look like closed, logical task, it should be
a function.

This isn't too important is a code that you can print
on one page, but this is good exercise.


> 1) Get the card number from the user.

This point is very good example.
Write a function

int read_input(char * cards)

where you deal with communication with user,
reading input (to 'cards'), maybe dealing with errors.
The result type may be void, or int, if you want to return
a error code.

In main just put your buffor to this function.

int main(void)
{
      char card_number[17];

      if (read_input(card_number)==1)  //reading success
      {
        ...
      }  	

And then in next loop do the calculation.

This is not the optimal way to to your task if you look
at runtime, but is much more clear (and in the most cases
(not all but most) clear code is more important than 5% slower code)



> 2) Multiply every other digit by 2,
> starting with the number's second-to-last digit,
> and then add those products' digits together.
>
> 3) Add the sum to the sum of the digits that
> weren't multiplied by 2.
>
> 4) If the total's last digit is 0 (or, put more
> formally, if the total modulo 10 is congruent to
> 0), the number is valid.
>
> 5) If the card is valid, print that it's a valid
> card and the name of the issuer.
> The name of issuer depends on the following:
>
> American Express uses 15-digit numbers,
> MasterCard uses 16-digit numbers, and
> Visa uses 13- and 16-digit numbers.

You do not use the control sum in the program elsewhere.
It looks like you can create function that take
the number, then return a bool if it is valid and return
two first digits.


int first_digits
if (is_valid(card_number(&first_digits)))
{
     if-else ladder.
}


Int can contain both digits;)

Or, even better, you check if it is valid and what
the two first digits are in separate functions.


int first_digits
if (is_valid(card_number  ))
{
      first_digits = digg_first_digits(card_number);
      if-else ladder ussing lenght and 'first_diggis'
}


> American Express numbers all start with 34 or 37;
> MasterCard numbers all start with 51, 52, 53, 54, or 55;
> and Visa numbers all start with 4.
that is 40,41...49
You still can always look at the first two.

And test like
if (first_digits>=51 &&  first_digits<=55 )
is a easier way to check if a number is 51 or 52 ... or 55.


>          if (sum % 10 == 0)
>          {
>              if (strlen(card_number) == 15 && card_number[0] == 3 && (card_number[1] == 4 || card_number[1] == 7))
>                 printf ("AMEX");
>
>              else if (strlen(card_number) == 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");

No, this is too simple problem to summon great Cthulhu!
:)

bartekltg



Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Improving the program, issues with logical operators, incorrect output Alla _ <modelling.data@gmail.com> - 2015-09-08 06:47 -0700
  Re: Improving the program, issues with logical operators, incorrect output fir <profesor.fir@gmail.com> - 2015-09-08 07:23 -0700
    Re: Improving the program, issues with logical operators, incorrect output Alla _ <modelling.data@gmail.com> - 2015-09-08 07:25 -0700
      Re: Improving the program, issues with logical operators, incorrect output fir <profesor.fir@gmail.com> - 2015-09-08 07:44 -0700
      Re: Improving the program, issues with logical operators, incorrect output Richard Heathfield <rjh@cpax.org.uk> - 2015-09-08 17:04 +0100
      Re: Improving the program, issues with logical operators, incorrect output Keith Thompson <kst-u@mib.org> - 2015-09-08 09:21 -0700
  Re: Improving the program, issues with logical operators, incorrect output Richard Heathfield <rjh@cpax.org.uk> - 2015-09-08 18:32 +0100
  Re: Improving the program, issues with logical operators, incorrect output Barry Schwarz <schwarzb@dqel.com> - 2015-09-08 15:53 -0700
  Re: Improving the program, issues with logical operators, incorrect output bartekltg <bartekltg@gmail.com> - 2015-09-09 15:42 +0200
  Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-09 16:11 +0200
    Re: Improving the program, issues with logical operators, incorrect output Richard Heathfield <rjh@cpax.org.uk> - 2015-09-09 16:01 +0100
      Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-09 21:11 +0200
        Re: Improving the program, issues with logical operators, incorrect output Richard Heathfield <rjh@cpax.org.uk> - 2015-09-09 20:42 +0100
          Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-09 22:23 +0200
          Re: Improving the program, issues with logical operators, incorrect output Keith Thompson <kst-u@mib.org> - 2015-09-09 13:41 -0700
            Re: Improving the program, issues with logical operators, incorrect output Richard Heathfield <rjh@cpax.org.uk> - 2015-09-09 22:21 +0100
            Re: Improving the program, issues with logical operators, incorrect output Les Cargill <lcargill99@comcast.com> - 2015-09-09 18:25 -0500
    Re: Improving the program, issues with logical operators, incorrect output Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-09 12:41 -0700
      Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-09 22:24 +0200
      Re: Improving the program, issues with logical operators, incorrect output Keith Thompson <kst-u@mib.org> - 2015-09-09 13:34 -0700
        Re: Improving the program, issues with logical operators, incorrect output Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-09 16:25 -0700
          Re: Improving the program, issues with logical operators, incorrect output Keith Thompson <kst-u@mib.org> - 2015-09-09 17:38 -0700
            Re: Improving the program, issues with logical operators, incorrect output fir <profesor.fir@gmail.com> - 2015-09-12 00:48 -0700
              Re: Improving the program, issues with logical operators, incorrect output fir <profesor.fir@gmail.com> - 2015-09-12 00:55 -0700
              Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-12 14:50 +0200
                Re: Improving the program, issues with logical operators, incorrect output fir <profesor.fir@gmail.com> - 2015-09-12 07:12 -0700
        Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-10 09:56 +0200
  Re: Improving the program, issues with logical operators, incorrect output gordonb.papec@burditt.org (Gordon Burditt) - 2015-09-11 17:18 -0500
    Re: Improving the program, issues with logical operators, incorrect output Barry Schwarz <schwarzb@dqel.com> - 2015-09-13 01:08 -0700
    Re: Improving the program, issues with logical operators, incorrect output Stephen Sprunk <stephen@sprunk.org> - 2015-09-13 19:03 -0500

csiph-web