Thread: cin.get() every line?

  1. #1
    Registered User GigaRoid's Avatar
    Join Date
    Aug 2011
    Location
    Everywhere
    Posts
    23

    cin.get() every line?

    This might be a stupid question, but is there a way to get a program to wait for enter before every string besides doing this?:

    Code:
    cout << "this "; cin.get();
    cout << "is "; cin.get();
    cout << "pretty "; cin.get();
    cout << "sloppy "; cin.get();
    cout << "isn't "; cin.get();
    cout << "it?"; cin.get();
    thanks.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What's wrong with the way you've done it?
    I can't think of a better way (or see what's wrong with that way).
    If you tell us exactly what you're trying to accomplish you might get a more useful answer.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Make a preprocessor macro for it, or a function to wrap it up neatly if you'd rather do that.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    When you have repetition, you should either be using a loop, or a function:

    Loop:
    Code:
    const char *strs[] = {"this ", "is ", "pretty ", "cool ", "isn't ", "it!"};
    for (int i=0; i<sizeof(strs)/sizeof(strs[0]); ++i) {
        cout << strs[i];
        cin.get();
    }
    Function:
    Code:
    void outputAndWait(const char *str) {
        cout << str;
        cin.get();
    }
    
    // elsewhere...
    outputAndWait("this ");
    outputAndWait("is ");
    outputAndWait("pretty ");
    outputAndWait("cool ");
    outputAndWait("isn't ");
    outputAndWait("it!");
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User GigaRoid's Avatar
    Join Date
    Aug 2011
    Location
    Everywhere
    Posts
    23
    @Ooga One reason: neat code =
    @Epy That's a great idea, but I only know enough C++ to ask where the bathroom is.
    @iMalc I obviously could, but it would cut off some of iostream's built in convenience.

    To make things clearer, I would like to find a code that works the same as my code, but without the cin.get() each line, if that's possible.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by GigaRoid View Post
    @iMalc I obviously could, but it would cut off some of iostream's built in convenience.
    How does iMalc's suggestion do that? If you don't want to use flow control structures or functions, then you basically have to do it the way you already did.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    iMalc's suggestions arethe way to go. Since the issue can be solved with a function, there is no reason to consider a macro.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User GigaRoid's Avatar
    Join Date
    Aug 2011
    Location
    Everywhere
    Posts
    23
    Okay, the only reason I said that is for the whole "<< " thing. But I geuss I'm not making super complex programs yet, so that works. Thanks.

  9. #9
    Registered User GigaRoid's Avatar
    Join Date
    Aug 2011
    Location
    Everywhere
    Posts
    23
    Apparently that messes things up in my program. Any other way?
    "For every error, there is something wrong with the method, or something wrong the information. If not you just screwed up."

  10. #10
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Considering that's exactly the same as your original code, the other way would be to fix whatever uninitialized variable/buffer overflow is causing your bug in the first place.

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You could go with C++11 version of the loop ...looking much more simplistic.
    Code:
    	for(auto x:{"You","Shall","Not","Pass" })
    	{
    		cout<<x;
    		cin.get();
    	}

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    another possibility might be something like this:
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    std::ostream& IstreamGetAfterEachWord(std::ostream& os, std::istream& is, std::stringstream ss)
    {
      std::string word;
      while (std::getline(ss, word, ' '))
      {
        os << word;
        is.get();
      }
      return os;
    }
    then you could call it like so:
    Code:
    int main(void)
    {
      std::cout << IstreamGetAfterEachWord(std::cout, std::cin, "this is pretty cool isn't it!") << std::endl;
      return 0;
    }

  13. #13
    Registered User GigaRoid's Avatar
    Join Date
    Aug 2011
    Location
    Everywhere
    Posts
    23
    @manasijamizich: That concept would work best in some sort of game loop. Which is what I'm considering..
    @Elkvis: As was said before, I'm a C++ noob, so I don't really want to go running around with code I haven't learned yet, I could poke someone's eye out!

    So I was thinking of making some sort of game loop that it goes through each line. This might solve the problem. I'm just wondering if there's a simpler way of going line by line in a loop than assigning specific #'s to each of the lines that it goes through by incrementing a variable every cycle... is there?
    "For every error, there is something wrong with the method, or something wrong the information. If not you just screwed up."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 08-07-2011, 09:55 PM
  2. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  3. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  4. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  5. Replies: 1
    Last Post: 05-20-2006, 11:17 PM