So I have to make a credit card verifier and my god I have never been so frustrated in all my life. The entire source follows, I get 3 runtime errors -
http://i.imgur.com/MXoYNv1.png
that are even less clear than the Visual Studio errors.
What I'm generally trying to do (required in the project) -
Check if the user wants to use assertions (seems to work fine)
Get the card numbers from a file (also works)
Check what brand the card is by taking the first digit and checking if its a 4, 5, 6, or in the case of Amex, 3 and the 2nd digit being 7. (this method works as well)
Also check for the length, greater than or equal to 13, but less than or equal to 16 digits long. (Also seems to work)
Where I get the problems are in the lower methods where all the calculations are done. I realize in SumofDoubleEvenPlace I have no occasions for when length is 15, or 14, or 13, but I'm just trying to get 16 to work for a start...
What this method has to do is, from left to right, double every second number in the card number. With a length of 16, it should skip 16, and start at 15 (which is 14 due to .at using memory locations, 0-15 and not 1-16 like .length)
If the result of any doubling is a two digit number, these two digits are added. So if a card had a 9 in it, that was doubled, you'd get 18. These two added you'd be back to 9. At the end it sums all results together.
For the next method, it does a similar thing except not doubling, simply adding the odd places in the number from right to left (in a 16 length number, position 15, 13, 11, 9, etc)
At the end, this method too sums all the odd places for a particular card together.
Finally, in IsValid, it adds both the final sums together. If this number is divisible by 10 (ends in a zero. For this it has to know ValidNumber's length, and I check that through the thousands place.) the card is VALID, if not, INVALID.
Quite frankly I've rewritten several of these methods over and I'm still throwing the same errors and still having no idea why it's failing so hard.
Some insight would be absolutely fantastic.
Running the example card number 4388576018402626 (which should be invalid)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#include <iostream>
#include <fstream>
#include <string>
#include <assert.h>
#include <iomanip>
#define infile "InData.txt"
#define outfile "OutData.txt"
#define errorfile "ErrorData.txt"
#define outfile2 "Sums.txt"
using namespace std;
ifstream ins; //input file
ofstream ons; //valid output file
ofstream ens; //error file
ofstream sums;
ifstream sumsin;
struct CreditCard
{
string CardNumber;
string CardBrand;
int SumProductEven;
int SumOdd;
int CardLength;
string Validity;
};
CreditCard CreditCards[9999];
bool ProcessingOption()
{
char Affirm;
bool AssertionUse;
cout << "Hello, and welcome to Cameron Labut's credit card verifying program." << endl;
cout << "Would you like to break operations at the first invalid credit card length, or continue with operations and note the invalid card(s)?" << endl;
cout << "If you want to break on invalid cards, type y, if not, type n" << endl;
cin >> Affirm;
if (Affirm == 'y' || Affirm == 'Y')
{
AssertionUse = true;
}
else if (Affirm = 'n' || Affirm == 'N')
{
AssertionUse = false;
}
return(AssertionUse);
}
int GetCardNumbers()
{
int TotalCards;
TotalCards = 0;
ins.open(infile);
if (ins.fail())
{
cout << "File open error, verify file exists." << endl;
}
do
{
ins >> CreditCards[TotalCards].CardNumber;
TotalCards = TotalCards + 1;
} while (!ins.eof());
return(TotalCards);
}
void CreditCardType(int TotalCards)
{
int TotalCards2; //since the silly arrays wont allow variables, I have to keep setting it to zero as a workaround, instead of the more direct way of actually using the number of cards, I have to loop.
TotalCards2 = 0;
do
{
if (CreditCards[TotalCards2].CardNumber.at(0) == '4' )
{
CreditCards[TotalCards2].CardBrand = "Visa";
}
else if (CreditCards[TotalCards2].CardNumber.at(0) == '5')
{
CreditCards[TotalCards2].CardBrand = "MasterCard";
}
else if (CreditCards[TotalCards2].CardNumber.at(0) == 3 && CreditCards[TotalCards2].CardNumber.at(1) == '7')
{
CreditCards[TotalCards2].CardBrand = "American Express";
}
else if (CreditCards[TotalCards2].CardNumber.at(0) == '6')
{
CreditCards[TotalCards2].CardBrand = "Discover";
}
else
{
CreditCards[TotalCards2].CardBrand = "Void";
}
TotalCards2 = TotalCards2 + 1;
} while (TotalCards2 < TotalCards);
}
void GetLengthAssertTrue(int TotalCards)
{
int TotalCards4;
TotalCards4 = 0;
do
{
if (CreditCards[TotalCards4].CardNumber.length() < 13 || CreditCards[TotalCards4].CardNumber.length() > 16)
{
const int i = 0;
assert(i == 1);
}
else if (CreditCards[TotalCards4].CardNumber.length() >= 13 && CreditCards[TotalCards4].CardNumber.length() <= 16)
{
CreditCards[TotalCards4].CardLength = CreditCards[TotalCards4].CardNumber.length();
}
TotalCards4 = TotalCards4 + 1;
} while (TotalCards4 < TotalCards);
}
void GetLengthAssertFalse(int TotalCards)
{
int TotalCards5;
TotalCards5 = 0;
int ErrorCount = 1;
ens.open(errorfile);
do
{
if (CreditCards[TotalCards5].CardNumber.length() < 13 || CreditCards[TotalCards5].CardNumber.length() > 16)
{
cout << "WARNING - A card number is invalid, writing relevant data to ErrorData.txt" << endl;
if (ErrorCount == 1)
{
ens << "Invalid Card Numbers due to Length" << endl;
}
ens << CreditCards[TotalCards5].CardNumber << endl;
CreditCards[TotalCards5].CardLength = CreditCards[TotalCards5].CardNumber.length();
ErrorCount = ErrorCount + 1;
}
else if (CreditCards[TotalCards5].CardNumber.length() >= 13 && CreditCards[TotalCards5].CardNumber.length() <= 16)
{
CreditCards[TotalCards5].CardLength = CreditCards[TotalCards5].CardNumber.length();
}
TotalCards5 = TotalCards5 + 1;
} while (TotalCards5 < TotalCards);
ens.close();
}
| |