473,480 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ Primer ex 8.3

I get an error, can't find what is the problem:

/* C++ Primer - 4/e
*
* Chapter 8, exercise 8.3
* STATEMENT
* write a function that takes and returns an istream&. the function
should read the stream untill it hits the EOF and should print what it
read to the standard output. reset the stream so that it is valid and
return the stream.
*
*/

#include <iostream>
#include <istream>

istream& stream_game( std::istream& i_strm) /* this is line #14 */
{
while( std::istream >i_strm )
{
std::cout << i_strm;
}

std::istream.clear();

return i_strm;
}
int main()
{
std::istream& i_strm;
stream_game( i_strm );

return 0;

}

/* OUTPUT
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra ex_08.03.cpp

ex_08.03.cpp:14: error: expected constructor, destructor, or type
conversion before '&' token ~/programming/cpp $

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

Aug 25 '07 #1
20 2731
arnuld wrote:
I get an error, can't find what is the problem:

/* C++ Primer - 4/e
*
* Chapter 8, exercise 8.3
* STATEMENT
* write a function that takes and returns an istream&. the function
should read the stream untill it hits the EOF and should print what it
read to the standard output. reset the stream so that it is valid and
return the stream.
*
*/

#include <iostream>
#include <istream>

istream& stream_game( std::istream& i_strm) /* this is line #14 */
maybe that should be std::istream at the beginning of the line.
{
while( std::istream >i_strm )
{
std::cout << i_strm;
}

std::istream.clear();

return i_strm;
}
int main()
{
std::istream& i_strm;
stream_game( i_strm );

return 0;

}

/* OUTPUT
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra ex_08.03.cpp

ex_08.03.cpp:14: error: expected constructor, destructor, or type
conversion before '&' token ~/programming/cpp $

*/

Aug 25 '07 #2
arnuld wrote:
I get an error, can't find what is the problem:

/* C++ Primer - 4/e
*
* Chapter 8, exercise 8.3
* STATEMENT
* write a function that takes and returns an istream&. the function
should read the stream untill it hits the EOF and should print what it
read to the standard output. reset the stream so that it is valid and
return the stream.
*
*/

#include <iostream>
#include <istream>

istream& stream_game( std::istream& i_strm) /* this is line #14 */
{
while( std::istream >i_strm )
{
std::cout << i_strm;
}

std::istream.clear();

return i_strm;
}
int main()
{
std::istream& i_strm;
Also - this is broken - you need to initialize a reference.

stream_game( i_strm );

return 0;

}

/* OUTPUT
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra ex_08.03.cpp

ex_08.03.cpp:14: error: expected constructor, destructor, or type
conversion before '&' token ~/programming/cpp $

*/

Aug 25 '07 #3
On Sat, 25 Aug 2007 20:07:03 +1000, Gianni Mariani wrote:
Also - this is broken - you need to initialize a reference.
i did and I also made some changes but then something else broke:

/* C++ Primer - 4/e
*
* Chapter 8, exercise 8.3
* STATEMENT
* write a function that takes and returns an istream&. the function
should read the stream untill it hits the EOF and should print what it
read to the standard output. reset the stream so that it is valid and
return the stream.
*
*/

#include <iostream>
#include <istream>

std::istream& stream_game( std::istream& i_strm) {
while( std::cin >i_strm ) /* line #16 */
{
std::cout << i_strm;
}

std::istream.clear();

return i_strm;
}
int main()
{
int i;
std::istream& i_strm = (std::cin >i); stream_game( i_strm );

return 0;

}

/* OUTPUT

~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra ex_08.03.cpp
ex_08.03.cpp: In function ‘std::istream& stream_game(std::istream&)’:
ex_08.03.cpp:16: error: no match for ‘operator>>’ in ‘std::cin >>
i_strm’
/usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/istream:131:
note: candidates are: std::basic_istream<_CharT, _Traits>&
std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT,
_Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/istream:135:
note: std::basic_istream<_CharT, _Traits>&
std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT,
_Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
.................................................. .....
.................................................. .... ex_08.03.cpp:21:
error: expected unqualified-id before ‘.’ token ~/programming/cpp $

*/

--
http://arnuld.blogspot.com

