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 ?
* 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 ?