While and fgets

Discussion in 'C Programming' started by Nezhate, Apr 22, 2008.

  1. Nezhate

    Nezhate Guest

    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);
    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!
     
    Nezhate, Apr 22, 2008
    #1
    1. Advertising

  2. Nezhate

    kooladi Guest

    kooladi, Apr 22, 2008
    #2
    1. Advertising

  3. Nezhate

    Nezhate Guest

    Hi Adi,
    The printf isn't a part of program, I added it when I was debugging,
    so I must use strcpy.
     
    Nezhate, Apr 23, 2008
    #3
  4. Nezhate

    Nezhate Guest

    On Apr 23, 7:39 am, Nezhate <> wrote:
    > 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,sizeof(line),stdin);
    line[strlen(line)-1]='\0';
    printf("Enter the character you wish!\n");
    fgets(character,sizeof(character),stdin);
    character[strlen(character)-1]='\0';
    begins(line,character[0]);
    }
    exit(0);
    }

    Adi, Thanks for lnks!
     
    Nezhate, Apr 23, 2008
    #4
  5. Nezhate

    Nezhate Guest

    On Apr 23, 7:39 am, Nezhate <> wrote:
    > 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,sizeof(line),stdin);
    line[strlen(line)-1]='\0';
    printf("Enter the character you wish!\n");
    fgets(character,sizeof(character),stdin);
    character[strlen(character)-1]='\0';
    begins(line,character[0]);
    }
    exit(0);
    }

    Adi, Thanks for lnks!
     
    Nezhate, Apr 23, 2008
    #5
  6. Nezhate

    Nezhate Guest

    On Apr 23, 7:39 am, Nezhate <> wrote:
    > 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,sizeof(line),stdin);
    line[strlen(line)-1]='\0';
    printf("Enter the character you wish!\n");
    fgets(character,sizeof(character),stdin);
    character[strlen(character)-1]='\0';
    begins(line,character[0]);
    }
    exit(0);
    }

    Adi, Thanks for lnks!
     
    Nezhate, Apr 23, 2008
    #6
  7. Nezhate

    Nezhate Guest

    On Apr 23, 7:39 am, Nezhate <> wrote:
    > 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,sizeof(line),stdin);
    line[strlen(line)-1]='\0';
    printf("Enter the character you wish!\n");
    fgets(character,sizeof(character),stdin);
    character[strlen(character)-1]='\0';
    begins(line,character[0]);
    }
    exit(0);
    }

    Adi, Thanks for lnks!
     
    Nezhate, Apr 23, 2008
    #7
  8. Nezhate

    Nezhate Guest

    sorry, but It was an error.
     
    Nezhate, Apr 24, 2008
    #8
  9. 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
     
    Barry Schwarz, Apr 24, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RSoIsCaIrLiIoA

    on fgets and sscanf and their bugs

    RSoIsCaIrLiIoA, Apr 20, 2006, in forum: C Programming
    Replies:
    7
    Views:
    1,558
    Richard Heathfield
    Apr 25, 2006
  2. Justme
    Replies:
    9
    Views:
    619
    clayne
    Oct 1, 2006
  3. dave
    Replies:
    4
    Views:
    673
  4. Replies:
    24
    Views:
    1,100
    santosh
    Jul 18, 2007
  5. Eric Sosman

    Re: position of \0 while using fgets()

    Eric Sosman, Jan 2, 2011, in forum: C Programming
    Replies:
    1
    Views:
    852
    Ben Bacarisse
    Jan 2, 2011
Loading...

Share This Page