Re: input/output trouble cause segmentation fault

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Eric,


That's not a question regarding gcc ...

is there any reason why you rewrite the examples by hand before you mail
them? In this example, there are at least 3 typing mistakes -- I'm sure
that g++ did NOT compile this code:
>   cout << "Enter some strings: "
a ";" is missing
>             s2,begin(), s2.end(),
this has to be "s2.begin" instead of "s2,begin"
>   printContainer(setinter);
the variable is named "setInter".

In Addition, I think the output you presented in your example is
generated by a different program:
> eric@eric-laptop:~/cppcookbook/ch7$ ./a.out
> Enter a series of strings: a b c d
> {a, b, c, d}

The beginning of the example you mailed us is:
> int main() {
> 
>   cout << "Enter some strings: "

so EITHER the compiler by miracle prints "Enter a series of strings:"
when you ask it to print "Enter some strings: "
OR you mailed us the wrong code!!! 

Beside that, the code seems fine to my (except that it is missing all
error-checking).
On Ubuntu, <Control-D> should be correct to finish the string-reading


Axel




On Tue, Jul 12, 2011 at 10:27:27PM -0700, eric wrote:
> Dear advanced c/g++ programers:
> 
>   A program request me to enter twice input
> that program probably is tested good on visual c++ 7.1 on window xp,
> but my system is g++ on linux(Ubuntu10.04)
> It assume
> Enter some strings: a b c d
> ^Z
> Enter some more strings: d e f g
> ^Z
> Union: a b c d e f g
> Difference: a b c
> Intersection: d
> all these you can get from page 273 and 274 of book(c++ cookbook)
> 
> but my test result is
> -------------------------------
> eric@eric-laptop:~/cppcookbook/ch7$ ./a.out
> Enter a series of strings: a b c d
> ^Z
> [7]+  Stopped                 ./a.out
> eric@eric-laptop:~/cppcookbook/ch7$ ./a.out
> Enter a series of strings: a b c d
> {a, b, c, d}
> Segmentation fault
> -------------------------------------------------------------------
> second case , I used <Enter><Control-D>
> first case, I used <Enter><Control-Z>
> 
> the following in the program I tested, you still can download from
> http://examples.oreilly.com/9780596007614/
> ------------------------------------------------------------------------------------------------
> // Example 7-8.  Unsing set operations
> #include <iostream>
> #include <algorithm>
> #include <string>
> #include <set>
> #include <iterator>
> #include "utils.h"      // For parintContainer(): see 7.10
> 
> using namespace std;
> 
> int main() {
> 
>   cout << "Enter some strings: "
>   istream_iterator<string> start(cin);
>   istream_iterator<string> end;
>   set<string> s1(start, end);
> 
>   cin.clear();
> 
>   cout << "Enter some more strings: ";
>   set<string> s2(++start, end);
> 
>   set<string> setUnion;
>   set<string> setInter;
>   set<string> setDiff;
> 
>   set_union(s1.begin(), s1.end(),
>             s2,begin(), s2.end(),
>             inserter(setUnion, setUnion.begin()));
> 
>   set_difference(s1.begin(), s1.end(),
>                  s2.begin(), s2.end(),
>                  inserter(setDiff, setDiff.begin()));
> 
>   cout << "Union:\n";
>   printContainer(setUnion);
>   cout << "Difference:\n";
>   printContainer(setDiff);
>   cout << "Intersection:\n";
>   printContainer(setinter);
> }
> --------------------------------------------------------------------------------------------------
> utils.h
> -------------------------------
> // Example 7-12. Writing your own printing function
> #include <iostream>
> #include <string>
> #include <algorithm>
> #include <iterator>
> #include <vector>
> 
> using namespace std;
> 
> template<typename C>
> void printContainer(const C& c, char delim = ',', ostream& out = cout)
> {
>    printRange(c.begin(), c.end(), delim, out);
> }
> 
> template<typename Fwd>
> void printRange(Fwd first, Fwd last, char delim = ',', ostream& out =
> cout) {
>    out << "{";
>    while (first != last) {
>      out << *first;
>      if (++first != last)
>         out << delim << ' ';
>    }
>    out << "}" << endl;
> }
> ------------------------------------------------------------------------------------------------------------------
> Ned and thanks your help a lot in advance
> Eric


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux