2013-09-23 5 views
-1
fstream file; 
char *email=new char[100]; 
cout<<endl<<"enter email"; 
cin.getline(email,100); 
char *password=new char[100]; 
cout<<endl<<"enter password"; 
cin.getline(password,100); 
file.open("admin.txt",ios::out); 
if(file.good()) 
{ 
    file<<email<<"\n"; 
    file<<password<<"\n"; 
} 
cout<<"contents added"; 

콘솔에는 암호 변수에 저장된 값 하나만 입력 할 수 있습니다. 이유는 무엇입니까?첫 번째 cin.getline 건너 뛰기

+3

* 수백. 질문을하기 전에이 사이트를 조금만 검색하십시오. –

+0

어쨌든 질문을 만든 바로 지금 "관련"을보십시오.) –

+0

왜'std :: string' 대신 동적으로 할당하고 있습니까? –

답변

0

당신은 입력 스트림의 나머지 문자를 무시해야합니다 또한 원시 포인터 대신 std::string를 사용한다

std::cin.getline(email, 100); 
std::cin.ignore(std::numeric_limits<std::streamsize>::max()); 

: * 중복

std::string email; 
std::string password; 

std::cin >> email >> password; 

std::fstream file("admin.txt", std::ios_base::out); 

if (file << email << password) 
{ 
    std::cout << "Content added" << std::endl; 
} 
관련 문제