Question

I am not sure if being in linux makes any different, but i have found online that this:

    cout << "Press Enter to Continue...";
    cin.ignore(numeric_limits<streamsize>::max(),'\n');

Should be sufficient, with #include<limits> in the header of course.

However, it does not seem to work in my program.

It compiles, it runs, but it does not wait.

Basically, i have a menu, which lead to a method call to display a list of people on the screen. I wish to pause that list before the system goes back to the menu.

Here is my code from the menu:

//Manager's Menu
void SelectionPage::showManagerMenu(){
    char option;
    while(true)
    {
        system("clear");                                                //Clears the terminal
        cout<<"             Flat Manager's Menu"<<endl<<endl;           //Display manager's menu
        cout << "Select Manager option" << endl;
        cout << "a) Add a new Flat Member" << endl;
        cout << "b) Delete an existing Flat Member" << endl;
        cout << "c) List Flat Members" << endl;
        cout << "d) Duties" <<endl;
        cout << "e) Resources" <<endl;
        cout << "f) Reset System" <<endl;
        cout << "q) Exit" << endl;
        cout << "make selection: ";
        cin >> option;

        switch(option) {                                                //Takes the user to the corresponding menu or method
            case 'a':   system("clear");
                        memberList.addNewFlatMember(points);
                    break;
            case 'b':   system("clear");
                        memberList.deleteFlatMember();
                    break;
            case 'c':   system("clear");
                        memberList.listFlatMembers();
                    break;
            case 'd':   system("clear");
                        showDutiesMenu();
                    break;
            case 'e':   system("clear");
                        showResourcesMenu();
                    break;
            case 'f':   //reset();
                    break;
            case 'q':   exit(0);
            default:    cout << "Option not recognised: " << option << endl;
                        showManagerMenu();  
        }
    }
}

the option i wish to select is c) which leads to:

//Show the current flat population
void MemberManagement::listFlatMembers(){
    cout<<"             Member List"<<endl<<endl;

    importFlatMembers();                                                //get flat member info from file

    for( int count = 0; count<flatMemberList.size(); count++){
        cout << count+1<<". "<<flatMemberList[count].getName() << endl;
    }

    cout << "Press any key to Continue...";
    cin.ignore(numeric_limits<streamsize>::max(),'\n');

    return;

}

if you want to see any other bit of my code, feel free to let me know.

Thanks in advance.

Was it helpful?

Solution

In *nix, terminals usually wait for a whole line of input before sending anything to the program. Which is why the example code you posted said "Press Enter to Continue...";, and then discarded everything until the next newline.

To avoid that, you should put your terminal in non-canonical mode, which can be done using the POSIX termios(3) functions, as explained in How to check if a key was pressed in Linux?.

OTHER TIPS

Couldn't you just use cin.get() (get one character)?

Here is a snippet from my code. It works in both windows and linux.

#include <iostream>

using std::cout;
using std::cin;

// Clear and pause methods
#ifdef _WIN32
// For windows
void clearConsole() {
    system("cls");
}

void waitForAnyKey() {
    system("pause");
}
#elif __linux__
// For linux
void clearConsole() {
    system("clear");
}

void waitForAnyKey() {
    cout << "Press any key to continue...";
    system("read -s -N 1"); // Continues when pressed a key like windows
}

#endif

int main() {
    cout << "Hello World!\n";
    waitForAnyKey();
    clearConsole();
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top