Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > scanf problem

Reply
Thread Tools

scanf problem

 
 
Pete
Guest
Posts: n/a
 
      10-16-2004
Following is a fragment from a program I'm writing. I am trying to enter an
8 bit binary representation from keyboard which is then scanfed into &Ptext
and then again into &Key1. This is not binary just a representation, when I
print each character in the two char arrays, Key1 displays ok but Ptext is
missing the first element. I'm stumped can someone please help. The
following compiles and runs to show my problem.

Thanks in advance.

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

main() {

char Ptext[8], Key1[8];
int i;

printf("Enter 8 bit binary string Plaintext: ");
scanf("%s", &Ptext);

printf("\n");

printf("Enter 8 bit binary string Key1 test key: ");
scanf("%s", &Key1);

printf("Plaintext is: ");
for(i=0; i<8; i++){
printf("%c", Ptext[i]);
}
printf("\n");

printf("Key No1 is: ");
for(i=0; i<8; i++){
printf("%c", Key1[i]);
}
}/*end main*/


 
Reply With Quote
 
 
 
 
Jaroslav Sykora
Guest
Posts: n/a
 
      10-16-2004
Pete wrote:

> Following is a fragment from a program I'm writing. I am trying to enter
> an 8 bit binary representation from keyboard which is then scanfed into
> &Ptext and then again into &Key1. This is not binary just a
> representation, when I print each character in the two char arrays, Key1
> displays ok but Ptext is missing the first element. I'm stumped can
> someone please help. The following compiles and runs to show my problem.
>
> Thanks in advance.
>
> #include <stdio.h>
> #include <string.h>
>
> main() {
>
> char Ptext[8], Key1[8];
> int i;
>
> printf("Enter 8 bit binary string Plaintext: ");
> scanf("%s", &Ptext);
>
> printf("\n");
>
> printf("Enter 8 bit binary string Key1 test key: ");
> scanf("%s", &Key1);
>
> printf("Plaintext is: ");
> for(i=0; i<8; i++){
> printf("%c", Ptext[i]);
> }
> printf("\n");
>
> printf("Key No1 is: ");
> for(i=0; i<8; i++){
> printf("%c", Key1[i]);
> }
> }/*end main*/


two errors:
1) you read in 8 characters so you need 9 bytes to store them:
char Ptext[9], Key1[9];
(Key1[9] will overwrite Ptest[0] by \0 in your version)

2) scanf is scanf("%s", char*) so there should be
scanf("%s", Ptext);
scanf("%s", Key1);
because Ptext[int] is char, Ptext is char* and &Ptext is char**

 
Reply With Quote
 
 
 
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      10-19-2004
Groovy hepcat Jaroslav Sykora was jivin' on Sat, 16 Oct 2004 15:29:17
+0200 in comp.lang.c.
Re: scanf problem's a cool scene! Dig it!

>Pete wrote:
>
>> char Ptext[8], Key1[8];

>
>2) scanf is scanf("%s", char*) so there should be
> scanf("%s", Ptext);
> scanf("%s", Key1);
>because Ptext[int] is char, Ptext is char* and &Ptext is char**


Not quite. Ptext is char []. It is converted to char *, but not when
it is the operand of the & operator. &Ptext is (char *)[].

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      10-19-2004
Groovy hepcat Pete was jivin' on Sat, 16 Oct 2004 20:19:25 +1000 in
comp.lang.c.
scanf problem's a cool scene! Dig it!

>Following is a fragment from a program I'm writing. I am trying to enter an
>8 bit binary representation from keyboard which is then scanfed into &Ptext
>and then again into &Key1. This is not binary just a representation, when I
>print each character in the two char arrays, Key1 displays ok but Ptext is
>missing the first element. I'm stumped can someone please help. The
>following compiles and runs to show my problem.
>
>Thanks in advance.
>
>#include <stdio.h>
>#include <string.h>
>
>main() {


int main(void) {

It was always a good idea to explicitly specify the return type. But
in C99 it is now mondatory.

>char Ptext[8], Key1[8];


As Jaroslav Sykora said, you need 9 elements for each of these
arrays, not 8. Remember, a char array needs (at least) one more
element than the length of the string you intend to store in it. Thus,
a string of 8 characters needs an array of 9 (or more).

>int i;
>
>printf("Enter 8 bit binary string Plaintext: ");


There is no guarantee that this prompt will be displayed right away.
So, you should do something that will increase the chances. You can
either a) terminate the output with a newline character or b) flush
stdout with fflush().

