So I am making this program that checks user input for a Customer ID #. Formats that should be allowed are LLLNNNN so L = Letter and N = Number. Examples: ABC1234, BCDE2345. However if I input a wrong format I want the program to loop again to allow the user to try again. If I put in anything, it will still say that it is valid and will not loop for the user to try again. Why is that?
include <iostream>
#include <string>
#include <cctype>
usingnamespace std;
//function prototype
bool TestID (char [], constint);
int main ()
{
constint size = 8;
char CustomerID[size];
bool test;
do
{
//get customer id number
cout << "Valued Customer," << endl;
cout << "Please enter your unique ID verification." << endl;
cout << "ID Format: LLLNNNN (L = Letter, N = Number)" << endl;
cout << "Customer ID: ";
cin.getline(CustomerID, size); //Use this format for capturing input that goes into an array with a determined size
//detemine if the ID is incorrect
test = TestID(CustomerID, size);
if (test = true)
cout << "That is a valid Customer ID." << endl;
elseif (test = false)
{
cout << "That is not the proper format of the " << endl;
cout << "Customer ID. Please try again. " << endl;
cout << "Here is an example ABC1234\n " << endl;
}
} while (test = false);
system ("pause");
return 0;
}
bool TestID (char ID [], constint SIZE)
{
int count; // Loop counter
bool test;
// Test the first three characters for alphabetic letters.
for (count = 0; count < 3; count++) //checks elements 0 through 3
{
if (!isalpha(ID[count]))
{
return test = false;
}
}
// Test the remaining characters for numeric digits.
for (count = 3; count < SIZE - 1; count++) //checks elements 3 through 7
{
if (!isdigit(ID[count]))
return test = false;
}
return test = true;
}
Yup. I totally overlooked the relational operators. Silly me. Thanks, I fixed up my code for the if statements and while loop and it works as expected.