counting repeated words in input

A

arnuld

/* C++ Primer 4/e

* exercise 6.12, page 208
* STATEMENT
* write small programme to read a sequence of strings from
standard input
* looking for duplicated words. the programme should find places in
the input where
* one word is followed immediately by itself. keep track of the
largest number of
* times a single repetition occurs and which word is repeated . print
the maximum
* number of duplicates or else print the message that no word was
repeated. e.g for
* the input of:
* how cow now now how cow cow now now now how how
* the output should say: "now" occurs 3 times
*
*/

#include<iostream>
#include<string>

int main()
{
int current_cnt = 0;
int final_cnt = 0;

std::string current_string, prev_string, final_string;
/* all strings are initialised to empty strings */

while(std::cin >> current_string)
{
/* we can compare "current_string" to "final_string"
because "final_string" was initialised by its default contructor
to empty string */
if(current_string == prev_string)
{
++current_cnt;
final_cnt = current_cnt;
final_string = current_string;
}
else
{
current_cnt = 0;
}
}


/* print the desired output */

if(final_cnt == 0)
{
std::cout << "no word was repeated" << std::endl;
}
else
{
std::cout << "word "
<< final_string
<< " was repeated "
<< final_cnt
<< " times"
<< std::endl;
}

return 0;
}


this programme compiles and runs without any error but it has has a
semantic-bug. it never counts the repeated words and no matter what
you input it always provides one output: "no word was repeated".

i am am unable to trace the reason of bug. any ideas ?
 
C

Colander

/* C++ Primer 4/e

* exercise 6.12, page 208
* STATEMENT
* write small programme to read a sequence of strings from
standard input
* looking for duplicated words. the programme should find places in
the input where
* one word is followed immediately by itself. keep track of the
largest number of
* times a single repetition occurs and which word is repeated . print
the maximum
* number of duplicates or else print the message that no word was
repeated. e.g for
* the input of:
* how cow now now how cow cow now now now how how
* the output should say: "now" occurs 3 times
*
*/

#include<iostream>
#include<string>

int main()
{
int current_cnt = 0;
int final_cnt = 0;

std::string current_string, prev_string, final_string;
/* all strings are initialised to empty strings */

while(std::cin >> current_string)
{
/* we can compare "current_string" to "final_string"
because "final_string" was initialised by its default contructor
to empty string */
if(current_string == prev_string)
{
++current_cnt;
final_cnt = current_cnt;
final_string = current_string;
}
else
{
current_cnt = 0;
}
}

/* print the desired output */

if(final_cnt == 0)
{
std::cout << "no word was repeated" << std::endl;
}
else
{
std::cout << "word "
<< final_string
<< " was repeated "
<< final_cnt
<< " times"
<< std::endl;
}

return 0;

}

this programme compiles and runs without any error but it has has a
semantic-bug. it never counts the repeated words and no matter what
you input it always provides one output: "no word was repeated".

i am am unable to trace the reason of bug. any ideas ?


I think somewhere, somehow you should assign something to
'prev_string'.
 
Z

Zeng Fucen

/* C++ Primer 4/e

* exercise 6.12, page 208
* STATEMENT
* write small programme to read a sequence of strings from
standard input
* looking for duplicated words. the programme should find places in
the input where
* one word is followed immediately by itself. keep track of the
largest number of
* times a single repetition occurs and which word is repeated . print
the maximum
* number of duplicates or else print the message that no word was
repeated. e.g for
* the input of:
* how cow now now how cow cow now now now how how
* the output should say: "now" occurs 3 times
*
*/

#include<iostream>
#include<string>

int main()
{
int current_cnt = 0;
int final_cnt = 0;

std::string current_string, prev_string, final_string;
/* all strings are initialised to empty strings */

while(std::cin >> current_string)
{
/* we can compare "current_string" to "final_string"
because "final_string" was initialised by its default contructor
to empty string */
if(current_string == prev_string)
{
++current_cnt;
final_cnt = current_cnt;
final_string = current_string;
}
else
{
current_cnt = 0;
}
}

/* print the desired output */

if(final_cnt == 0)
{
std::cout << "no word was repeated" << std::endl;
}
else
{
std::cout << "word "
<< final_string
<< " was repeated "
<< final_cnt
<< " times"
<< std::endl;
}

return 0;

}

this programme compiles and runs without any error but it has has a
semantic-bug. it never counts the repeated words and no matter what
you input it always provides one output: "no word was repeated".

i am am unable to trace the reason of bug. any ideas ?


Yes ,I agree with Colander ,
see 'prev_string' in the 'while' loop
 
A

arnuld

I think somewhere, somehow you should assign something to 'prev_string'.

Colander thanks. here is the corrected code, any advice on improvement ?

/* C++ Primer 4/e

* exercise 6.12, page 208
* STATEMENT
* write small programme to read a sequence of strings from
standard input looking for duplicated words. the programme should find
places in the input where one word is followed immedoately by itself.
keep track of the largest number of times a single repitition occurs
and which word is repeated . print the maximum number of duplicates or
else prin tthe message that no word was repeated. e.g for the input of:
how cow now now how cow cow now now now how how the output shpuld
say: "now" occures 3 times
*/

#include<iostream>
#include<string>

