Home

arnuld wrote:


> int main()
> {
> std::string cstr, pstr;
> bool same_str = false;
>
> while(std::cin >> cstr)
> {
> if(cstr == pstr)
> {
> same_str = true;
> break;
> }
>
> pstr = cstr;
> }
>
>
> if(same_str)
> {
> std::cout << "\n'"
> << cstr
> << "' was repeated\n";
> }
> else
> {
> std::cout << "\nno word was repeated\n";
> }
>
> return 0;
> }

As I see it, if the end of std::cin is reach (highly unlikely but
possible
if you associate it with a file buffer, for instance), then the
duplicate
consecutive words were not found (due to your break). For this reason
the boolean same_str is unnecessary, and only serves a documenting
purpose.

This could have been a possible solution:

int main()
{
using namespace std;

string prev, next;
while( cin >> next )
{
if( prev != next )
{
prev = next;
continue;
}
break;
}

if( cin.eof() == false ) //...or if( !cin.eof() ) if you like
{
cout << "Word repeated was " << next;
}
else
{
cout << "No word repeated";
}
cout << "." << endl;
}

previous
next

Re: obtaining multiple values from a function.
We urgently require SAP consultant with 5 year of SAP experience
Re: function name hiding
Re: New UI Toolkit
Re: How to unload a module after I've imported it.
Fundacja Iskierka
Mam Marzenie
Pajacyk
Dzieci Niczyje
Nasze Dzieci