Aug 25 '07 #4
Hi!

Gianni Mariani schrieb:
arnuld wrote:
>int main()
{
std::istream& i_strm;

Also - this is broken - you need to initialize a reference.
std::istream is an abstract class. Valid implementations include:
std::ifstream file("hello.txt"); //read file
std::istringstream fromString("Hello World!"); //read string
std::cin //standard input

HTH,
Frank
Aug 25 '07 #5
LR
arnuld wrote:
std::istream& stream_game( std::istream& i_strm) {
while( std::cin >i_strm ) /* line #16 */
You're trying to read a std::istream& from std::cin?

Consider that:

int a;
std::cin >a;

would read an integer from std::cin.

If i_strm is a std::istream&, then what will

std::cin >i_strm;

read from std::cin?
The error message should provide a good clue as to what's wrong here:
ex_08.03.cpp:16: error: no match for ‘operator>>’ in ‘std::cin >>
i_strm’

I suggest that you step back from the assignment and think about this
and why you're unlikely to encounter it:

int main() {

std::istream &a = ??? must be initialized so what could go here?
std::cin >a; // what would this do?

}
Aug 25 '07 #6
arnuld wrote:
>On Sat, 25 Aug 2007 20:07:03 +1000, Gianni Mariani wrote:
>Also - this is broken - you need to initialize a reference.

i did and I also made some changes but then something else broke:
std::istream& stream_game( std::istream& i_strm) {
while( std::cin >i_strm ) /* line #16 */
What do you think that line is supposed to do ?
Aug 25 '07 #7
utab wrote:
....
>
Lets see the comments of the gurus :) I am sure there is a better
implementation, but this should do the trick,
Your code will work but it's going to have problem with large files as
it will use an excessive amount of memory and CPU time. This might work
for you if your constraints comply.

#include <iostream>
#include <istream>
#include <ostream>

std::istream& copy( std::ostream& out, std::istream& in )
{
out << in.rdbuf();
in.clear();
return in;
}

int main()
{
copy( std::cout, std::cin );
return 0;
}

Aug 25 '07 #8
On Aug 26, 1:21 am, Gianni Mariani <gi3nos...@mariani.wswrote:
utab wrote:

...
Lets see the comments of the gurus :) I am sure there is a better
implementation, but this should do the trick,

Your code will work but it's going to have problem with large files as
it will use an excessive amount of memory and CPU time. This might work
for you if your constraints comply.

#include <iostream>
#include <istream>
#include <ostream>

std::istream& copy( std::ostream& out, std::istream& in )
{
out << in.rdbuf();
in.clear();
return in;

}

int main()
{
copy( std::cout, std::cin );
return 0;

}
I see, I told there are better implementations. I was just thinking
for console input, but you are right, there are also file streams
inherited from istream :), good nice, short review for me...

Regards,

Aug 25 '07 #9
On Aug 25, 12:05 pm, arnuld <geek.arn...@gmail.comwrote:
I get an error, can't find what is the problem:

/* C++ Primer - 4/e
*
* Chapter 8, exercise 8.3
* STATEMENT
* write a function that takes and returns an istream&. the function
should read the stream untill it hits the EOF and should print what it
read to the standard output. reset the stream so that it is valid and
return the stream.
*
*/

#include <iostream>
#include <istream>

istream& stream_game( std::istream& i_strm) /* this is line #14 */
{
while( std::istream >i_strm )
{
std::cout << i_strm;
}

std::istream.clear();

return i_strm;

}

int main()
{
std::istream& i_strm;
stream_game( i_strm );

return 0;

}

/* OUTPUT
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra ex_08.03.cpp

ex_08.03.cpp:14: error: expected constructor, destructor, or type
conversion before '&' token ~/programming/cpp $

*/

--http://arnuld.blogspot.com
BTW, I had look at the previous topics and saw that you are trying to
solve almost all the exercises in C++ primer. It is a good book,
however IMHO, isn't it better to find or imagine small projects and
try to solve them and when in trouble consult C++ primer or here. This
is my opinion... I respect your view though.

