Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Issues with array, strlen, etc |
| Date | 2015-09-07 14:47 -0400 |
| Organization | Self |
| Message-ID | <55EDDBBF.9020804@verizon.net> (permalink) |
| References | <1866812f-e415-44b4-8127-8e21c4a13653@googlegroups.com> |
On 09/07/2015 01:47 PM, Alla _ wrote:
> Hello!
>
> I have started working on a new task.
> I decided to break the program into
> parts and then combine them.
>
> First part should prompt the user for
> a card number; then multiply every other
> digit by 2, starting from the number's
> second-to-last digit. (please, bear with
> me until I will be able to get rid of
> scanf; for now, please, allow me to
> use it).
>
> I plan to try a few approaches. I think
> I am facing not only C programming issues
> here, but also issues connected with math.
>
> First approach involves arrays. The program
> doesn't do what I thought it should. I post
> my thoughts within comments.
>
> #include <stdio.h>
> #include <string.h>
>
> int main(void)
> {
> char card_number[16];
>
> /** limiting the number of possible inputs by
> 16, because it represents the longest card
> number
> **/
> int i, digit;
>
> printf ("Please, enter your card number: \n");
>
> if (scanf ("%16s", card_number) == 1)
> {
> for (i = (strlen(card_number)-1); i >= 0; i -= 1)
> {
> digit = card_number[i] * 2;
Note: if the card number is "9876543210", then filling in the array by
calling scanf() this way is roughly is equivalent to
char card_number[16] = {'9', '8', '7', '6', '5', '4', '3', '2', '1',
'0', '\0'};
The main difference is that, with your code, card_number[11],
card_number[12], card_number[13], card_number[14], card_number[15] all
remain uninitialized. With my replacement code, they would all have
values of 0.
On a typical system that uses ASCII encoding, this will also be
equivalent to
char card_number[16] = {57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 0};
If that's not what you expected, that's probably where your problem is.
The reason is that '9' == 57; ASCII uses a value of 57 to encode the
character '9'. So do most of the other encodings currently in
wide-spread use, but there are exceptions, the most prominent being EBCDIC.
If you read in one digit at a time using scanf("%1.1d",
&card_number[i]), then scanf() will perform the conversion from the
character '5' to integer value 5 for you, so you don't have to worry
about it. It will use the appropriate encoding for your platform,
regardless of whether that is ASCII, UTF-8, EBCDIC, UCS2, Shift-JIS,
UTF-16, or anything else.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-07 10:47 -0700
Re: Issues with array, strlen, etc James Kuyper <jameskuyper@verizon.net> - 2015-09-07 14:47 -0400
Re: Issues with array, strlen, etc bartekltg <bartekltg@gmail.com> - 2015-09-07 22:03 +0200
Re: Issues with array, strlen, etc Paul N <gw7rib@aol.com> - 2015-09-07 13:08 -0700
Re: Issues with array, strlen, etc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-07 22:55 +0100
Re: Issues with array, strlen, etc Rosario19 <Ros@invalid.invalid> - 2015-09-08 07:59 +0200
Re: Issues with array, strlen, etc Rosario19 <Ros@invalid.invalid> - 2015-09-08 08:05 +0200
Re: Issues with array, strlen, etc Barry Schwarz <schwarzb@dqel.com> - 2015-09-07 20:35 -0700
Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-08 01:08 -0700
Re: Issues with array, strlen, etc Keith Thompson <kst-u@mib.org> - 2015-09-08 09:04 -0700
Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-10 01:22 -0700
Re: Issues with array, strlen, etc Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 09:38 +0100
Re: Issues with array, strlen, etc Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-09-10 07:34 -0400
Re: Issues with array, strlen, etc David Brown <david.brown@hesbynett.no> - 2015-09-10 13:47 +0200
Re: Issues with array, strlen, etc Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 13:11 +0100
Re: Issues with array, strlen, etc Keith Thompson <kst-u@mib.org> - 2015-09-10 10:21 -0700
Re: Issues with array, strlen, etc Geoff <geoff@invalid.invalid> - 2015-09-10 10:42 -0700
Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-08 01:11 -0700
Re: Issues with array, strlen, etc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 09:51 +0100
Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-08 02:39 -0700
csiph-web