不知道为什么代码被计算两次
Not sure why code is count twice
我不确定为什么变量的代码已知。它总是翻2倍。代码应该显示Times处理过的有效卡、无效卡和未知卡。已知的卡是美国运通卡、discover卡、visa卡和master卡。我试图得到它们的计数,但似乎它们总是因为某种原因翻倍
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void validateCC(string);
string checkCC(string, int, bool&);
bool validateCCNum(string);
string setCCType(string);
int proc = 0, valid = 0 , invalid = 0 , unknown = 0 , known = 0;
void main() {
string cardnum[300];
int ccLen;
ifstream openfile("Sample Credit Card numbers.txt");
if (openfile.is_open())
{
while(!openfile.eof())
{
for (int count = 0; !openfile.eof(); count++)
{
getline(openfile,cardnum[count]);
ccLen = cardnum[count].length();
cout<<"sdfsd";
proc++;
if (ccLen !=0)
{
validateCC(cardnum[count]);
}
}
}
cout<<valid<<" validn";
cout<<invalid<<" invalidn";
cout<<unknown<< " unknwonn";
cout<<proc<<" processedn";
system("Pause");
}
}
void validateCC(string ccn) {
string msg;
bool OK;
int ccLen;
ccLen = ccn.length();
msg = checkCC(ccn, ccLen, OK);
if(!OK)
{
cout <<ccn<< msg << "n";
invalid++;
}
else
{
if(validateCCNum(ccn))
{
msg = setCCType(ccn);
setCCType(ccn);
valid++;
cout<<ccn<<msg << "Card Typen";
}
else
{
cout << (ccn)<<" Invalid"<< " credit card numbern";
invalid++;
}
}
}
string checkCC(string c, int cLen, bool& ccOK) {
string s = "";
ccOK = true;
for(int i=0;i<cLen && ccOK;++i)
ccOK = isdigit(c[i]);
if(ccOK == false) {
s = " Invalid credit card number digits";
} else if(cLen == 15) {
if(c.substr(0, 2) != "34" && c.substr(0, 2) != "37") {
ccOK = false;
s = " Invalid American Express credit card number";
}
} else if(cLen != 16) {
ccOK = false;
s = " Invalid credit card number length";
}
return s;
}
bool validateCCNum(string cc) {
bool flip = true;
int tmp, num = 0, ccLen = cc.length()-1;
for(int ndx=ccLen;ndx>=0;ndx--) {
if (flip)
num += cc[ndx] - '0';
else {
tmp = (cc[ndx] - '0') * 2;
if(tmp <= 9)
num += tmp;
else
num += (1 + (tmp - 10)); // max of 18
}
flip = !flip;
}
return num % 10 == 0;
}
string setCCType(string cc) {
int num = cc[0]-'0';
int num1 =cc[1]-'0';
int num2 = cc[2]-'0';
int num3 = cc[3]-'0';
string cct = " Unknown";
if(cc.length()==15 &&num ==3 &&num1 ==4|| cc.length()==15 &&num ==3 &&num1 ==7)
{
cct = " American Express";
known++;
}
else if(num == '4')
{
cct = " Visa";
known++;
}
else if(num ==5 && num1 ==1 ||num ==5 && num1 ==2|| num ==5 && num1 ==3||num ==5 && num1 ==4|| num ==5 && num1 ==5)
{
cct = " MasterCard";
known++;
}
else if (num == 6 && num1 ==0 && num2 == 1 && num3==1 || num ==6 && num==5)
{
cct = " Discover"; //ignoring other prefixes
known++;
}
else
{
unknown++;
}
return cct;
}
msg = setCCType(ccn);
setCCType(ccn);
你调用了函数两次。这将对known
/unknown
进行两次计数。
相关文章:
- g++的分段错误(在NaN上使用to_string两次时)
- 蛇在C++不会连续转两次
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 我应该如何去缓解两次出现的cin?
- Realloc 两次无法在 Visual Studio 上运行
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- C++析构函数调用两次,堆栈分配的复合对象
- 为什么参数在构造 std::thread 时移动两次
- Qt插槽调用了两次
- 做 std::用相同的unique_ptr移动两次
- C++两次定义相同的函数会导致错误
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 对于优化级别为 0 的 std::vector,析构函数被调用两次
- 使用柯南打包时如何避免列出两次依赖?
- 为什么要执行两次位移((x >> 4)<< 4)?
- 我正在计算的序列(Hailstone)需要打印一次时两次打印
- 试着计算两次之间的差,不要相减
- 不知道为什么代码被计算两次