Reading a full line of input

后端 未结 5 1838
广开言路
广开言路 2020-11-27 06:27

I\'m trying to store the input that user enters through console. so I need to include the \"enter\" and any white space.

But cin stops giving me input a

5条回答
  •  旧巷少年郎
    2020-11-27 07:19

    For my program, I wrote the following bit of code that reads every single character of input until ctrl+x is pressed. Here's the code:

    char a;
    string b;
    while (a != 24)
    {
    cin.get(a);
    b=b+a;
    }
    cout << b;
    

    For Ctrl+z, enter this:

    char a;
    string b;
    while (a != 26)
    {
    cin.get(a);
    b=b+a;
    }
    cout << b;
    

    I can't confirm that the ctr+z solution works, as I'm on a UNIX machine, and ctrl+z kills the program. It may or may not work for windows, however; You'd have to see for yourself.

提交回复
热议问题