2

I am coding a banking app for university and i have stumbled upon a problem. Let's take for example a method of my class Customer.

void Customer::create_customer_data() {
    cout << "Client' Address: ";
    getline(cin, clientAddress);
    cin.ignore();
    cout << "Client's birth date: ";
    getline(cin, clientBirthDate);
    cin.ignore();
    cout << "Client's telephone number: ";
    getline(cin, clientTelephoneNumber);
    cin.ignore();
    cin.clear(); }

In the main function, i have a switch statement which handles the user choice.

int main() {
int choice, _globalClientNumber = 0;
Customer a(_globalClientNumber);
cout << "Welcome to bank manager 1.0!" << endl;

do {
    cout << "Main Menu"<< endl;
    cout << "Create a new customer (1)" << endl;
    cout << "Create a new account (2)" << endl;
    cout << "Cash money into account (3)" << endl;
    cout << "Cash money out of account (4)" << endl;
    cout << "Transfer money between two accounts (5)" << endl;
    cout << "See current status of a customer and its accounts (6)" << endl;
    cout << "End Application (0)" << endl;

    cout << "Choice: ";
    cin >> choice;

    switch (choice) {
        case 1:
            a.create_customer_data();
            break;
        case 2:
            a.create_new_account();
            break;
        case 3:
            a.cash_in();
            break;
        case 4:
            a.cash_out();
            break;
        case 5:
            a.transfer_money();
            break;
        case 6:
            a.print();
            break;
        default:
            break;
    }

    cout << endl;
}
while (choice != 0);

return 0; }

The problem that i keep having is that the values that are written on the screen by the create_customer_data method are being processed by the do-while loop. So if the clientTelephoneNumber in the create_customer_data does not end in 0, the main menu is shown twice by the do-while loop. I would appreciate it if someone could tell me where my mistake is.

Edit: Shortly said: the choice variable gets overridden and the do-while loop gets executed once more resulting in double printing the menu.

cookiefier
  • 29
  • 1
  • 8

1 Answers1

1

i would say, that your cin needs to be flushed.

the answer can be found here: How do I flush the cin buffer?

Community
  • 1
  • 1
Zaiborg
  • 2,492
  • 19
  • 28
  • This question might also be related http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input – Codor Jan 12 '15 at 12:29
  • indeed. however it comes to the same conclusion – Zaiborg Jan 12 '15 at 12:40