【问题标题】:Does not show the data entry form as I want不显示我想要的数据输入表单
【发布时间】:2013-07-23 09:30: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;
}

【问题讨论】:

    标签: c++


    【解决方案1】:

    尝试使用:

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

    之后

     std::cin >> client.usAge;
    

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

    你也可以试试:

    cin.ignore();
    

    【讨论】:

      【解决方案2】:

      问题是您将 getline() 调用与“>>”混合在一起:

      c++ getline() isn't waiting for input from console when called multiple times

      建议:

      • 用“>>”代替 cin.getline()

      • 同时,用“std::string”替换“char name[15]”。

      • 还可以考虑用一个类代替“struct bc_Detail”。

      【讨论】:

        猜你喜欢
        • 2020-02-29
        • 1970-01-01
        • 1970-01-01
        • 2013-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-30
        • 1970-01-01
        相关资源
        最近更新 更多