While and fgets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nezhate

    While and fgets

    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,st ring1,string2);
    }
    else
    {
    strcpy(answer," False");
    printf("%s, %s do not begin with %c\n",answer,st ring1,string2);
    }
    return(0);
    }

    int main ()
    {
    // program to test begins function
    while(1)
    {
    printf ("Enter a new line:\n");
    fgets(line,size of(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);
    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!
  • kooladi

    #2
    Re: While and fgets

    Have you tried reading the C-Faq.




    Btw, what are you trying to achieve my copying "True" or "False" into
    answer. You could directly use them inside the printf

    Regards,
    Adi


    Comment

    • Nezhate

      #3
      Re: While and fgets


      Hi Adi,
      The printf isn't a part of program, I added it when I was debugging,
      so I must use strcpy.

      Comment

      • Nezhate

        #4
        Re: While and fgets

        On Apr 23, 7:39 am, Nezhate <mazouz.nezh... @gmail.comwrote :
        Hi Adi,
        The printf isn't a part of program, I added it when I was debugging,
        so I must use strcpy.
        Hi again,
        problem solved: to get while loop work fine, I removed the scanf.
        int main ()
        {
        while(1)
        {
        printf ("Enter a new line:\n");
        fgets(line,size of(line),stdin) ;
        line[strlen(line)-1]='\0';
        printf("Enter the character you wish!\n");
        fgets(character ,sizeof(charact er),stdin);
        character[strlen(characte r)-1]='\0';
        begins(line,cha racter[0]);
        }
        exit(0);
        }

        Adi, Thanks for lnks!

        Comment

        • Nezhate

          #5
          Re: While and fgets

          On Apr 23, 7:39 am, Nezhate <mazouz.nezh... @gmail.comwrote :
          Hi Adi,
          The printf isn't a part of program, I added it when I was debugging,
          so I must use strcpy.
          Hi again,
          problem solved: to get while loop work fine, I removed the scanf.
          int main ()
          {
          while(1)
          {
          printf ("Enter a new line:\n");
          fgets(line,size of(line),stdin) ;
          line[strlen(line)-1]='\0';
          printf("Enter the character you wish!\n");
          fgets(character ,sizeof(charact er),stdin);
          character[strlen(characte r)-1]='\0';
          begins(line,cha racter[0]);
          }
          exit(0);
          }

          Adi, Thanks for lnks!

          Comment

          • Nezhate

            #6
            Re: While and fgets

            On Apr 23, 7:39 am, Nezhate <mazouz.nezh... @gmail.comwrote :
            Hi Adi,
            The printf isn't a part of program, I added it when I was debugging,
            so I must use strcpy.
            Hi again,
            problem solved: to get while loop work fine, I removed the scanf.
            int main ()
            {
            while(1)
            {
            printf ("Enter a new line:\n");
            fgets(line,size of(line),stdin) ;
            line[strlen(line)-1]='\0';
            printf("Enter the character you wish!\n");
            fgets(character ,sizeof(charact er),stdin);
            character[strlen(characte r)-1]='\0';
            begins(line,cha racter[0]);
            }
            exit(0);
            }

            Adi, Thanks for lnks!

            Comment

            • Nezhate

              #7
              Re: While and fgets

              On Apr 23, 7:39 am, Nezhate <mazouz.nezh... @gmail.comwrote :
              Hi Adi,
              The printf isn't a part of program, I added it when I was debugging,
              so I must use strcpy.
              Hi again,
              problem solved: to get while loop work fine, I removed the scanf.
              int main ()
              {
              while(1)
              {
              printf ("Enter a new line:\n");
              fgets(line,size of(line),stdin) ;
              line[strlen(line)-1]='\0';
              printf("Enter the character you wish!\n");
              fgets(character ,sizeof(charact er),stdin);
              character[strlen(characte r)-1]='\0';
              begins(line,cha racter[0]);
              }
              exit(0);
              }

              Adi, Thanks for lnks!

              Comment

              • CBFalconer

                #8
                Re: While and fgets

                Nezhate wrote:
                Nezhate <mazouz.nezh... @gmail.comwrote :
                >
                >The printf isn't a part of program, I added it when I was
                >debugging, so I must use strcpy.
                >
                problem solved: to get while loop work fine, I removed the scanf.
                int main ()
                {
                while(1)
                {
                printf ("Enter a new line:\n");
                fgets(line,size of(line),stdin) ;
                line[strlen(line)-1]='\0';
                printf("Enter the character you wish!\n");
                fgets(character ,sizeof(charact er),stdin);
                character[strlen(characte r)-1]='\0';
                begins(line,cha racter[0]);
                }
                exit(0);
                }
                But why 4 posts of the identical message, spread over about 1
                hour? Usenet is not an instantaneous transmission mechanism.

                --
                [mail]: Chuck F (cbfalconer at maineline dot net)
                [page]: <http://cbfalconer.home .att.net>
                Try the download section.


                ** Posted from http://www.teranews.com **

                Comment

                • Nezhate

                  #9
                  Re: While and fgets



                  sorry, but It was an error.

                  Comment

                  • Barry Schwarz

                    #10
                    Re: While and fgets

                    On Tue, 22 Apr 2008 01:14:16 -0700 (PDT), Nezhate
                    <mazouz.nezhate @gmail.comwrote :
                    >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,st ring1,string2);
                    }
                    else
                    {
                    strcpy(answer," False");
                    printf("%s, %s do not begin with %c\n",answer,st ring1,string2);
                    }
                    return(0);
                    >}
                    >
                    >int main ()
                    >{
                    // program to test begins function
                    while(1)
                    {
                    printf ("Enter a new line:\n");
                    fgets(line,size of(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

                    Comment

                    Working...