1

I need to end my function once the user inputs CTRL-X or '/'. I don't know how to detect/check for a user input of CTRL-X. The task given to me states: 'When you edit the file, you will enter data line by line and when done, you will enter ‘/’ or CTRL + X to exit.'

I have written this code so far. I'm a beginner so pardon my code.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string data;
    int line_num=1;
    ofstream editFile;
    editFile.open("edit_test.txt");

    while(data!="/"){ //some kind of condition ORed with the condition already present in while loop? 
        cout<<line_num<<"> "; //I want to display line numbers every time the user presses enter
        getline(cin, data);
        if(data!="/"){
            editFile<<data<<endl;
            line_num++;
        }
    }

    editFile.close();
    return 0;   
}
Caduchon
  • 4,574
  • 4
  • 26
  • 67
Wahaj Ahmad
  • 53
  • 1
  • 7
  • https://stackoverflow.com/questions/1118957/c-how-to-simulate-an-eof – wally Sep 03 '17 at 21:07
  • `GetAsyncKeystate` for Windows, `CGEventSourceFlagsState` for OSX. As for Linux, I think XInput or NCurses library might do it. You really should post the exact homework requirements if you want us to help.. We can't give you an exact answer because it is homework, but we can still help. We don't know what exactly you're asking.. it doesn't sound right for a school assignment. – Brandon Sep 03 '17 at 21:10
  • @Dúthomhas Ooops, sorry. I meant ncurses of course. :-P ... – user0042 Sep 03 '17 at 21:10
  • 1
    Have you misunderstood your assignment? Ctrl+X is a way to exit an editor, which, presumably, you will be using to create your file. _Reading_ the file with C++ is a different matter. – Dúthomhas Sep 03 '17 at 21:10
  • @Dúthomhas I am using Code::blocks on a windows machine. ctrl-x does not exit the editor, shows '^X' instead. I am pretty sure I do not misunderstand the assignment, I just wish to step out of my while loop once user presses ctrl-x. – Wahaj Ahmad Sep 03 '17 at 21:46
  • @Brandon Assignment is pretty long. i'll try to explain quickly, I need to make an editor, when the user inputs 'edit', a new file opens and allows the user to enter text which saves in text file. When user presses ctrl-x or '/', the edited file saves itself and the whole code is supposed to run again itself after 3 seconds. I just need help on how to detect ctrl-x so I can set a suitable condition. – Wahaj Ahmad Sep 03 '17 at 21:51
  • Oh, well, Ctrl-X is the same as `'\x18'`, so if your input has that in it, user pressed Ctrl-X, and you can stop reading from `cin`. You do not need to bother checking for it when reading from file. – Dúthomhas Sep 03 '17 at 21:57
  • @Dúthomhas It worked! Thanks a lot! – Wahaj Ahmad Sep 03 '17 at 22:10
  • @Dúthomhas post your comment as an answer? – Yuchen Sep 03 '17 at 22:28

1 Answers1

1

CTRL+X is the same as character code 24 (since X is the 24th letter of the alphabet). Barring any system interference*, all you need to do is check to see if character code 24 is in the input.

while (getline( std::cin, s ))
{
  // Find ^C in s
  auto n = s.find( '\x18' );
  if (n != s.npos) s = s.substr( 0, n );

  // process s normally here

  // If ^C was found in s, we're done reading input from user
  if (n != s.npos) break;
}

CTRL+X doesn’t tend to have any special system actions attached to it, so you shouldn’t have any problem getting it as input.

Mwiza
  • 7,780
  • 3
  • 46
  • 42
Dúthomhas
  • 8,200
  • 2
  • 17
  • 39