Press esc to exit

hi.. I make a program that can open the file.
Open file : 


but when user press esc button the program will back to the menu

what must I use, cin , getch, or what.
I don't have any Idea, please help me,

thanks

Qabil
Check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

const int ESCAPE=27;
const int RETURN=13;

bool get(string & s);

int main()
{
    string str;

    get(str);

    cout << str << endl;

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}

bool get(string & s)
{
    char c;
    s="";

    while (true)
    {
        c=getch();

        if (c==ESCAPE)
        {
            s="(input aborted)";
            cout << endl;
            return false;
        }

        if (c==RETURN)
        {
            cout << endl;
            return true;
        }

        cout.put(c) << flush;
        s+=c;
    }
}
Recommending <conio.h> now, are we?

Getting a single, immediate character from the user is a platform-specific kind of thing to do. There are libraries that help minimize multi-platform problems (like NCurses), but for something simple you might as well just use the proper platform-specific code.

I have commented on this several times before.

POSIX: http://www.cplusplus.com/forum/general/5304/page1.html#msg23940
Windows: http://www.cplusplus.com/forum/beginner/5619/#msg25047
(both): http://www.cplusplus.com/forum/articles/7312/#msg33734

Recognizing the ESC key depends on how you get it, but its proper (non-platform-specifically-translated) value is 27.

Hope this helps.
friend, I'm newbie . . . give me simple Idea for solve this problem,

I just want, when user press esc/27 the program back to the main menu
if I using getch() for this case, that mean I can't ask input for the file name will be open,

open file :  _

user can input file name, but when user press esc , program will go to main menu,

A simple idea would be to use cin to get the file name, and enable the user to cancel the operation by entering '*' and hitting enter. '*' can't be part of the name of a file, so it's ok.
Last edited on
Or just enter nothing...

1
2
3
4
5
6
7
8
9
cout << "Please enter a filename to open (or only press enter to cancel)\n> " << flush;
getline( cin, filename );
if (filename.empty())
  {
  cout << "Cancelled.\n";
  return;
  }
ifstream f( filename.c_str() );
...
Duoas wrote:
Or just enter nothing...

Yes, that's an option too, but if you start writing something big like:

"C:\Program Files\...(some directories here)...\data.dat"

and you regret it in the middle of typing, you'll have to hold backspace for a while until you erase all of that. I believe it would be much simpler to check if the last character entered is a '*'.

Of course, you may also add the option to check for an empty string just in case the user immediately hits enter.
Topic archived. No new replies allowed.