While and fgets

N

Nezhate

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!
 
N

Nezhate

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

Nezhate

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!
 
N

Nezhate

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!
 
N

Nezhate

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!
 
N

Nezhate

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!
 
B

Barry Schwarz

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top