CC digit matching (part 2)

Valid Input: xxxx-xxxx-xxxx-xxxx or xxxx xxxx xxxx xxxx (x being digit 0-9)
Valid Output: Valid, Invalid
My Output: Nothing (Press any key to continue ...)

So I thought I had my conditions right where it would tell me whether or not my input is valid or not. But I get neither. What am I missing?


#include <iostream>
#include <string>
using namespace std;

bool isvalid(string& strDest, const string& strSource)
{
string strTemp;
strDest = "";
for (int i = 0; i < strSource.length(); i++)
{
if (isdigit(strSource[i]))
{
strTemp += strSource[i];
}
else if (strSource[i] != '-' && strSource[i] != ' ') {
break;
}
}

if (strTemp.length() == 16) {
strDest = strTemp.substr(0, 4) + "-" +
strTemp.substr(4, 4) + "-" +
strTemp.substr(8, 4) + "-" +
strTemp.substr(12, 4);

return true;
}

return false;
}



bool validate(string& strValidated)
{
cout << "Enter credit card number: ";
string strTemp("");
char cget;
for (int i = 0; i < 16; i++) {
cget = cin.get();
if (isdigit(cget)) {
strTemp += cget;
}
}
if (strTemp.length() == 16) {
return true;
}
else{
return false;
}
}


int main()
{
string strSrc[] = {
"1234-5678-9123-4567",
"1111-1111-1111-1111",
"123A-123B-123C-123D",
"2222 2222 2222 2222"

},
strDest[4], strValidate;
string strSource;


if (validate(strValidate))
{
cout <<strValidate << endl;
}

for (int i = 0; i < 1; i++)
{
for (int j = 0; j < strSource.length(); j++)
{
if (isdigit(strSource[i]) && isvalid(strDest[i], strSrc[i]))
{
cout << "Valid" << endl;
}
else
{
cout << "Invalid" << endl;
}
}
}


/*
if (isvalid(strDest[i], strSrc[i]))
{
cout <<"Valid!" << endl;
}
else
{
cout <<"Invalid" <<endl;
}
}
*/
system("pause");
return 0;
}
First of all, please use code tags. Click the "<>" button in the format options and paste your code between the tags. It will make it much easier for us to read your code.

When you validate strValidate, the argument is empty. So the validate() function will always return false.

strSource is also an empty string, so it's length is 0. The inner loop is never entered, and nothing prints.
I think I can figure it out. I guess I'm just getting my variables mixed up.
Topic archived. No new replies allowed.