473,396 Members | 1,804 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,396 software developers and data experts.

problem with getchar

The program compiles properly (most of it is from k&r) but the second
function (here character_count) gives wrong answer.
Can someone please explain why ?
#include<stdio.h>
#define IN 1
#define OUT 0

int word_count();
int character_count();

int
main(void)
{
printf("%d\n",word_count());
printf("%d\n",character_count());

return 0;
}

int
word_count ()
{

int c, nw, state;
state = OUT;
nw = 0;

while ((c = getchar ()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}

return nw ;
}
int
character_count ()
{
int c;
int nc = 0;
while ((c = getchar ()) != EOF)
++nc;
return nc;
}

Jun 10 '06 #1
11 2465

sh************@gmail.com schreef:
The program compiles properly (most of it is from k&r) but the second
function (here character_count) gives wrong answer.
What answer is required?
Can someone please explain why ?
getchar reads one character from stdin.

<snip>
int wc = 0; /* word count */
int cc = 0; /* character count */
int
main(void)
{ count(); printf("%d\n",wc);
printf("%d\n",cc);
both word_count() and character_count() call get_char independently.
Hence they operate on two totally different sequences. If this is
required, than it's ok. If both should operate on the _same_ sequence,
this is a serious problem.
return 0;
}
void
count (void) {

int c, nw, state;
state = OUT;
nw = 0;

It's good to see you practice FSM's. There may be easier ways to do it,
though.
while ((c = getchar ()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t') { state = OUT; } else if (state == OUT) { state = IN; ++wc;
++cc; }
}
}


Jun 10 '06 #2
sh************@gmail.com schrieb:
The program compiles properly (most of it is from k&r) but the second
function (here character_count) gives wrong answer.
What did you give as input? What did you expect? What did you get?

Can someone please explain why ?
In theory, it is perfectly possible that EOF is sticky for stdin,
so you would have to clear it before using character_count().
Did you test character_count() without preceding word_count()?
#include<stdio.h>
#define IN 1
#define OUT 0

int word_count();
int character_count();

int
main(void)
{
printf("%d\n",word_count());
printf("%d\n",character_count());

return 0;
}
Using
int
main (void)
{
printf("%d\n",character_count());

return 0;
}
on an input of "a bb ccc\ndddd eeeee ffffff\n" gives the
expected answer.
Maybe there is some conceptual problem on your part: If you
want to count the characters and words of _one_ set of inputs,
you either have to first read all the input and provide it as
argument to counting functions or you have to count everything
in one function as you read it (i.e. read a character with
getchar() and adjust _all_ counters as necessary).

int
word_count ()
{

int c, nw, state;
state = OUT;
Note: As you use the states IN and OUT only here, it may be
better to restrict their "visibility":
enum {OUT, IN};
state = OUT;
nw = 0;

while ((c = getchar ()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}

return nw ;
} int
character_count ()
{
int c;
int nc = 0;
while ((c = getchar ()) != EOF)
++nc;
return nc;
}


This looks fine to me.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 10 '06 #3
kl*****@xs4all.nl schrieb:
sh************@gmail.com schreef:
int wc = 0; /* word count */
int cc = 0; /* character count */
How is inducing the OP to use file scope identifiers with
extern linkage (vulgo: "globals") helpful?
int
main(void)
{ count();
printf("%d\n",wc);
printf("%d\n",cc);


both word_count() and character_count() call get_char independently.
Hence they operate on two totally different sequences. If this is
required, than it's ok. If both should operate on the _same_ sequence,
this is a serious problem.


True.
return 0;
}


void
count (void)
{
int c, nw, state;
state = OUT;
nw = 0;


It's good to see you practice FSM's. There may be easier ways to do it,
though.


FSM is no "recognized TLA" around here, so some people may
wonder about religious exercises... ;-)
while ((c = getchar ()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')

{
state = OUT;

}
else if (state == OUT)

{
state = IN;

++wc;
++cc;
}
How is that supposed to work? Your correction makes wc and cc
effectively the same. You probably meant:
++cc;
if (c == ' ' || c == '\n' || c == '\t') {
state = OUT;
}
else if (state == OUT) {
state = IN;
++wc;
}
However, there is still the exceedingly ugly use of file scope
wc and cc.
}
}

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 10 '06 #4
Hei

Just a beginner so I won't try to give any kind of technical reply.

If you change your code a little and add a printf statement to the body
of the while
statement in the function character_count you should get a clue as to
what is happening.
Something like this should do.

while ((c = getchar ()) != EOF){
printf ("The value of c is %d\n", c);
++nc;
}

The problem can be solved with the following:

while ((c = getchar ()) != EOF){
getchar(); /* Seems to eat up that newline character */
++nc;
}

As I said at the start I am just a beginner and there maybe a better
solution to the
problem that you face.

Take care.

t. ir
sh************@gmail.com wrote:
The program compiles properly (most of it is from k&r) but the second
function (here character_count) gives wrong answer.
Can someone please explain why ?
#include<stdio.h>
#define IN 1
#define OUT 0

int word_count();
int character_count();

int
main(void)
{
printf("%d\n",word_count());
printf("%d\n",character_count());

return 0;
}

int
word_count ()
{

int c, nw, state;
state = OUT;
nw = 0;

while ((c = getchar ()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}

return nw ;
}
int
character_count ()
{
int c;
int nc = 0;
while ((c = getchar ()) != EOF)
++nc;
return nc;
}


Jun 10 '06 #5
Please do not top-post.
Quote enough of the previous message(s) to provide sufficient
context and write your own reply interspersed with that and/or
below.

[Context: Seemingly erroneous character count function]

keltanokka schrieb:
sh************@gmail.com wrote:
int
character_count ()
{
int c;
int nc = 0;
while ((c = getchar ()) != EOF)
++nc;
return nc;
} Just a beginner so I won't try to give any kind of technical reply.

If you change your code a little and add a printf statement to the body
of the while statement in the function character_count you should
get a clue as to what is happening.
Something like this should do.

while ((c = getchar ()) != EOF){
printf ("The value of c is %d\n", c);
++nc;
}


Indeed. To be on the safe side, add fflush(stdout); if your
programme seems to "stop" at a certain point. fflush()ing an
output stream forces all remaining output to be written.
If output is terminated by a '\n' character, this happens
automatically for line-buffered streams but to be sure "when"
the whole thing is going wrong, use fflush().
The problem can be solved with the following:

while ((c = getchar ()) != EOF){
getchar(); /* Seems to eat up that newline character */
++nc;
}


You are reading two characters for every loop iteration but
increase nc only once.
This does not give the right number of characters.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 10 '06 #6

Michael Mair schreef:
kl*****@xs4all.nl schrieb:
sh************@gmail.com schreef:
int wc = 0; /* word count */
int cc = 0; /* character count */
How is inducing the OP to use file scope identifiers with
extern linkage (vulgo: "globals") helpful?


I did not want to rewrite the whole thing. This is not meant to be
copied verbatim or pretend to be production code. It's merely intended
to Q&D show what's wrong.
<snip>
FSM is no "recognized TLA" around here, so some people may
wonder about religious exercises... ;-)
'Xcuse me. I'll ask C3PO next time, round. FSM --> Finite State
Machine.
while ((c = getchar ()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')

{
state = OUT;

}
else if (state == OUT)

{
state = IN;

++wc;
++cc;
}


How is that supposed to work?


The way you suggest below. After giving him the main part of the
(probable) solution. (I still don't know what exactly the requirements
are, i left this for him to solve. You spotted it easily enough.
Your correction makes wc and cc
effectively the same. You probably meant:
++cc;
if (c == ' ' || c == '\n' || c == '\t') {
state = OUT;
}
else if (state == OUT) {
state = IN;
++wc;
}
However, there is still the exceedingly ugly use of file scope
wc and cc.


True. But I won't do _all_ his homework.

Jun 10 '06 #7
kl*****@xs4all.nl schrieb:
Michael Mair schreef:
FSM is no "recognized TLA" around here, so some people may
wonder about religious exercises... ;-)
'Xcuse me. I'll ask C3PO next time, round. FSM --> Finite State
Machine.


Thank you; not having to guess (and misunderstand) makes live
easier in discussions.
However, there is still

<snip: some matter>
True. But I won't do _all_ his homework.


Fair enough :-)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 10 '06 #8
Michael Mair wrote:
sh************@gmail.com schrieb:
The program compiles properly (most of it is from k&r) but the second
function (here character_count) gives wrong answer.
What did you give as input? What did you expect? What did you get?


This is my first posting so maybe I was not all that precise.
I gave the input as a text file.
My problem was that the first function works but the second does
not(independently both of them work i.e only if one of them is used it
works)

Can someone please explain why ?


In theory, it is perfectly possible that EOF is sticky for stdin,
so you would have to clear it before using character_count().
Did you test character_count() without preceding word_count()?
#include<stdio.h>
#define IN 1
#define OUT 0

int word_count();
int character_count();

int
main(void)
{
printf("%d\n",word_count());
printf("%d\n",character_count());

return 0;
}


Using
int
main (void)
{
printf("%d\n",character_count());

return 0;
}
on an input of "a bb ccc\ndddd eeeee ffffff\n" gives the
expected answer.
Maybe there is some conceptual problem on your part: If you
want to count the characters and words of _one_ set of inputs,
you either have to first read all the input and provide it as
argument to counting functions or you have to count everything
in one function as you read it (i.e. read a character with
getchar() and adjust _all_ counters as necessary).

int
word_count ()
{

int c, nw, state;
state = OUT;


Note: As you use the states IN and OUT only here, it may be
better to restrict their "visibility":
enum {OUT, IN};
state = OUT;
nw = 0;

while ((c = getchar ()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}

return nw ;
}

int
character_count ()
{
int c;
int nc = 0;
while ((c = getchar ()) != EOF)
++nc;
return nc;
}


This looks fine to me.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Jun 11 '06 #9
shekhardeodhar wrote:
Michael Mair wrote:
sh************@gmail.com schrieb:
The program compiles properly (most of it is from k&r) but the
second function (here character_count) gives wrong answer.


What did you give as input? What did you expect? What did you get?


This is my first posting so maybe I was not all that precise.
I gave the input as a text file.
My problem was that the first function works but the second does
not(independently both of them work i.e only if one of them is
used it works)
Can someone please explain why ?


You have two function operating on stdin. The first operates until
stdin has been read to EOF. When you call the second, the input
file is still at EOF, so it reads nothing.

You don't want to get into any nonsense about rewinding, because in
addition to being a nuisance it may not be possible (think
interactive input). So read the input once and draw conclusions.
Somewhere you can have something like:

while (EOF != (ch = getchar())) {
chcount++;
if (isspace(ch)) state = OUT;
else if (OUT == state) {
state = IN;
wdcount++;
}
}

Now you have to worry about where to declare and initialize, and
how to return, the values of chcount and wdcount.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Jun 11 '06 #10
Michael Mair <Mi**********@invalid.invalid> writes:
Please do not top-post.
Quote enough of the previous message(s) to provide sufficient
context and write your own reply interspersed with that and/or
below.


It looks like Google really has fixed the quoting problem -- but they
still leave the cursor at the *top* of the article, and they don't
provde any clue that the user should write his reply at the bottom.
We're going to be seeing a lot of top-posting.

Maybe Google will fix this after a few years of complaining.

*sigh*

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 11 '06 #11

Keith Thompson wrote:
Michael Mair <Mi**********@invalid.invalid> writes:
Please do not top-post.
Quote enough of the previous message(s) to provide sufficient
context and write your own reply interspersed with that and/or
below.


It looks like Google really has fixed the quoting problem -- but they
still leave the cursor at the *top* of the article, and they don't
provde any clue that the user should write his reply at the bottom.
We're going to be seeing a lot of top-posting.

Maybe Google will fix this after a few years of complaining.


Actually the cursor appears at the bottom of the article on my browser.
But if the article has greater length than the size of the typing
window
then you need to scroll to the bottom of the window to see the cursor.
Presumably some users will just click at the top of the window if they
don't see a cursor.

But I don't think that all that is very relevant. Not top-posting
should
be common sense.

Spiros Bousbouras

Jun 12 '06 #12

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

Similar topics

21
by: clusardi2k | last post by:
/* The below code on SGI will wait for you to enter 2 things, but on Linux it will only wait the first time. I can make the code work by replacing the scanf with: char data ; fgets...
13
by: broeisi | last post by:
Hello, Can someone help me out with this one? The following program should read characters till it met the EOF (-1) on my pc. I'm running linux and using the gcc compiler version 3.4.5. But...
5
by: Jonathan | last post by:
Hi-- I have the following code: #include <stdio.h> char a,b; int main()
6
by: KOFKS | last post by:
I have come across a problem when I'm reading "comp.lang.c FAQ list · Question 12.1". This item is about char c; while((c = getchar()) != EOF) ... One paragraph of the item writes:"If type...
25
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
14
by: arnuld | last post by:
i have slightly modified the programme from section 1.5.1 which takes the input frm keyboard and then prints that to the terminal. it just does not run and i am unable to understand the error...
20
by: Senthil-Raja | last post by:
The getchar() function is expected to fetch the next character in the input stream and return it. But, when I wrote a program using this function, it looks like the reading of the input stream...
9
by: primeSo | last post by:
// FIRST int main(void){ int c, i = 0; char cArray; while( (c = getchar()) != '\n' && c != EOF){ cArray = c; i ++; }
2
by: Raj | last post by:
Following is a code to print integer equivalent of a hexadecimal number. If the entered number starts with a 0x or a 0X, calculation is done after skipping these two characters. Now, this code...
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: 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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.