首页 > 解决方案 > 程序忽略用户名,只允许我输入密码?

问题描述

class acc_cr{
protected:
  string username;
  string password;
  int i = 0;
public:
  void create(){
    fstream fout;

    //new username:
    cout<<"Enter Your New Username:\n";
    while(i == 0){
      getline(cin,username);

      stringstream check(username);
      string intermediate;

      // Tokenizing w.r.t. space ' '
      //Extracting the username up to first space
      getline(check, intermediate,' ');

      //Checking if anything is there after first space
      if(getline(check, intermediate,' ')){
        cout<<"Invalid Username-Contains Spaces!\nTry Another Username:\n";
      }

      //checking if this username already exists:
      else if(fstream(username)){
        cout<<"Username Already Exists!\nTry Another Username:\n";
      }
      else{
        fout.open(username,ios::out);//creating new file to store contents
        break;//breaking loop
      }
    }

    //new password:
    cout<<"Enter Your New Password:\n";
    while(i == 0){
      getline(cin,password);

      //Extracting
      stringstream pass(password);
      string a_ps;
      getline(pass,a_ps,' ');

      //checking whether password contains spaces:
      if(getline(pass,a_ps,' ')){
        cout<<"Invalid Password - Constains Spaces\nTry Another Password\n";
      }
      else{
        break;//breaking loop
      }
    }

    //inserting data into csv file:
    fout<<username<<","
        <<password<<"\n";
  }
};

此代码的目的是要求输入用户名和密码并创建一个 csv 文件,其中将存储这些详细信息,并且 csv 文件的名称与您在代码中看到的用户名相同....但是这里的问题是它不询问用户名,而只询问密码......输出是这样的:

Enter your username!
Enter Your Password!
|(blinker)//this is where i am only abled to type

它只允许我输入密码而不是用户名......我不知道为什么......

标签: c++c++11

解决方案


使用提取运算符 >> 输入名称时,输入过程在第一个空格处停止。未处理的字符保留在输入缓冲区中(缓冲区是临时存储键盘输入(或其他序列,如文件)的内存块。)。这包括在数据之后从键盘输入的换行符“n”。如果您随后执行 getline() 操作,它将处理所有内容,直到第一个字符再次行。新行本身被删除。所以你会得到前一个条目的剩余部分,这是一个空行。这就是为什么我建议您最好使用cin,只需通过cin修改getline您的问题就可以解决

class acc_cr{
protected:
  string username;
  string password;
  int i = 0;
public:
  void create(){
    fstream fout;

    //new username:
    cout<<"Enter Your New Username:\n";
    while(i == 0){
      cin>>username;

      stringstream check(username);
      string intermediate;

      // Tokenizing w.r.t. space ' '
      //Extracting the username up to first space
      getline(check, intermediate,' ');

      //Checking if anything is there after first space
      if(getline(check, intermediate,' ')){
        cout<<"Invalid Username-Contains Spaces!\nTry Another Username:\n";
      }

      //checking if this username already exists:
      else if(fstream(username)){
        cout<<"Username Already Exists!\nTry Another Username:\n";
      }
      else{
        fout.open(username,ios::out);//creating new file to store contents
        break;//breaking loop
      }
    }

    //new password:
    cout<<"Enter Your New Password:\n";
    while(i == 0){
      cin>>password;

      //Extracting
      stringstream pass(password);
      string a_ps;
      getline(pass,a_ps,' ');

      //checking whether password contains spaces:
      if(getline(pass,a_ps,' ')){
        cout<<"Invalid Password - Constains Spaces\nTry Another Password\n";
      }
      else{
        break;//breaking loop
      }
    }

    //inserting data into csv file:
    fout<<username<<","
        <<password<<"\n";
  }
};

推荐阅读