Thread: Simple - but not for me!!

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    Simple - but not for me!!

    I am just starting to re-learn c++ having learnt the extreme basics some years ago and seem to be stumbling at the first hurdle.

    I am using Code Blocks and the Ray Lischner book Exploring C++

    Having typed in the very first listing it runs ok except that when I have typed in a list of words I can't figure out how to tell it that I have finished typing in and want the output to appear; the only change I made was to add using namespace std; to save typing std:: all the time; I have very carefully checked the listing and can find no errors :

    Code:
    nclude <algorithm>
    #include <fstream>
    #include <iostream>
    #include <iterator>
    #include <ostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    void read(istream& in, vector<string>& text)
    {
        string line;
        while(getline(in, line))
            text.push_back(line);
    }
    
    int main(int argc, char* argv[])
    {
        vector<string> text;
        if (argc < 2)
            read(cin, text);
        else
        {
            ifstream in(argv[1]);
        if (not in)
            {
                perror(argv[1]);
                return EXIT_FAILURE;
            }
            read(in, text);
        }
            sort(text.begin(), text.end());
    
            copy(text.begin(), text.end(), ostream_iterator<string>(cout, "\n"));
    }

  2. #2
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    You can input ctrl-z. It signifies end-of-line.
    "Derp, derp, derp" - Me

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I assume the missing two characters on the first line of the code you posted are due to a copy/paste mistake.

    In any event ....

    The way to trigger an end-of-file on std::cin is system dependent.

    Under DOS or windows, it is necessary to hit CTRL-Z (Hold CTRL key down and touch the Z key) followed by the enter key.

    Under most unix variants, it is necessary to hit CTRL-D.

    Other systems may do it differently. Even on windows and unix, sometimes it is necessary to do things twice, depending on system/console settings (for example, under windows, it might be necessary to hit CTRL-Z twice).



    BTW: "using namespace std" may seem a handy way of avoiding typing "std::". It has other effects too .... If all you're using it for is to avoid typing "std::", I suggest you are better off typing "std::" everywhere.

    Personally, (almost) the only time I employ "using namespace std" is when responding to beginner questions in forums .... when said beginners have employed "using namespace std". I haven't used it in working code for several years.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Thanks Iceboxes and Grumpy, I remember now!!

    Grumpy yes those missing characters were copy and paste problems.

    May I also ask why you deprecate the use of using namespace; I recall when I started before that it was recommended but if you can tell me why I shouldn't I would be pleased to accept your advice and start off on the right foot.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by JayCee++ View Post
    May I also ask why you deprecate the use of using namespace;
    It pollutes the global namespace. Let's say you have a class called "string." If you use "using namespace std;" you will now have a conflict with the std::string class. It's best to just type out the few extra characters every time.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The type of conflict Elkvis describes is actually ambiguity (two things with similar names, that the compiler interprets as equally viable candidates when using an identifier like string). That prevents code compiling at all. The only solution to such problems is to go back to using the full names (std::string and ::string, respectively in the example Elvis gave) even in cases where it wouldn't normally be necessary.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socket Library Not so simple
    By jbully in forum C Programming
    Replies: 4
    Last Post: 12-23-2010, 09:23 AM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. simple simple design question
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2005, 11:33 PM
  5. Replies: 1
    Last Post: 07-20-2002, 06:33 PM