2

私のループの終わりに私は持っています:

cout<<"\n\n  any key to continue or Ctrl+Z to exit.";

これにより、ユーザーはデータの入力を続行したり、を押して終了したりできますCtrlZ。ユーザーがデータの入力を続行することを決定したときに、押されたキーを非表示にしたいのですが。

ユーザーがループにとどまるためにいずれかのキーを押したときに、押されたキーが表示されないようにします。どうやってやるの?Dev-C++を使用しています。私の関数のコードは以下の通りです。

void student::read()
{
    char response;  ofstream OS ("student.dat", ios::app);

    do
    {
        cout<<"Name: ";
        cin>>name;
        cout<<"Age: ";
        cin>>age;
        cout<<"GPA: ";
        cin>>GPA;

        //calling writefile to write into the file student.dat
        student::writefile();
        cout<<"\n\n  any key to continue or Ctrl+Z to exit."<<endl<<endl;

        cin>>response;
        cin.ignore();

    }
    while(cin);  //Ctrl+Z to exit
}
4

1 に答える 1

3

これを処理する方法は複数あります

ただし、使用しているオペレーティングシステムによって異なります

http://opengroup.org/onlinepubs/007908799/xcurses/curses.h.html http://en.wikipedia.org/wiki/Conio.h

オプション1:conio.hを使用するWindows

  getch() 

または*nixの場合はcurses.hを使用します

getch() 

オプション2:Windowsでは、SetConsoleMode()を使用して、標準の入力関数のエコーをオフにできます。コード:

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main()
{
  HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
  DWORD mode = 0;
  GetConsoleMode(hStdin, &mode);
  SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));

  string s;
  getline(cin, s);

  cout << s << endl;
  return 0;
 }//main

または*nixsyle

#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>

using namespace std;

int main()
{
   termios oldt;
   tcgetattr(STDIN_FILENO, &oldt);
   termios newt = oldt;
   newt.c_lflag &= ~ECHO;
   tcsetattr(STDIN_FILENO, TCSANOW, &newt);

   string s;
   getline(cin, s);

   cout << s << endl;
   return 0;
 }//main
于 2012-04-20T03:46:15.143 に答える