On Tue, 22 Apr 2008 01:14:16 -0700 (PDT), Nezhate
<> wrote:
>Hi all,
>I've not written c code for many times a go, and now I can't
>understand why this occur when executing the next program:
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>
>char line[100];
>char s;
>
>int begins(char string1[], char string2)
>{
> char answer[6];
> if (string1[0]==string2)
> {
> strcpy(answer,"True");
> printf ("%s, %s begins with %c\n",answer,string1,string2);
> }
> else
> {
> strcpy(answer,"False");
> printf("%s, %s do not begin with %c\n",answer,string1,string2);
> }
> return(0);
>}
>
>int main ()
>{
> // program to test begins function
> while(1)
> {
> printf ("Enter a new line:\n");
> fgets(line,sizeof(line),stdin);
> line[strlen(line)-1]='\0'; // To trim :remove the character
>'\0'added by fgets
> printf("Enter the character you wish!\n");
> scanf("%c",&s);
When you entered your input for this line, exactly how many keys did
you press? How many key presses did the scanf process? What do you
think happened to the excess?
See also question 12.18a and b of the faq at
www.c-faq.com
> begins(line,s);
> }
> exit(0);
>}
>When executing first time all is good but in the second time I can't
>enter a new line
>$ ./exo_9_2
>Enter a new line:
>This line begins with T
>Enter the character you wish!
>T
>True, This line begins with T begins with T
>Enter a new line:
>Enter the character you wish!
Remove del for email