Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > While and fgets

Reply
Thread Tools

While and fgets

 
 
Nezhate
Guest
Posts: n/a
 
      04-22-2008
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!
 
Reply With Quote
 
 
 
 
kooladi
Guest
Posts: n/a
 
      04-22-2008
Have you tried reading the C-Faq.

http://c-faq.com/stdio/scanfinterlace.html
http://c-faq.com/stdio/scanfc.html

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

Regards,
Adi


 
Reply With Quote
 
 
 
 
Nezhate
Guest
Posts: n/a
 
      04-23-2008

Hi Adi,
The printf isn't a part of program, I added it when I was debugging,
so I must use strcpy.
 
Reply With Quote
 
Nezhate
Guest
Posts: n/a
 
      04-23-2008
On Apr 23, 7:39 am, Nezhate <(E-Mail Removed)> 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!
 
Reply With Quote
 
Nezhate
Guest
Posts: n/a
 
      04-23-2008
On Apr 23, 7:39 am, Nezhate <(E-Mail Removed)> 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!
 
Reply With Quote
 
Nezhate
Guest
Posts: n/a
 
      04-23-2008
On Apr 23, 7:39 am, Nezhate <(E-Mail Removed)> 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!
 
Reply With Quote
 
Nezhate
Guest
Posts: n/a
 
      04-23-2008
On Apr 23, 7:39 am, Nezhate <(E-Mail Removed)> 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!
 
Reply With Quote
 
Nezhate
Guest
Posts: n/a
 
      04-24-2008


sorry, but It was an error.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      04-24-2008
On Tue, 22 Apr 2008 01:14:16 -0700 (PDT), Nezhate
<(E-Mail Removed)> 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
 
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
Re: position of \0 while using fgets() Eric Sosman C Programming 1 01-02-2011 04:35 PM
problems using fgets() and sscanf() while modifying file contents allpervasive@gmail.com C Programming 24 07-18-2007 05:17 PM
seek and fgets and fputs fopen dave C++ 4 11-02-2006 10:10 PM
ow can i use fgets to read and ignore the first two lines of a file and output into another file Justme C Programming 9 10-01-2006 02:26 PM
on fgets and sscanf and their bugs RSoIsCaIrLiIoA C Programming 7 04-25-2006 06:42 PM



Advertisments