English 中文(简体)
缩略语
原标题:cout<<" any key to continue or Ctrl+Z to exit."; how to hide the pressed key?
  • 时间:2012-04-20 03:31:39
  •  标签:
  • c++

在我行程结束时,我已经:

cout<<"

  any key to continue or Ctrl+Z to exit.";

这使得用户能够继续输入数据,或者通过硬盘(CtrlZ退出。 当用户决定继续输入数据时,我谨隐瞒所报的关键。

我不想让压力的钥匙出现在用户施加压力时,不要把任何钥匙留在 lo中。 我如何能够这样做? 我正在使用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<<"

  any key to continue or Ctrl+Z to exit."<<endl<<endl;

        cin>>response;
        cin.ignore();

    }
    while(cin);  //Ctrl+Z to exit
}
最佳回答

有多种办法处理这一问题。

但这取决于你使用什么样的操作系统

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

Option 1: Windows using conio.h

  getch() 

或者说是使用 cur。 h

getch() 

Option 2: In Windows, you can turn off echo for any standard input function with SetConsoleMode(). Code:

#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

或*ix yle

#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
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签