Thread: cin + copy paste

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    cin + copy paste

    Hi,

    When I copy paste a text in the command prompt for one of the "cin" the text will gets in the other too and loops 3 or 4 times.
    Its a text without space so it shouldn't get fragmented.
    I tried to use cin.ignore(); and fflush(stdin); but it won't change a thing.

    What's happening here?

    Edit: apparently its the new line that causes it but wouldn't cin.ignore(); and fflush(stdin); should empty what's in "cin" ?

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        string response;
        char buf[1255];
        while(1)
        {
            cin >> response;
            cout << "string: "<<response << endl;
            cin >> buf;
            cout << "buf: "<< buf << endl;
        }
    
        return 0;
    }
    Last edited by Ducky; 12-16-2012 at 12:09 PM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    fflush(stdin) has undefined behaviour, so there's nothing it should do.

    You haven't shown the input you are providing and the output you are getting. But yes, cin will stop at the newline.
    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"

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks, so what should we use to flush cin?
    For that the second time it is called there would be nothing in it.

    input:
    aaa
    bbb
    ccc
    ddd

    output:
    aaa
    string aaa
    bbb
    buf bbb
    ccc
    string ccc
    ddd
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ducky, you should know that you should never do this kind of thing:

    char buf[1255];
    cin >> buf;

    Reading into char arrays like this can cause a buffer overflow. So use a std::string. Always (that is, if you intend to read a string from the input).
    Also be aware that cin >> stops at whitespace, so spaces in the text will make it stop.
    If you want to fetch a single line, then use std::getline. Again, this will not read the entire input. It will stop at newlines.
    Reading everything pasted (including newlines) is very difficult with standard C++ (because there is no good way to know when there is no more characters in the input buffer and any reads when there is no more input will block). You may have to use workarounds or some native API or library to do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you Elysia, that's very clearly explained.

    Is that on purpose that there is no way to flush it between calls or is it a design flaw?
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can flush the input buffer. But if you do that, then you might risk missing stuff.
    Anyway, to flush the input buffer, normally one would do:

    std::cin.ignore(std::numerical_limits<std::streams ize>::max());
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. API copy and paste a string (Newbie)
    By SorryBadBeat in forum C++ Programming
    Replies: 11
    Last Post: 04-24-2012, 06:01 PM
  2. Custom Copy/Paste
    By C_ntua in forum Windows Programming
    Replies: 5
    Last Post: 06-16-2010, 01:15 PM
  3. Copy and paste text
    By glue21 in forum C++ Programming
    Replies: 11
    Last Post: 10-07-2005, 08:24 AM
  4. copy paste from another app
    By bonkey in forum Windows Programming
    Replies: 1
    Last Post: 09-08-2003, 08:03 AM