473,379 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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,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!
Jun 27 '08 #1
9 4537
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
Jun 27 '08 #2

Hi Adi,
The printf isn't a part of program, I added it when I was debugging,
so I must use strcpy.
Jun 27 '08 #3
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,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!
Jun 27 '08 #4
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,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!
Jun 27 '08 #5
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,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!
Jun 27 '08 #6
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,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!
Jun 27 '08 #7
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,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);
}
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 **
Jun 27 '08 #8


sorry, but It was an error.
Jun 27 '08 #9
On Tue, 22 Apr 2008 01:14:16 -0700 (PDT), Nezhate
<ma************@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,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
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Redduck | last post by:
Hello everyone. I am frustrated, I have written the simple program below for a class and I am having problems with the DO-WHILE loop. On the first run through the loop, everything works well, the...
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
5
by: Patrick | last post by:
I'm trying to while loop using a flat txt file and write the values out into a form. I have 50-58 values to echo. Am I going to have to echo each line of html. Or is there a way to make this work...
1
by: 2stepme | last post by:
I know I should be using a database ... but ... I have created a few pages which store user data in text files. The user text files where created using session_encode. There is a text file for...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
23
by: lisp9000 | last post by:
I wrote a small test program to read a file of data and print each line, but it's only printing the 2nd line out of 3 total lines. The test file, "foo.txt", has 3 lines: 7388: Zn->Z0 Run...
3
by: Willem | last post by:
As we all know, the following code is not quite correct: while (!feof(fp)) { fgets(...); /* do_something */ } And should be replaced by: while (fgets(...)) { /* do_something */ } But...
11
by: nembo kid | last post by:
If i>0 the while loop is executed; if i==0 not. Ok, but also if i<0 the while loop is executed. So, which are the rules? Which values must assume the "test condition" to be assumed like true o...
4
by: jemccarthy13 | last post by:
I am a very very low level beginner programmer, trying to teach myself C, and I could use some help with the following program for guessing a random number. Please, when helping, try to use low...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.