Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #69904 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-09-08 06:47 -0700 |
| Last post | 2015-09-13 19:03 -0500 |
| Articles | 20 on this page of 30 — 11 participants |
Back to article view | Back to comp.lang.c
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
Page 1 of 2 [1] 2 Next page →
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-08 06:47 -0700 |
| Subject | Improving the program, issues with logical operators, incorrect output |
| Message-ID | <cd2ea66c-053e-4ba5-8704-211f2dd5609b@googlegroups.com> |
Hello!
Here is what the program should do, and
below is the program.
1) Get the card number from the user.
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.
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.
I have tried to add above conditions to my program,
but all conditions based on the value of first two
numbers ruined the program, and the program returns
incorrect outputs.
#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)
{
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");
}
else if (strlen(card_number) == 16 && card_number[0] == 4)
printf ("VISA");
else
printf ("Card of an unknown card issuer\n");
}
else
printf ("INVALID");
}
printf ("\n");
return 0;
}
[toc] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2015-09-08 07:23 -0700 |
| Message-ID | <993b8cd3-b9ad-491a-bda7-7ddc50bf46c8@googlegroups.com> |
| In reply to | #69904 |
W dniu wtorek, 8 września 2015 15:47:39 UTC+2 użytkownik Alla _ napisał:
> Hello!
>
> Here is what the program should do, and
> below is the program.
>
> 1) Get the card number from the user.
>
> 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.
>
> 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.
>
> I have tried to add above conditions to my program,
> but all conditions based on the value of first two
> numbers ruined the program, and the program returns
> incorrect outputs.
>
> #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)
> {
> 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");
> }
>
> else if (strlen(card_number) == 16 && card_number[0] == 4)
> printf ("VISA");
>
> else
> printf ("Card of an unknown card issuer\n");
> }
>
> else
> printf ("INVALID");
> }
>
> printf ("\n");
> return 0;
> }
I think you should not produce to much threads on the same topic.. right now it is yet not radically to much but better join some of your posts in one related thread, with maybe only some exceptions - you made really lot of threads on nearly same baginning topics
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-08 07:25 -0700 |
| Message-ID | <16775f9b-598e-4023-91da-7aeb791ecbf6@googlegroups.com> |
| In reply to | #69906 |
On Tuesday, September 8, 2015 at 6:23:41 PM UTC+4, fir wrote:
> W dniu wtorek, 8 września 2015 15:47:39 UTC+2 użytkownik Alla _ napisał:
> > Hello!
> >
> > Here is what the program should do, and
> > below is the program.
> >
> > 1) Get the card number from the user.
> >
> > 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.
> >
> > 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.
> >
> > I have tried to add above conditions to my program,
> > but all conditions based on the value of first two
> > numbers ruined the program, and the program returns
> > incorrect outputs.
> >
> > #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)
> > {
> > 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");
> > }
> >
> > else if (strlen(card_number) == 16 && card_number[0] == 4)
> > printf ("VISA");
> >
> > else
> > printf ("Card of an unknown card issuer\n");
> > }
> >
> > else
> > printf ("INVALID");
> > }
> >
> > printf ("\n");
> > return 0;
> > }
>
> I think you should not produce to much threads on the same topic.. right now it is yet not radically to much but better join some of your posts in one related thread, with maybe only some exceptions - you made really lot of threads on nearly same baginning topics
you are right. I was afraid to overload previous
threads. have deleted this one. sorry for any
inconveniences.
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2015-09-08 07:44 -0700 |
| Message-ID | <7559f275-01da-4079-a331-a42537d43ffd@googlegroups.com> |
| In reply to | #69907 |
W dniu wtorek, 8 września 2015 16:25:45 UTC+2 użytkownik Alla _ napisał:
> On Tuesday, September 8, 2015 at 6:23:41 PM UTC+4, fir wrote:
> > W dniu wtorek, 8 września 2015 15:47:39 UTC+2 użytkownik Alla _ napisał:
> > > Hello!
> > >
> > > Here is what the program should do, and
> > > below is the program.
> > >
> > > 1) Get the card number from the user.
> > >
> > > 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.
> > >
> > > 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.
> > >
> > > I have tried to add above conditions to my program,
> > > but all conditions based on the value of first two
> > > numbers ruined the program, and the program returns
> > > incorrect outputs.
> > >
> > > #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)
> > > {
> > > 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");
> > > }
> > >
> > > else if (strlen(card_number) == 16 && card_number[0] == 4)
> > > printf ("VISA");
> > >
> > > else
> > > printf ("Card of an unknown card issuer\n");
> > > }
> > >
> > > else
> > > printf ("INVALID");
> > > }
> > >
> > > printf ("\n");
> > > return 0;
> > > }
> >
> > I think you should not produce to much threads on the same topic.. right now it is yet not radically to much but better join some of your posts in one related thread, with maybe only some exceptions - you made really lot of threads on nearly same baginning topics
>
> you are right. I was afraid to overload previous
> threads. have deleted this one. sorry for any
> inconveniences.
im speaking general 'advice' or how to call it, right now it is not yet so much bad but if you would make 10 new threads
on this simple program (really oldschool
meaning of the program) i think it will be to much
as those above it is simple read the input (as someone said validate it)
then check those first letters then
sum those sums - yopu know what to do i think.. i may scary you that bigger programming is much much harder (though it somewhad depends)
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-08 17:04 +0100 |
| Message-ID | <msn0qg$cta$1@dont-email.me> |
| In reply to | #69907 |
On 08/09/15 15:25, Alla _ wrote:
> On Tuesday, September 8, 2015 at 6:23:41 PM UTC+4, fir wrote:
>> W dniu wtorek, 8 września 2015 15:47:39 UTC+2 użytkownik Alla _ napisał:
>>> Hello!
>>>
>>> Here is what the program should do, and
>>> below is the program.
>>>
>>> 1) Get the card number from the user.
>>>
>>> 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.
>>>
>>> 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.
>>>
>>> I have tried to add above conditions to my program,
>>> but all conditions based on the value of first two
>>> numbers ruined the program, and the program returns
>>> incorrect outputs.
>>>
>>> #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)
>>> {
>>> 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");
>>> }
>>>
>>> else if (strlen(card_number) == 16 && card_number[0] == 4)
>>> printf ("VISA");
>>>
>>> else
>>> printf ("Card of an unknown card issuer\n");
>>> }
>>>
>>> else
>>> printf ("INVALID");
>>> }
>>>
>>> printf ("\n");
>>> return 0;
>>> }
>>
>> I think you should not produce to much threads on the same topic.. right now it is yet not radically to much but better join some of your posts in one related thread, with maybe only some exceptions - you made really lot of threads on nearly same baginning topics
>
> you are right.
No, he's wrong. In all the months he has been posting here, he has been
right on perhaps two, maybe even as many as three occasions. This isn't
one of them.
--
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] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-09-08 09:21 -0700 |
| Message-ID | <lnh9n4swzn.fsf@kst-u.example.com> |
| In reply to | #69907 |
Alla _ <modelling.data@gmail.com> writes:
> On Tuesday, September 8, 2015 at 6:23:41 PM UTC+4, fir wrote:
[snip]
>
> you are right. I was afraid to overload previous
> threads. have deleted this one. sorry for any
> inconveniences.
You can't delete articles. There are mechanisms for "cancelling"
articles you've posted, but they generally don't work.
I wouldn't worry too much about any criticism from fir.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-08 18:32 +0100 |
| Message-ID | <VYEHx.430210$tz.320756@fx02.am4> |
| In reply to | #69904 |
On 08/09/2015 14:47, Alla _ wrote:
<snip>
> if (scanf ("%s", card_number) == 1)
What do you think could happen if the user types more data than the
array can store?
I'm not going to look at the rest of the program right now, because this
is the single biggest problem with your code - this is the Lesson You
Must Learn.
--
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] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-09-08 15:53 -0700 |
| Message-ID | <jiouua981b9jg1srkcp4jjdhk0miq3ikvp@4ax.com> |
| In reply to | #69904 |
On Tue, 8 Sep 2015 06:47:29 -0700 (PDT), Alla _ <modelling.data@gmail.com> wrote: >I have tried to add above conditions to my program, >but all conditions based on the value of first two >numbers ruined the program, and the program returns >incorrect outputs. Be specific. What inputs return incorrect results? What did you expect? What did you get? -- Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | bartekltg <bartekltg@gmail.com> |
|---|---|
| Date | 2015-09-09 15:42 +0200 |
| Message-ID | <mspcvi$rcq$1@node1.news.atman.pl> |
| In reply to | #69904 |
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
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2015-09-09 16:11 +0200 |
| Message-ID | <mspej3$u3d$1@dont-email.me> |
| In reply to | #69904 |
On 08/09/15 15:47, Alla _ wrote:
<snip>
>
> 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");
> }
>
> else if (strlen(card_number) == 16 && card_number[0] == 4)
> printf ("VISA");
>
> else
> printf ("Card of an unknown card issuer\n");
> }
>
> else
> printf ("INVALID");
> }
>
> printf ("\n");
> return 0;
> }
>
This is not actually something you asked about, but I'd like to give you
a little advice about structure and coding style. (bartekltg
recommended restructuring as separate functions, and I fully agree with
that.)
Note first that style is a matter of personal choice and preference,
unless you have a style forced upon you (by coding rules for a project,
employer, etc.). And people sometimes have strong opinions on what is
good style and what is not - some people will tell you that /obviously/
code is clearer and neater with lots of brackets to show exactly what is
happening, while others will tell you that /obviously/ you should avoid
unnecessary brackets that get in the way of the code.
What I am going to recommend here is basically what is known as "the one
true brace style". I believe it is a style that makes code structure
clear, and minimises the risk of differences between the visual
structure (what the code looks like to the programmer) and the syntactic
structure (how it is actually structured, as seen by the compiler which
ignores spacing, newlines, indentation, etc.).
Note that I put in more parenthesis () than many people - I don't want
to start a discussion about this, it is up to you to decide if you want
to use them or not. But I can tell you that many safety and reliability
oriented embedded coding standards mandate them - and they do so for a
reason. (However, such standards also often have rules that I think are
completely wrong!)
I am spacing the code out a little more than I usually would, and I
would probably re-structure the tests to avoid conditionals that span
multiple lines - but I am trying to avoid changing your code here,
except to skip a little of it.
{
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) == 15) &&
(card_number[0] == 5)) {
printf("MASTERCARD");
} else {
printf("INVALID");
}
} else {
printf("INVALID");
}
}
Key points here:
You move in one tab space if and only if the previous line ended in {.
And when you write a }, you move back a tab space on that line.
The open brackets goes on the end of the "if" line. Close brackets can
be combined with "else" (or the "while" in a do-while loop), but not
anything else.
After /every/ "if", and every "else", you always have { ... } - with the
exception of "else if". It keeps everything neat and clear, there is
never any risk if something that looks like a single statement is
actually more than one statement (or no statement at all), which can
happen unexpectedly with macros.
Think about coding style, read about it (such as on the Wikipedia
article), and find a style that suits you and helps you write code that
is as clear and easily maintainable as you can manage. You will have to
understand different styles written by different people, but try to find
a preference for your own code, and get used to writing it - it's easier
to get good habits at the beginning than to change later on.
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-09 16:01 +0100 |
| Message-ID | <pRXHx.303626$GG3.270469@fx10.am4> |
| In reply to | #70043 |
On 09/09/2015 15:11, David Brown wrote: <snip> > What I am going to recommend here is basically what is known as "the one > true brace style". Heresy! Stop poisoning her mind! Her bracing style is Just Fine. Leave it alone. > I believe it is a style that makes code structure clear, You can go on believing that if you like (it's a free country), but comp.lang.c is not the proper forum for evangelism. Really, Mr Brown, I'm surprised at you! 'Tis a most un-Scottish way to go on. -- 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] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2015-09-09 21:11 +0200 |
| Message-ID | <msq04l$bi7$1@dont-email.me> |
| In reply to | #70046 |
On 09/09/15 17:01, Richard Heathfield wrote: > On 09/09/2015 15:11, David Brown wrote: > > <snip> > >> What I am going to recommend here is basically what is known as "the one >> true brace style". > > Heresy! Stop poisoning her mind! Her bracing style is Just Fine. Leave > it alone. You are beyond saving, but I believe there is hope for Alla_ yet. Leave the dark side, with it's strange and inconsistent mixture of indentations and risky "else" clause, and write your code as the prophets K and R intended! > >> I believe it is a style that makes code structure clear, > > You can go on believing that if you like (it's a free country), but > comp.lang.c is not the proper forum for evangelism. > > Really, Mr Brown, I'm surprised at you! 'Tis a most un-Scottish way to > go on. >
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-09 20:42 +0100 |
| Message-ID | <wY%Hx.329115$Ec3.78627@fx24.am4> |
| In reply to | #70063 |
On 09/09/2015 20:11, David Brown wrote: > On 09/09/15 17:01, Richard Heathfield wrote: >> On 09/09/2015 15:11, David Brown wrote: >> >> <snip> >> >>> What I am going to recommend here is basically what is known as "the one >>> true brace style". >> >> Heresy! Stop poisoning her mind! Her bracing style is Just Fine. Leave >> it alone. > > You are beyond saving, but I believe there is hope for Alla_ yet. It's Alla, not Alla_. _ would appear to be her surname. (Oh, I *hope* that's true! It'd be one in the eye for an awful lot of computer systems.) > > Leave the dark side, with it's Can you see the damage 1TBS does, David? You're turning into a greengrocer! > strange and inconsistent mixture of Inconsistent? Me? Nonsense. Most of the time, I'm 100% consistent. > indentations and risky "else" clause, and write your code as the > prophets K and R intended! There's nothing risky about my else, I'll have you know. As for the prophets, they had the foresight to realise that they couldn't be expected to get /everything/ right, which is why they gave us such freedom in pracing our blaces. They knew that a sensible style would eventually evolve, and so it did - Allman style. (Nowadays we have to call it "Allperson".) Seriously, Alla, if you're reading all this guff, you may have realised by now that bracing style (and layout in general) really is a matter of religion. Pick a style that suits *you*, and pay no attention either to myself or David on that score, but be prepared to change it when to do so would be a genuine improvement. I picked a style early on, and I have more or less stuck to it for the last quarter-century, although I have tweaked it in minor ways over the years when I saw ways that it might be improved. I'm not daft enough to let someone else (especially not David!) pick my style for me, and neither should you be (unless someone's paying you, in which case of course they call the tune). -- 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] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2015-09-09 22:23 +0200 |
| Message-ID | <msq4cf$tlg$1@dont-email.me> |
| In reply to | #70067 |
On 09/09/15 21:42, Richard Heathfield wrote: > On 09/09/2015 20:11, David Brown wrote: >> On 09/09/15 17:01, Richard Heathfield wrote: >>> On 09/09/2015 15:11, David Brown wrote: >>> >>> <snip> >>> >>>> What I am going to recommend here is basically what is known as >>>> "the one >>>> true brace style". >>> >>> Heresy! Stop poisoning her mind! Her bracing style is Just Fine. Leave >>> it alone. >> >> You are beyond saving, but I believe there is hope for Alla_ yet. > > It's Alla, not Alla_. _ would appear to be her surname. (Oh, I *hope* > that's true! It'd be one in the eye for an awful lot of computer systems.) > >> >> Leave the dark side, with it's > > Can you see the damage 1TBS does, David? You're turning into a greengrocer! > >> strange and inconsistent mixture of > > Inconsistent? Me? Nonsense. Most of the time, I'm 100% consistent. > >> indentations and risky "else" clause, and write your code as the >> prophets K and R intended! > > There's nothing risky about my else, I'll have you know. As for the > prophets, they had the foresight to realise that they couldn't be > expected to get /everything/ right, which is why they gave us such > freedom in pracing our blaces. They knew that a sensible style would > eventually evolve, and so it did - Allman style. (Nowadays we have to > call it "Allperson".) > > Seriously, Alla, if you're reading all this guff, you may have realised > by now that bracing style (and layout in general) really is a matter of > religion. Pick a style that suits *you*, and pay no attention either to > myself or David on that score, but be prepared to change it when to do > so would be a genuine improvement. I picked a style early on, and I have > more or less stuck to it for the last quarter-century, although I have > tweaked it in minor ways over the years when I saw ways that it might be > improved. I'm not daft enough to let someone else (especially not > David!) pick my style for me, and neither should you be (unless > someone's paying you, in which case of course they call the tune). > And if nothing else tells you how seriously we are taking this, the phrase "pracing our blaces" should be a hint!
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-09-09 13:41 -0700 |
| Message-ID | <lnk2rzpbq5.fsf@kst-u.example.com> |
| In reply to | #70067 |
Richard Heathfield <rjh@cpax.org.uk> writes:
[...]
> Seriously, Alla, if you're reading all this guff, you may have realised
> by now that bracing style (and layout in general) really is a matter of
> religion. Pick a style that suits *you*, and pay no attention either to
> myself or David on that score, but be prepared to change it when to do
> so would be a genuine improvement. I picked a style early on, and I have
> more or less stuck to it for the last quarter-century, although I have
> tweaked it in minor ways over the years when I saw ways that it might be
> improved. I'm not daft enough to let someone else (especially not
> David!) pick my style for me, and neither should you be (unless
> someone's paying you, in which case of course they call the tune).
Agreed.
I have my own preferences about brace placement, as well as the
tabs-vs-spaces controversy. And I've found that the only thing
worse than using a style other than my own is mixing different
styles within the same project.
I've worked on projects where the coding standards imposed
requirements that I absolutely *hated*, but following the coding
standards and keeping the code consistent is far more important
than following my own personal preferences (even though they're
clearly superior).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-09 22:21 +0100 |
| Message-ID | <7p1Ix.327890$nx.314456@fx16.am4> |
| In reply to | #70077 |
On 09/09/2015 21:41, Keith Thompson wrote: <snip> > I have my own preferences about brace placement, ...which are, of course, quite wrong, but that's another matter. > as well as the > tabs-vs-spaces controversy. Two space indent, end of argument! > And I've found that the only thing > worse than using a style other than my own is mixing different > styles within the same project. I would guess that anyone who has been writing C for a while has developed a style which they tend to stick to, and so a pot-pourri of styles is probably more of an issue in multi-programmer projects. A house style is one solution, and indent(1) is another. These can, of course, be combined: checkout -> indent (my prefs) -> edit/compile/test/debug -> indent (house prefs) -> checkin - although some people won't like this either, as it is sometimes helpful, for a particular place in the code, to have the spaces *just* so, and there's no way to tell indent to respect that. > I've worked on projects where the coding standards imposed > requirements that I absolutely *hated*, but following the coding > standards and keeping the code consistent is far more important One of my first tasks on starting a new project has always been to read the coding standards thoroughly, and the second task is to compile a list of proposed changes. (Oddly, I don't recall ever having an issue with bracing style - I've been very lucky in that respect.) > than following my own personal preferences (even though they're > clearly superior). Your preferences will only become superior when you change them to match mine. -- 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] | [next] | [standalone]
| From | Les Cargill <lcargill99@comcast.com> |
|---|---|
| Date | 2015-09-09 18:25 -0500 |
| Message-ID | <msqesh$8at$2@dont-email.me> |
| In reply to | #70077 |
Keith Thompson wrote: > Richard Heathfield <rjh@cpax.org.uk> writes: > [...] >> Seriously, Alla, if you're reading all this guff, you may have realised >> by now that bracing style (and layout in general) really is a matter of >> religion. Pick a style that suits *you*, and pay no attention either to >> myself or David on that score, but be prepared to change it when to do >> so would be a genuine improvement. I picked a style early on, and I have >> more or less stuck to it for the last quarter-century, although I have >> tweaked it in minor ways over the years when I saw ways that it might be >> improved. I'm not daft enough to let someone else (especially not >> David!) pick my style for me, and neither should you be (unless >> someone's paying you, in which case of course they call the tune). > > Agreed. > > I have my own preferences about brace placement, as well as the > tabs-vs-spaces controversy. And I've found that the only thing > worse than using a style other than my own is mixing different > styles within the same project. > > I've worked on projects where the coding standards imposed > requirements that I absolutely *hated*, but following the coding > standards and keeping the code consistent is far more important > than following my own personal preferences (even though they're > clearly superior). > http://astyle.sourceforge.net/ -- Les Cargill
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-09-09 12:41 -0700 |
| Message-ID | <49a5f4a4-8d02-4a48-a338-33a7ebaed71b@googlegroups.com> |
| In reply to | #70043 |
On Wednesday, September 9, 2015 at 3:11:44 PM UTC+1, David Brown wrote: > > What I am going to recommend here is basically what is known as "the one > true brace style". I believe it is a style that makes code structure > clear, and minimises the risk of differences between the visual > structure (what the code looks like to the programmer) and the syntactic > structure (how it is actually structured, as seen by the compiler which > ignores spacing, newlines, indentation, etc.). > As far as a I can ascertain, that style was used in printed books to save vertical space. Printing is a different technology to screen scrolling, and paper costs money.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2015-09-09 22:24 +0200 |
| Message-ID | <msq4f2$tlg$2@dont-email.me> |
| In reply to | #70066 |
On 09/09/15 21:41, Malcolm McLean wrote: > On Wednesday, September 9, 2015 at 3:11:44 PM UTC+1, David Brown wrote: >> >> What I am going to recommend here is basically what is known as "the one >> true brace style". I believe it is a style that makes code structure >> clear, and minimises the risk of differences between the visual >> structure (what the code looks like to the programmer) and the syntactic >> structure (how it is actually structured, as seen by the compiler which >> ignores spacing, newlines, indentation, etc.). >> > As far as a I can ascertain, that style was used in printed books to > save vertical space. Printing is a different technology to screen > scrolling, and paper costs money. > Different styles fit different people - my biggest point here is that Alla should think about it, and pick a style that works best for her.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-09-09 13:34 -0700 |
| Message-ID | <lnoahbpc0y.fsf@kst-u.example.com> |
| In reply to | #70066 |
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Wednesday, September 9, 2015 at 3:11:44 PM UTC+1, David Brown wrote:
>> What I am going to recommend here is basically what is known as "the one
>> true brace style". I believe it is a style that makes code structure
>> clear, and minimises the risk of differences between the visual
>> structure (what the code looks like to the programmer) and the syntactic
>> structure (how it is actually structured, as seen by the compiler which
>> ignores spacing, newlines, indentation, etc.).
>>
> As far as a I can ascertain, that style was used in printed books to
> save vertical space. Printing is a different technology to screen
> scrolling, and paper costs money.
Do you have any evidence that that was the actual reason? I use the
1TBS myself, and saving vertical space is not my motivation.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c
csiph-web