-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";

控制台只允许输入一个保存在密码变量中的值,为什么?

4

1 回答 1

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;
}
于 2013-09-23T12:29:58.900 回答