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";
控制台只允许输入一个保存在密码变量中的值,为什么?
答案 0 :(得分:0)
您需要忽略输入流中的剩余字符:
std::cin.getline(email, 100);
std::cin.ignore( std::numeric_limits<std::streamsize>::max() );
您还应该使用std::string
而不是原始指针:
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;
}