A
arnuld
it works fine. i am posting it to know your views (please remember, i am
at chapter 6, so i have not encountered stuf like Functions):
/* C++ Primer - 4/e
*
* exercise 6.20
* STATEMENT
* write a programme to rad a sequence of strings from standard
input until either the same word occurs twice in succession or all the
words have been read. use the while loop to read a word at a time. use
the break statement to terminate the loop if awords occurs twice in
succession & print that word or else print the message that no word was
repeated.
*
*/
#include <iostream>
#include <string>
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;
}
at chapter 6, so i have not encountered stuf like Functions):
/* C++ Primer - 4/e
*
* exercise 6.20
* STATEMENT
* write a programme to rad a sequence of strings from standard
input until either the same word occurs twice in succession or all the
words have been read. use the while loop to read a word at a time. use
the break statement to terminate the loop if awords occurs twice in
succession & print that word or else print the message that no word was
repeated.
*
*/
#include <iostream>
#include <string>
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;
}