Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:15:06+00:00 2026-06-17T13:15:06+00:00

im not sure why the code for the variable known. its always doubling 2

  • 0

im not sure why the code for the variable known. its always doubling 2 times. the code is suppose to display the times processed valid card invalid cards and unknown cards. the known cards are the ones that are american express discover visa and master. im trying to get the count of them but it seems like they are always doubling for some reason

#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<<" valid\n";
        cout<<invalid<<" invalid\n";
        cout<<unknown<< " unknwon\n";
        cout<<proc<<" processed\n";
        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 Type\n"; 
        }
        else
        {

        cout << (ccn)<<" Invalid"<< " credit card number\n";
        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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:15:07+00:00Added an answer on June 17, 2026 at 1:15 pm
     msg =  setCCType(ccn);
            setCCType(ccn);
    

    You are calling the function twice. This counts the known/unknown twice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

not sure why my code wont work, im teaching myself javascript i know php
I'm not sure why my code is not working.. I have code in vba
I am not sure why this code (gives stackoverflow at runtime) compiles: import java.io.*;
Not sure why my .plist looks like this when viwed as source code, and
Not sure if this is against stackoverflow rules as it's not a specific code
Not sure how to do this, here is my code Dim oWeb As New
not sure why I find this so difficult. Here is the html code: <tr>
I'm not sure if the following code can cause redundant calculations, or is it
I am not sure how to implement this C# code into java? Dev is
I am not sure I still get how to unit test legacy code such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.