Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C++ Primer ex 6.20 (http://www.velocityreviews.com/forums/t527234-c-primer-ex-6-20-a.html)

arnuld 08-06-2007 07:59 AM

C++ Primer ex 6.20
 
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;
}



--
http://arnuld.blogspot.com


Barry 08-06-2007 08:29 AM

Re: C++ Primer ex 6.20
 
arnuld wrote:
> 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):


So, what's the point?

Jim Langston 08-06-2007 08:43 AM

Re: C++ Primer ex 6.20
 

"arnuld" <geek.arnuld@gmail.com> wrote in message
news:pan.2007.08.06.07.59.10.348216@gmail.com...
> 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.
> *
> */
>


Personally, I'm not in favor of the break statement when other methods can
be easily used. I would rather do (untested code)

> #include <iostream>
> #include <string>
>
> int main()
> {
> std::string cstr, pstr;
> bool same_str = false;
>

while(! same_str && std::cin >> cstr)
> {
> if(cstr == pstr)
> same_str = true;
> pstr = cstr;
> }
>
>
> if(same_str)
> std::cout << "\n'" << cstr << "' was repeated\n";
> else
> std::cout << "\nno word was repeated\n";
>
> return 0;
> }
>
>
>
> --
> http://arnuld.blogspot.com
>




=?ISO-8859-1?Q?Erik_Wikstr=F6m?= 08-06-2007 08:44 AM

Re: C++ Primer ex 6.20
 
On 2007-08-06 10:29, Barry wrote:
> arnuld wrote:
>> 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):

>
> So, what's the point?


Point of what?

--
Erik Wikström

arnuld 08-06-2007 09:04 AM

Re: C++ Primer ex 6.20
 
> On Aug 6, 1:43 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> Personally, I'm not in favor of the break statement when other methods can
> be easily used. I would rather do (untested code)


> while(! same_str && std::cin >> cstr)


thanks, i will use it :)


-- arnuld
http://arnuld.blogspot.com



int2str@gmail.com 08-06-2007 06:36 PM

Re: C++ Primer ex 6.20
 
On Aug 6, 2:04 am, arnuld <geek.arn...@gmail.com> wrote:
> > On Aug 6, 1:43 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> > Personally, I'm not in favor of the break statement when other methods can
> > be easily used. I would rather do (untested code)
> > while(! same_str && std::cin >> cstr)

>
> thanks, i will use it :)
>
> -- arnuldhttp://arnuld.blogspot.com


I'm not a fan of unnecessary state variables (same_str) which can be
handled by logic in the code instead.

You can also omit the "return 0;" in C++.

So I propose the following version of your code:

#include <iostream>
#include <string>

int main()
{
std::string cstr, pstr;

while( std::cin >> cstr && pstr != cstr )
{
pstr = cstr;
cstr = "";
}

if( cstr != "" )
std::cout << "\n'" << cstr << "' was repeated\n";
else
std::cout << "\nno word was repeated\n";
}

Cheers,
Andre


BobR 08-06-2007 07:28 PM

Re: C++ Primer ex 6.20
 

<int2str@gmail.com> wrote in message...
>
> You can also omit the "return 0;" in C++.


BUT, why would you want to?

Lazy?

--
Bob R
POVrookie



Victor Bazarov 08-06-2007 08:03 PM

Re: C++ Primer ex 6.20
 
BobR wrote:
> <int2str@gmail.com> wrote in message...
>>
>> You can also omit the "return 0;" in C++.

>
> BUT, why would you want to?
>
> Lazy?


<shrug> I don't understand the desire to label somebody right away
with a negative epithet. Every use of a member name in a non-static
member function _can_ be preceded by 'this->'. Do *you* do that?
I don't think so. Why? Lazy? It's unnecessary in *most* cases.

Same here. It's unnecessary to spell out "return 0;". What do you
*gain* (except more typing exercise for your hands) when you spell
it out?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



werasm 08-06-2007 09:23 PM

Re: C++ Primer ex 6.20
 

Jim Langston wrote:

> Personally, I'm not in favor of the break statement when other methods can
> be easily used. I would rather do (untested code)


[snip]

> while(! same_str && std::cin >> cstr)
> > {
> > if(cstr == pstr)
> > same_str = true;
> > pstr = cstr;
> > }



This causes one unnecessary string assignment, even
though the strings already tested equal. IMO this could
be regarded as pessimizing prematurely (for the sake
of not using a break?). It also causes additional an
comparison. If you did not want to break, the least
you could have done is continue after the if( cstr == pstr ).
Matter of style: Using no braces after if is often a source
of error.


James Kanze 08-06-2007 09:30 PM

Re: C++ Primer ex 6.20
 
On Aug 6, 8:36 pm, "int2...@gmail.com" <int2...@gmail.com> wrote:
> On Aug 6, 2:04 am, arnuld <geek.arn...@gmail.com> wrote:


> > > On Aug 6, 1:43 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> > > Personally, I'm not in favor of the break statement when other methods can
> > > be easily used. I would rather do (untested code)
> > > while(! same_str && std::cin >> cstr)


> > thanks, i will use it :)


> I'm not a fan of unnecessary state variables (same_str) which can be
> handled by logic in the code instead.


> You can also omit the "return 0;" in C++.


You can, but not everything that you can do you should do. The
return is good form, and I would generally criticize a
programmer for omitting it.

--
James Kanze (GABI Software) email:james.kanze:gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34




All times are GMT. The time now is 09:49 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.