Here is a small snippet of my code:
int read_prompt() {
string prompt,fname,lname,input;
int id;
cout << "customers> ";
cin >> prompt;
if (prompt.compare("add") == 0) {
cin >> id;
cin >> fname;
cin >> lname;
NewCustomer(id,fname,lname);
} else if (prompt.compare("print")==0) {
print_array();
} else if (prompt.compare("remove")==0) {
cin >> id;
RemoveCustomer(id);
} else if (prompt.compare("quit")==0) {
return 0;
} else {
cout << "Error!" << endl;
}
read_prompt();
return 0;
}
This works just fine as long as the user doesn't input anything unexpected. One of the test cases this program is supposed to pass inputs "add 125mph Daffy Duck," which id ends up being 125, fname equals mph, and lname equals Daffy. After this function receives all three variables it calls itself again and reprompts the user, which Duck then gets entered which "Error!" gets output obviously.
How would I catch this error as the user enters it? Is cin the best function to use in this regard? I did look up getline(), but I'm a little unsure how to implement it.