I am writing a program fro credit card validation. I am testing out some parts of it, but when I run it, the writing all appears on one line and kind of smushed together.
Also, my function that checks the first digit is not working. Any help please? Thanks.
bool checkType(string creditCardNumber)
{
if (creditCardNumber.at(0)=='4')
{
cout << "Credit card type is Visa";
return true;
};
else
if(creditCardNumber.at(0)=='5')
{
cout << "Credit card type is Mastercard";
return true;
};
else
if (creditCardNumber.at(0)=='3' && creditCardNumber.at(1)=='7')
{
cout << "Credit card type is American Express;
return true;
}
else
if (creditCardNumber.at(0)=='6')
{
cout << "Credit card type is Discover;
return true;
}
else return false;
}
Also, I am supposed to check the first digit of the credit card number entered (which is a string) and if it's a 4 then it's "Visa", 5 for "Mastercard", "3" and then "7" for "American Express" and 6 for "Discover". But, even though I am entering valid credit card numbers to test, it returns "invalid credit card type".
Did you mean to have those semicolons after your if statement scopes? I'd think those would cause compiler errors. And can you give an example of your input is? And possibly what a debugger says the string creditCardNumber is when you enter the function?
Input has to be a string of numbers between 13-16 digits. Here is one I tested and got invalid "type" (even though it should be valid): 4310311301120.
Those semicolons weren't supposed to be there, tried again but still didn't work. Also, I added "endl", did I add them to the correct areas? My program so far is below. Thanks
int main()
{
string creditCardNumber;
cout << "Please enter your credit card number (or EXIT to exit):" << endl;
cin >> creditCardNumber;
while(creditCardNumber!="EXIT")
{
if(!checkLength(creditCardNumber))
{cout << "Invalid credit card length"<<endl;}
else
if(!checkType(creditCardNumber))
{cout << "Invalid credit card type" << endl;}
else
{cout << "Credit card number is valid" << endl;}
cout << "Please enter your credit card number(or EXIT to exit):" << endl;;
cin >> creditCardNumber;
}
}