-1
我想掩蓋項目的密碼,我正在OS X上進行操作。每個被屏蔽的字符都附加到名爲password的字符串。但是,當我嘗試打印或使用字符串時,我什麼也得不到。但是,如果我嘗試打印字符串的一個元素,我能夠。例。 cout < <密碼;將不會打印任何東西,但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這是有道理的。謝謝。 –