Aug 25 '07 #10
On Aug 26, 4:47 am, utab <umut.ta...@gmail.comwrote:
BTW, I had look at the previous topics and saw that you are trying to
solve almost all the exercises inC++primer.
umut, Thanks for reading my earlier posts. I thought 90% of them were
useless.
It is a good book,
I think so.

however IMHO, isn't it better to find or imagine small projects and
try to solve them and when in trouble consultC++primeror here. This
is my opinion... I respect your view though.
thanks for the advice and it is exactly the same I wanted to do from
last 1 year. I still want to do it, I want to join with a Project and
start contributing my skills but what C++ skills I have today...
NOTHING......... ZERO C++ skills I have. SO I am doing it the old way:

basic book -advanced book -Project

do you have something better for me?

( In fact I feel sick of myself, 24 hours a day and it is after 16
days I opened the C++ Primer, since AUG 26th and that is why I am
replying so late. I just want to become a Programmer for some C++
based project and there is nothing else I want at now)

Sep 10 '07 #11
LR
arnuld wrote:
( In fact I feel sick of myself, 24 hours a day and it is after 16
days I opened the C++ Primer, since AUG 26th and that is why I am
replying so late. I just want to become a Programmer for some C++
based project and there is nothing else I want at now)

Don't be so hard on yourself. It takes time. And more time. It's also
not something that you can learn in just a few days or weeks.

Don't get discouraged. It can be frustrating, but that can make every
aha! moment even sweeter.

[moved from above]
basic book -advanced book -Project

do you have something better for me?
Thinking about it, yes.

Maybe as you go take off a little, not a lot, of time from the book and
come up with some *small* project for yourself and try it with what you
already know about C++.

Try to vary these so you don't get bored doing the same thing over and
over. But as you gain more experience revisit the early ones later.

Hobby things like electronics or carpentry or modeling or collecting can
offer some interesting and still *small* projects.

Or, you can always do some simple things with programming stuff. Think
about tools. Maybe write something like the wc utility.

Write something silly. I remember some people who always wanted to see
if they could write their code in fewer lines than anyone else.

Google for programming class homework assignments. Teachers struggle to
make those doable.

These little projects don't have to be the ultimate thing that you want.
Keep it simple and then be more ambitious as you learn to do more.

Don't be afraid to experiment. Fiddle around a little bit with what
you've done in these little things. Make the output look different.
Calculate things some other way. Try seeing if you can enter data to
make your code crash.

And have fun. Learning should be fun. It's better that way.

And then, before you get bored, or start thinking that you're really
smart, go back to the book and learn something new.

Repeat as necessary. ;)

Enjoy!

LR
Sep 10 '07 #12
On Sep 10, 6:30 pm, LR <lr...@superlink.netwrote:
arnuld wrote:
Maybe as you go take off a little, not a lot, of time from the
book and come up with some *small* project for yourself and
try it with what you already know about C++.
This is very good advice. It puts what you are learning in
perspective, and avoids the risk that you end up knowing all of
the details of C++ syntax, but are still totally incapable of
writing an actual program.

At this point, I would strongly recommend "Programming Tools in
Pascal", by Kernighan and Plauger. It's fairly dated, and its
examples use Pascal syntax. (But they are basically C
programs---the authors obviously prefer C, and use Pascal in a
very C-like way. But when the book was written, C was still a
small, niche language, little known outside of AT&T, and so not
a reasonable choice for such a book.) It will teach you a lot
about the larger picture when developing real small programs,
just rewriting the examples in C++ should teach you something,
and doing the exercises even more. And I don't know of anything
better which has been written since.

--
James Kanze (GABI Software) email:ja*********@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

Sep 11 '07 #13
On Sep 11, 1:06 pm, James Kanze <james.ka...@gmail.comwrote:
At this point, I would strongly recommend "Programming Tools in
Pascal", by Kernighan and Plauger. It's fairly dated, and its
examples use Pascal syntax. (But they are basically C
programs---the authors obviously prefer C, and use Pascal in a
very C-like way. But when the book was written, C was still a
small, niche language, little known outside of AT&T, and so not
a reasonable choice for such a book.) It will teach you a lot
about the larger picture when developing real small programs,
just rewriting the examples in C++ should teach you something,
and doing the exercises even more. And I don't know of anything
better which has been written since.
since you praise it so much, so I checked it. I think you meant
"Software Tools in Pascal" :
http://www.amazon.com/Software-Tools...9519574&sr=8-1

