Hi everyone,
My program is having a small problem with getting input from the user. If I try to add a customer the first time, it runs through every input line and gets the user's input with no problem but every time I try to add a customer to the program after the first add, for some reason it skips the part where it allows the user to enter their name and it goes directly to where it asks for the user's address and then continues on fine. So for every time I try to add a customer after I add the first one, it is skipping the line where it allows the user to enter their name and goes directly to the address input line.
Note: all user input fields in the getdata() method are of string data type except for obviously the account balance. I've used getline to ensure the entire line of text is read and printed out and not just the first word which was a problem I was having before.
Here is the code:
This is baffling me as to why it's skipping the name input line when I try to add a customer for every run of adding a customer except for the first one which works fine.Code:#include <iostream> #include <string> #include <cctype> #include <conio.h> #include <stdlib.h> #include "custclass.h" Customer:: Customer() { // constructor acc_bal = 0.00; } void Customer::getdata() { int pcodelen; int pcode_flag = 0; int i; cout << "\nEnter your name: "; getline(cin, name); cout << "\nEnter your address: "; getline(cin, address); cout << "\nEnter your city: "; getline(cin, city); while (pcode_flag == 0) { cout << "\nEnter your postal code: "; getline(cin, pcode); pcodelen = pcode.length(); if (pcodelen == 6 && isalpha(pcode[0]) && isalpha(pcode[2]) && isalpha(pcode[4]) && isdigit(pcode[1]) && isdigit(pcode[3]) && isdigit(pcode[5])) { pcode_flag = 1; } else { cout << "\nInvalid postal code entry.\n"; } } cout << "\nEnter current account balance: "; cin >> acc_bal; cout << "\n\nCustomer added successfully. Press any key to return to main menu."; getche(); } void Customer::deposit() { float dep; cout << "\nEnter amount to be deposited: "; cin >> dep; acc_bal += dep; cout << "\nNew account balance is: $" << acc_bal; cout << "\n\nPress any key to return to main menu."; getche(); } void Customer::withdraw() { float wdraw; cout << "\nEnter amount to be withdrawn: "; cin >> wdraw; if (wdraw > acc_bal) { cout << "\nCan't withdraw more money than what there is in the account. Press any key to return to main menu."; getche(); } else { acc_bal -= wdraw; cout << "\nNew account balance is: $" << acc_bal; cout << "\n\nPress any key to return to main menu"; getche(); } } void Customer::showdata() { cout << "\nName: " << name; cout << "\nAddress: " << address; cout << "\nCity: " << city; cout << "\nPostal Code: " << pcode; cout << "\nAccount Balance: $" << acc_bal << endl; cout << "\n\nPress any key to return to main menu."; getche(); } int main() { char choice; int flag = 0; int count = 0; int recnum; Customer cust[10]; while (flag == 0) { cout << "\t\t\n\n" << "Main Menu"; cout << "\t\n\n" << "Select by letter:"; cout << "\t\n" << "a - Add a customer."; cout << "\t\n" << "d - Deposit money."; cout << "\t\n" << "w - Withdraw money."; cout << "\t\n" << "s - Show Account Information."; cout << "\t\n" << "q - Quit Application.\n\n"; cout << "\t" << "Choice: "; choice = getche(); switch(choice) { case 'a': system("cls"); if (count >= 10) { cout << "Can't add anymore records. Press any key to return to main menu."; getche(); break; } count += 1; cust[count].getdata(); system("cls"); break; case 'd': system("cls"); cout << "\nEnter customer number: "; cin >> recnum; cust[recnum].deposit(); system("cls"); break; case 'w': system("cls"); cout << "\nEnter customer number: "; cin >> recnum; cust[recnum].withdraw(); system("cls"); break; case 's': system("cls"); cout << "\nEnter customer number: "; cin >> recnum; cust[recnum].showdata(); system("cls"); break; case 'q': flag = 1; break; default: cout << "\nInvalid selection. Press a key to return to main menu."; getche(); } if (flag == 1) { break; } } return 0; }
Does anybody know how to correct this issue?
Also one other problem I'm having is that the program consistently is crashing after adding about 9 or 10 customers and I think that has to do with not flushing the input or output buffers. Is that indeed the problem? If it is, how can I ensure that buffers are always cleared?
All of this working with strings and user input is very picky with C++. Then again, it is for all programming languages.
Thanks.