不按我的要求显示数据输入表单

Does not show the data entry form as I want

本文关键字:数据 输入 表单 显示 我的      更新时间:2023-10-16

我正在做一个简单的银行系统,在这个系统中我使用account creation方法来创建新帐户
当客户输入时,要创建新帐户,他必须输入自己的个人数据
我知道这个问题既愚蠢又简单。

问题是当一个客户输入他的信息时,应该按如下方式显示数据。

  • 您的名字:(并等待客户端输入)

  • 您的姓氏:(并等待客户端输入)

  • 您的年龄:(并等待客户输入)

  • 您的地址:(并等待客户端输入)

    上面发生的事情很自然,但发生的事情不是这样的。

发生的情况如下。

Your First name: (doesn't waits client inputs then continue ) Your last name: (and waits for client input).
Your age: (waits for client input) .
Your address: (doesn't waits client inputs then continue ) press any key to continue . . .

发生的情况与上图完全相同。

我没有放所有的代码,但我只添加了重要的代码。

// this struct  to store the client information.
struct bc_Detail{
    char cFistName[15];
    char cLastName[15];
    unsigned short usAge;
    char cAddress[64];
};

// create an  account
class Account_Create {
private:
    int nAccountNumber; // account number
    time_t nCreationDate;  // date of join
    int nBalance;        // The amount of money
    bc_Detail client; // instance of bc_Detail to store client info
public:
    void createAccount(); // to create the account
};

// contents of create account method
void Account_Create::createAccount(){   
    std::cout << "Your First name: ";
    std::cin.getline(client.cFistName, 15);
    std::cout << "Your last name: ";
    std::cin.getline(client.cLastName, 15);
    std::cout << "Your age: ";
    std::cin >> client.usAge;
    std::cout << "Your address: ";
    std::cin.getline(client.cAddress, 64);
}

int main(){
     Account_Create create;
     create.createAccount();
   return 0;
}

尝试使用:

std::cin.get();// will eatup the newline

之后

 std::cin >> client.usAge;

cin存储在变量client.usAge中输入的数字,提交条目所需的尾随换行符留在缓冲区中。

您也可以尝试:

cin.ignore();

问题是将对getline()的调用与">>":混合在一起

c++getline()不是';t多次调用时等待控制台输入

建议:

  • 用">>"代替cin.getline()

  • 同时,将"char-name[15]"替换为"std::string"。

  • 还可以考虑用一个类来代替"struct bc_Detail"。