right ?

Sep 11 '07 #14
On Tue, 11 Sep 2007 08:06:03 +0000, James Kanze wrote:
On Sep 10, 6:30 pm, LR <lr...@superlink.netwrote:
At this point, I would strongly recommend "Programming Tools in
Pascal", by Kernighan and Plauger. It's fairly dated, and its
examples use Pascal syntax. (But they are basically C
programs---the authors obviously prefer C, and use Pascal in a
very C-like way. But when the book was written, C was still a
small, niche language, little known outside of AT&T, and so not
a reasonable choice for such a book.) It will teach you a lot
about the larger picture when developing real small programs,
just rewriting the examples in C++ should teach you something,
and doing the exercises even more. And I don't know of anything
better which has been written since.
James, by the same token, *I* think i can even use this excellent piece of
art: http://www.gigamonkeys.com/book/prac...am-filter.html

what do you say ?

I dont think the book you suggested is available in India :(
--
http://lispmachine.wordpress.com

Sep 11 '07 #15
On Sep 11, 4:07 pm, arnuld <geek.arn...@gmail.comwrote:
On Sep 11, 1:06 pm, James Kanze <james.ka...@gmail.comwrote:
At this point, I would strongly recommend "Programming Tools in
Pascal", by Kernighan and Plauger. It's fairly dated, and its
examples use Pascal syntax. (But they are basically C
programs---the authors obviously prefer C, and use Pascal in a
very C-like way. But when the book was written, C was still a
small, niche language, little known outside of AT&T, and so not
a reasonable choice for such a book.) It will teach you a lot
about the larger picture when developing real small programs,
just rewriting the examples in C++ should teach you something,
and doing the exercises even more. And I don't know of anything
better which has been written since.
since you praise it so much, so I checked it. I think you meant
"Software Tools in Pascal" :http://www.amazon.com/Software-Tools...ghan/dp/020110...
right ?
Yes. I think you'll find it highly readable, and it presents a
lot of small but really useful programs as examples.

--
James Kanze (GABI Software) email:ja*********@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

Sep 11 '07 #16
On Sep 11, 9:36 pm, arnuld <geek.arn...@gmail.comwrote:
On Tue, 11 Sep 2007 08:06:03 +0000, James Kanze wrote:
On Sep 10, 6:30 pm, LR <lr...@superlink.netwrote:
At this point, I would strongly recommend "Programming Tools in
Pascal", by Kernighan and Plauger. It's fairly dated, and its
examples use Pascal syntax. (But they are basically C
programs---the authors obviously prefer C, and use Pascal in a
very C-like way. But when the book was written, C was still a
small, niche language, little known outside of AT&T, and so not
a reasonable choice for such a book.) It will teach you a lot
about the larger picture when developing real small programs,
just rewriting the examples in C++ should teach you something,
and doing the exercises even more. And I don't know of anything
better which has been written since.
James, by the same token, *I* think i can even use this excellent piece of
art:http://www.gigamonkeys.com/book/prac...am-filter.html
what do you say ?
First, it's in Lisp, which has a completely different philosophy
than C or C++. Kernighan and Plauger may have been able to
write C in Pascal, but both C and Pascal are structured,
statically typed procedural languages---in the same family.
Lisp is something else entirely; it may be possible to translate
Lisp into C++, but it won't be natural or idiomatic C++.
I dont think the book you suggested is available in India :(
That might be a problem, if you have to buy your books in India.
It's not particularly recent, and I can easily imagine local
distributors not having a copy. (I'm not sure that's it's even
still in print; if not, you might even have problems finding it
in the United States or Europe.)

--
James Kanze (GABI Software) email:ja*********@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

Sep 11 '07 #17
On Sep 11, 8:49 pm, James Kanze <james.ka...@gmail.comwrote:
First, it's in Lisp, which has a completely different philosophy
than C or C++. Kernighan and Plauger may have been able to
write C in Pascal, but both C and Pascal are structured,
statically typed procedural languages---in the same family.
Lisp is something else entirely; it may be possible to translate
Lisp into C++, but it won't be natural or idiomatic C++.
:(
That might be a problem, if you have to buy your books in India.
It's not particularly recent, and I can easily imagine local
distributors not having a copy. (I'm not sure that's it's even
still in print; if not, you might even have problems finding it
in the United States or Europe.)

It is not available here. I have checked with all major book stores.

In USA, things are different, at Amazon, 31 old 2nd hand copies are
available at just 1 cent :-\

Can you suggest something else ?

Sep 11 '07 #18
On Sep 11, 8:49 pm, James Kanze <james.ka...@gmail.comwrote:
That might be a problem, if you have to buy your books in India.
It's not particularly recent, and I can easily imagine local
distributors not having a copy. (I'm not sure that's it's even
still in print; if not, you might even have problems finding it
This book was never printed in INDIA.

Sep 11 '07 #19

arnuld wrote:

do you have something better for me?
I started out using Deitel&Deitel's "How to Program" series.
They have nice little projects in there. I can also recommend
that you look at the Scot Meyers "Effective C++" series, and
Herb Sutter's "Exceptional C++" series. At least read an item
a day. Also, get into template basics fast, where after Nicolai
Josuttis's "The Standard C++ Template Library" is an absolute
must. This is more or less the way I learned.

I know there are many other good books, for instance
"Accelerated C++" by Koening and Moo, which I've not read
myself as it was released later. Perhaps it has some good
exercises too, as its purpose is for learning C++.

Of course "The C++ Programming Language" by Bjarne Stroustrup
himself, which I would keep as reference.

Once you've read all that, and even perhaps earlier than that,
getting a copy of the standard is also a good idea.

Another book that could probably be worth mentioning, is "Applied C+
+",
which attacks a real world problem. I would really look at Scott
Meyers's
and Herb Sutter's books very early, though.

Regards,

Werner

Sep 11 '07 #20
LR
arnuld wrote:
>
It is not available here. I have checked with all major book stores.

In USA, things are different, at Amazon, 31 old 2nd hand copies are
available at just 1 cent :-\

Can you suggest something else ?
I googled (google is your friend) and found:
http://www.abdulqabiz.com/blog/archi...snt_work_w.php
and then,
http://firstandsecond.com/store/book...ools+in+pascal

A little expensive perhaps, but when you consider that the shipping from
amazon would cost plenty...

LR

Sep 11 '07 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1973
by: Sandman | last post by:
Could anyone give me a tip about a good primer on object oriented php programming - why I should use it, the benefits, the drawbacks, the bugs, the glory? And, should I upgrade to php5 before...
1
340
by: Charles L | last post by:
Does anyone know where I can find errata for Stan Lippman's 'C++ Primer 2nd Edition'? Charles Leng
1
1625
by: hugo | last post by:
what is L&L ,people or book?
5
2314
by: hajime | last post by:
I purchased this book: C++ Primer, 4th Edition ISBN: 0201721481 in Australia. The right side of the last three lines on page 231 are not legible. A stain shows a piece of paper was on that part...
7
1924
by: Lycan. Mao.. | last post by:
Hello, I am a newbie in C++ and I'm in trouble in choosing books, I hope some one who can give me some tips. I'm already know C and a little about Scheme, C#, Python, Lua and so on, and now I want...
2
3787
by: W. Watson | last post by:
Is there a primer out there on these two items? I have the Python tutorial, but would like either a Tkinter tutorial/primer to supplement it, or a primer/tutorial that addresses both. Maybe there's...
2
1757
by: xianwei | last post by:
First, typedef struct pair { Node *parent; Node *child; } Pair; static Pair SeekItem(cosnt Item *pI, const Tree *pTree) { Pair look;
1
2562
by: Kveldulv | last post by:
Hi all, here is the code: http://pastebin.com/m6e74d36b I'm stuck at highfink constructor, last line before #endif. As parameters, I have reference to one parent class and int member of another...
0
1916
by: cincerite | last post by:
Hello , guys , I'm reading C++ Primer 3rd edition recently.I tried to download the errata of it from Stan Lippman's Home Page:http:// staff.develop.com/slip/ ,but it says:We're Sorry, we could not...
0
6911
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6743
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.