int main()
{
int current_cnt = 0;
int final_cnt = 0;

std::string current_string, prev_string, final_string;
/* all strings are initialised to empty strings */

while(std::cin >> current_string)
{
/* we can compare "current_string" to "final_string"
because "final_string" was initialised by its default contructor to empty
string */
if(current_string == prev_string)
{
++current_cnt;
}
else
{
current_cnt = 0;
}

if(current_cnt > final_cnt)
{
final_cnt = current_cnt;
final_string = current_string;
}

prev_string = current_string;
}


++final_cnt;
/* because "final_cnt" will always start counting from 1 when a word
has entered 2 times already. an ugly fix though :( */

/* print the desired output */

if(final_cnt == 0)
{
std::cout << "no word was repeated" << std::endl;
}
else
{
std::cout << "word '"
<< final_string
<< "' was repeated "
<< final_cnt
<< " times"
<< std::endl;
}

return 0;
}

======= OUTPUT ===========
~/programming/cpp $ ./a.out
cow
cow
noe
now
now
now
how
how
cow
cow
word 'now' was repeated 3 times
~/programming/cpp $
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

/* C++ Primer 4/e

* exercise 6.12, page 208
* STATEMENT
* write small programme to read a sequence of strings from
standard input
* looking for duplicated words. the programme should find places in
the input where
* one word is followed immediately by itself. keep track of the
largest number of
* times a single repetition occurs and which word is repeated . print
the maximum
* number of duplicates or else print the message that no word was
repeated. e.g for
* the input of:
* how cow now now how cow cow now now now how how
* the output should say: "now" occurs 3 times
*
*/

#include<iostream>
#include<string>

int main()
{
int current_cnt = 0;
int final_cnt = 0;

std::string current_string, prev_string, final_string;
/* all strings are initialised to empty strings */

while(std::cin >> current_string)
{
/* we can compare "current_string" to "final_string"
because "final_string" was initialised by its default contructor
to empty string */
if(current_string == prev_string)
{
++current_cnt;
final_cnt = current_cnt;
final_string = current_string;
}
else
{
current_cnt = 0;
}
}


/* print the desired output */

if(final_cnt == 0)
{
std::cout << "no word was repeated" << std::endl;
}
else
{
std::cout << "word "
<< final_string
<< " was repeated "
<< final_cnt
<< " times"
<< std::endl;
}

return 0;
}


this programme compiles and runs without any error but it has has a
semantic-bug. it never counts the repeated words and no matter what
you input it always provides one output: "no word was repeated".

i am am unable to trace the reason of bug. any ideas ?

In addition to what Colander and Zeng Fucen said you should probably
also check if the current repetition is the longest one, in other words
check if current_cnt is larger than final_cnt before assigning to it.
 
O

osmium

arnuld said:
/* C++ Primer 4/e

* exercise 6.12, page 208
* STATEMENT
* write small programme to read a sequence of strings from
standard input
* looking for duplicated words. the programme should find places in
the input where
* one word is followed immediately by itself. keep track of the
largest number of
* times a single repetition occurs and which word is repeated . print
the maximum
* number of duplicates or else print the message that no word was
repeated. e.g for
* the input of:
* how cow now now how cow cow now now now how how
* the output should say: "now" occurs 3 times
*
*/

#include<iostream>
#include<string>

Does not have

#include <map>

Which is, IMO, the proper way to solve the problem described in the header.

<snip>
 
V

Victor Bazarov

osmium said:
Does not have

#include <map>

Which is, IMO, the proper way to solve the problem described in the
header.

The propriety of the way depends on the knowledge the book assumes
accumulated by the time the exercise is presented. If 'std::map'
has not been discussed before chapter 6, it cannot be used, AIUI.

V
 
O

osmium

Victor Bazarov said:
The propriety of the way depends on the knowledge the book assumes
accumulated by the time the exercise is presented. If 'std::map'
has not been discussed before chapter 6, it cannot be used, AIUI.

That's a good point which I didn't consider. BTW, I don't even know what
book is being discussed, I think there are at least two books with that
title. But my general feeling is that you shouldn't give a problem that
can't be solved "properly". I can see isolated instances where this rule is
not followed, but I think such cases should be few and far between.
 
V

Victor Bazarov

osmium said:
[..]
BTW, I don't even know
what book is being discussed, I think there are at least two books
with that title.

Both have the specific exercise on the specific page? Come on!
But my general feeling is that you shouldn't give a
problem that can't be solved "properly".

I don't agree at all. According to you[r feeling], solving a problem
numerically is "improper" if an analytical solution exists? Propriety
depends on many things, and choosing a proper method should be even
taught separately, and to achieve the ability to deduce propriety of
some method, the method should actually be known/familiar. That's why
without knowing all of them how do you learn what's "proper"?
I can see isolated
instances where this rule is not followed, but I think such cases
should be few and far between.

How "isolated" do you see them? Should bubble sort ever be taught?

V
 
C

Colander

Colander thanks. here is the corrected code, any advice on improvement ?

Actualy I don't, it should work properly now! As for the 'ugly'
correction you
do, it isn't that ugly, it's well documented in your comment. That's a
very
clean way to solve 'strange' or 'ugly' pieces of code, compliments!

Have fun coding,
Colander
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

If the task was to get the total number of occurrences of a word, then a
map would certainly be the best choice. But if asked for the number of
repetitions then I don't think that a map is a good choice, not even if
asked to count all repetitions. Maybe I missed something.
That's a good point which I didn't consider. BTW, I don't even know what
book is being discussed, I think there are at least two books with that
title.

The other book, I believe, is called C++ Primer Plus, not the best
choice of name I agree.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top