0

I'm doing a simple banking system, in this system I doing account creation method to create new account.
When the client enter, to create new account, he must enter his personal data.
I know the problem is stupid and simple at the same time.

The problem is when a client entering his information, Supposed to show the data as follows.

  • Your First name: (and waits for client input)

  • Your last name: ( andwaits for client input)

  • Your age: ( and waits for client input)

  • Your address: (and waits for client input)

    Natural that occurs above, but what happens not like that.

That happens is the following.

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 . . .

What happens is exactly the same as the previous figure.

I did not put all the codes, but I added the important codes only.

// 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;
}
Community
  • 1
  • 1
Lion King
  • 32,851
  • 25
  • 81
  • 143

2 Answers2

1

Try using :

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

after

 std::cin >> client.usAge;

cin stores the number entered in the variable client.usAge, and the trailing newline character(s) needed to submit the entry is left in the buffer.

You can also try :

cin.ignore();
0decimal0
  • 3,884
  • 2
  • 24
  • 39
0

The problem is that you're mixing calls to getline() with ">>":

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

SUGGESTIONS:

  • Substitute ">>" for cin.getline()

  • At the same time, substitute "std::string" for "char name[15]".

  • Also consider substituting a class for "struct bc_Detail".

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190