The following code works as intended for the first two getlines and after you input the CC variable it goes into an infinite loop skipping the getlines and not waiting for input.
Here is a sample run:
Enter card holder name (or quit): John Doe
Enter CC number: 1234 1234 1234 1555
// code outputs the other couts but does not wait for input of getline. and reiterates the cout statements. cin.ignore does not seem to help or cin.clear()
Code:
int main(int argc, char* argv[]) {
char CCName[64]; //cardholder name
char CCNumber[16]; //credit card number
char Expiration[8]; //expiration date
float Amount;
while (true) {
/* input processing block */
//gather card holder name
cout << "\nEnter card holder name (or quit): ";
cin.getline(CCName, 64);
//quit command processing
if (strcmp(CCName, "quit") == 1) {
cout << "\nYou successfully terminated the program\n";
//~ close(sockfd); //close socket
exit(EXIT_SUCCESS);
}
//gather credit card number
cout << "\nEnter CC number: ";
cin.getline(CCNumber, 16);
//error checking
if (strlen(CCNumber) != 15 && strlen(CCNumber) != 16) {
cout << "\nCredit card number must be 15 to 16 digits, try again: ";
cin.getline(CCNumber,16);
}
//gather expiration date
cout << "\nEnter expiration: ";
cin.ignore();
cin.getline(Expiration, 7);
//error checking
if (strlen(Expiration) != 7) {
cout << "\nExpiration date format mm/yyyy. Try again: ";
cin.getline(Expiration, 7);
}
//gather amount
cout << "\nEnter amount: ";
cin >> Amount;
}
return 0;
}