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

The console only allows to enter one value which is saved in password variable , why?

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 に答える