Program not working correctly...

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;
}
Try adding some newline characters into your strings to make the output look nicer.

And how is it not working?
Last edited on
So like endl;? Or do you mean '\n'?

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".
Either should work.

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;
}
}


bool checkLength(string creditCardNumber)
{
int length;
length=creditCardNumber.length();
cout << "Length of credit card number is:" << " " << length << endl;
if(length>=13 && length<=16)
return true;
else return false;
}

bool checkType(string creditCardNumber)
{
if (creditCardNumber.at(0)=='4')
{
cout << "Credit card type is Visa" << endl;
return true;
}
else
if(creditCardNumber.at(0)=='5')
{
cout << "Credit card type is Mastercard" << endl;
return true;
}
else
if (creditCardNumber.at(0)=='3' && creditCardNumber.at(1)=='7')
{
cout << "Credit card type is American Express" << endl;
return true;
}
else
if (creditCardNumber.at(0)=='6')
{
cout << "Credit card type is Discover" << endl;
return true;
}
else return false;
}



Update: It worked, I didn't build it again when I edited it. Looks much better now, thank you!
Topic archived. No new replies allowed.