Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Issues with array, strlen, etc |
| Date | 2015-09-07 22:55 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87lhchq4h8.fsf@bsb.me.uk> (permalink) |
| References | <1866812f-e415-44b4-8127-8e21c4a13653@googlegroups.com> |
Alla _ <modelling.data@gmail.com> writes:
> 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)
You need 15 there. scanf must put a null (zero) byte at the end of the
input, and that means reading no more than 15 characters. You can, of
course, choose to change the array size to 17 instead.
> {
> for (i = (strlen(card_number)-1); i >= 0; i -= 1)
You said "every other digit". Setting i to i-1 will print the next one.
> {
> digit = card_number[i] * 2;
> printf ("%i", digit);
> }
You are likely to get confused by this output because it will be run
together. If is see 710 as the output is that one number, or the result
of printing 7 then 1 then 0, or 7 and then 10? You can't tell.
For now, do something like printf("[%i]", digit); so you can see where
one starts and another ends. You can remove the []s later.
Also, first, just print the digits without any multiplication and in the
normal order and see if you understand the output. Post again if you
don't!
for (i = 0; i < strlen(card_number); ++i) {
digit = card_number[1];
printf("[%i]", digit);
}
<snip>
--
Ben.
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