Like Tree1Likes
  • 1 Post By Elysia

cin + copy paste

This is a discussion on cin + copy paste within the C++ Programming forums, part of the General Programming Boards category; Hi, When I copy paste a text in the command prompt for one of the "cin" the text will gets ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747

    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 11:09 AM.
    Compiler MSVC++ 2010 with Code::Blocks.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    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
    Location
    France
    Posts
    747
    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
    Compiler MSVC++ 2010 with Code::Blocks.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    Ducky likes this.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    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?
    Compiler MSVC++ 2010 with Code::Blocks.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21