How to avoid pressing Enter with getchar() for reading a single character only?

后端 未结 12 1689
长发绾君心
长发绾君心 2020-11-22 00:33

In the next code:

#include 

int main(void) {   
  int c;   
  while ((c=getchar())!= EOF)      
    putchar(c); 
  return 0;
}
12条回答
  •  感情败类
    2020-11-22 01:07

    yes you can do this on windows too, here's the code below, using the conio.h library

    #include  //basic input/output
    #include   //provides non standard getch() function
    using namespace std;
    
    int main()
    {  
      cout << "Password: ";  
      string pass;
      while(true)
      {
                 char ch = getch();    
    
                 if(ch=='\r'){  //when a carriage return is found [enter] key
                 cout << endl << "Your password is: " << pass <

提交回复
热议问题