-1
OS Xで行っているプロジェクトのパスワードをマスクしようとしています。しかし、文字列を印刷または使用しようとすると何も得られません。しかし、文字列の要素を印刷しようとすると、私はできるようになります。例。 cout < <パスワード; < <パスワード[0]は文字列の最初の文字を出力します。C++文字列を操作してインデックス文字を出力できますが、完全な文字列ではありません
私は間違っていますか?
#include <termios.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
using namespace std;
int getch();
int main(){
char c;
string password;
int i=0;
while((c=getch())!= '\n'){
password[i]=c;
cout << "*";
i++;
}
cout<< password;
return 0;
}
int getch() {
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
は、なぜあなたはその 'getch'機能が必要なのでしょうか?あなたは 'std :: istream :: get'を使用できませんか? –
@GregKikolaエコーをオフにする可能性があります。 –
@Someprogrammerdudeそれは理にかなっています。ありがとう。 –