CC digit matching

Valid input: xxxx xxxx xxxx xxxx OR xxxx-xxxx-xxxx-xxxx (x being digit 0-9)
Correct Output: Only 2 possibilities are Valid or Invalid
My Output: ALWAYS Valid no matter the input
My Question: Why?


#include <iostream>
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;


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

for (int i = 0; i < 1; i++)
{
if (isvalid(strDest[i], strSrc[i]))
{
cout <<"Valid!" << endl;
}
else
{
cout <<"Invalid" <<endl;
}
}
system("pause");
return 0;
}
Your only evaluation is to see if the input is 16 characters long, after that you do some string operations and return true. This should fail if you don't have 16 characters at that point in the program though.
Ok, I think I fixed my conditons, but when I input the credit card number, I get NO output.


#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 << "Invalid" << endl;
}
else
{
cout << "Valid" << endl;
}
}
}


/*
if (isvalid(strDest[i], strSrc[i]))
{
cout <<"Valid!" << endl;
}
else
{
cout <<"Invalid" <<endl;
}
}
*/
system("pause");
return 0;
}
Topic archived. No new replies allowed.