fflush(stdout);

Actually, you could also turn off buffering for stdout. But that's
probably a little drastic just to get a prompt to display.

> scanf("%s", &Ptext);


As Jaroslav said, this is not right. Since Ptext is an array, and an
array is converted to a pointer to its first element (except when it
is the operand of sizeof or &), what you need here is this:

scanf("%s", Ptext);

However, scanf() isn't a very good function to use for user input. A
better way is to use fgets().

> printf("\n");
>
> printf("Enter 8 bit binary string Key1 test key: ");
> scanf("%s", &Key1);


Since you have basically duplicated some functionality here
(displaying a prompt and then reading some input), you could put this
functionality in its own function. You might call this function
getbin8(), or something similar. It could take, as arguments, a const
char * for the prompt, and a char * for the input; display the prompt,
making sure to flush the output with fflush(), and read in the input
using fgets(). You could do error checking too, to guard against I/O
errors and to validate the input. And you might use this function like
so:

getbin8("Enter 8 bit binary string Plaintext: ", Ptext));
getbin8("Enter 8 bit binary string Key1 test key: ", Key1);

> printf("Plaintext is: ");
> for(i=0; i<8; i++){
> printf("%c", Ptext[i]);


Why waste cycles interpreting a printf() conversion string? Using
putchar(), fputc() or putc() is easier.

putchar(Ptext[i]);
> }
> printf("\n");


Again, you could use putchar(), fputc() or putc() here.

> printf("Key No1 is: ");
> for(i=0; i<8; i++){
> printf("%c", Key1[i]);
> }


Again you've duplicated functionality. You could easily put this in
its own function too.
And you should make sure the last line of output has a newline at
the end. It is implementation defined whether the last line requires a
newline, so make sure it has one, to be on the safe side. You can do
this by issuing another printf("\n") call (or by using putchar(),
fputc() or putc() as suggested).
Also, you do not have a return statement. This is bad. In C99 main()
is allowed to have no return statement, but it's still probably not a
good idea. And besides, if you had a C99 compiler it would have
complained about your definition of main(), since you didn't
explicitly state the return type.

return 0;

> }/*end main*/


You wouldn't need this pointless comment if you had indented your
code properly. Comments that do not add to one's understanding of the
code are useless. And poor formating makes the code ugly and hard to
read. Here's your code reformatted (and with a few of the above
mentioned problems fixed). I'm sure you'll agree that it is much
easier to read:

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

int main(void)
{
char Ptext[8], Key1[8];
int i;

printf("Enter 8 bit binary string Plaintext: ");
fflush(stdout);
scanf("%s", &Ptext);

printf("\n");

printf("Enter 8 bit binary string Key1 test key: ");
fflush(stdout);
scanf("%s", &Key1);

printf("Plaintext is: ");
for(i=0; i<8; i++)
{
printf("%c", Ptext[i]);
}
printf("\n");

printf("Key No1 is: ");
for(i=0; i<8; i++)
{
printf("%c", Key1[i]);
}
printf("\n");

return 0;
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-19-2004
Peter Shaggy Haywood wrote:

> >> char Ptext[8], Key1[8];


> &Ptext is (char *)[].


&Ptext is char (*)[]

--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-19-2004
pete wrote:
>
> Peter Shaggy Haywood wrote:
>
> > >> char Ptext[8], Key1[8];

>
> > &Ptext is (char *)[].

>
> &Ptext is char (*)[]


&Ptext is char (*)[8]

--
pete
 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      10-20-2004
Groovy hepcat pete was jivin' on Tue, 19 Oct 2004 04:01:01 GMT in
comp.lang.c.
Re: scanf problem's a cool scene! Dig it!

>Peter Shaggy Haywood wrote:
>
>> >> char Ptext[8], Key1[8];

>
>> &Ptext is (char *)[].

>
>&Ptext is char (*)[]


Yep. That's what I meant. Dunno what happened there. Typo.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
scanf/getchar sequence problem clusardi2k@aol.com C++ 21 04-20-2005 09:50 AM
Re: printf scanf problem Dan Pop C Programming 0 08-04-2003 01:54 PM
Re: printf scanf problem code_wrong C Programming 1 08-02-2003 08:10 PM



Advertisments