C++ Primer ex 8.3

A

arnuld

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 $

*/
 
G

Gianni Mariani

arnuld said:
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.
 
G

Gianni Mariani

arnuld said:
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.
 
A

arnuld

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>::eek:perator>>(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>::eek:perator>>(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 $

*/
 
F

Frank Birbacher

Hi!

Gianni said:
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
 
L

LR

arnuld said:
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?

}
 
G

Gianni Mariani

arnuld said:
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 ?
 
U

utab

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

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

#include <iostream>
#include <istream>
#include <string> // to hold what you have read

std::istream& stream_game( std::istream & i_strm) /* this is line #14
*/
{
std::string dump;
char ch; //the character to read
//while( std::istream >> i_strm )
//You should initialize the reference with an object of type
istream, cin
while(i_strm.get(ch)){
dump.push_back(ch);
}
std::cout << dump;
i_strm.clear();

return i_strm;

}

int main()
{
stream_game( std::cin);
return 0;
}



12:05 AM utab@PMA-05-013 ~/documents $ ./a.out
arnuld
arnuld
bye bye arnuld (Press enter and CTRL+D)
arnuld
arnuld
bye bye arnuld
12:05 AM utab@PMA-05-013 ~/documents
 
G

Gianni Mariani

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::eek:stream& out, std::istream& in )
{
out << in.rdbuf();
in.clear();
return in;
}

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

utab

utab wrote:

...




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::eek:stream& 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,
 
U

utab

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.
 
A

arnuld

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)
 
L

LR

arnuld said:
( 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
 
J

James Kanze

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.
 
A

arnuld

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-Tool...5333562?ie=UTF8&s=books&qid=1189519574&sr=8-1

right ?
 
J

James Kanze

James, by the same token, *I* think i can even use this excellent piece of
art:http://www.gigamonkeys.com/book/practical-a-spam-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.)
 
A

arnuld

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

arnuld

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.
 
W

werasm

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C++ Primer ex 5.18 5
C++ Primer ex 3.14 8
C++ Primer ex 4.30 10
C++ Primer ex 7.12 2
C++ Primer ex 9.27 4
C++ Primer ex 4.16 2
C++ Primer ex 7.5 18
C++ Primer ex 7.16 - arguments to main 15

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top