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 9121571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:53:34+00:00 2026-06-17T05:53:34+00:00

I have recently begun C++ and am trying to understand syntax, but I’m having

  • 0

I have recently begun C++ and am trying to understand syntax, but I’m having placement problems. I have to do a credit card validation function that asks for a valid credit card and then I have to print out the type of credit card.

My only issue is with the if-statements for determining the credit card type. I don’t know where exactly to place them and whether or not I’m missing any brackets or such.

The exact chunk that’s problematic for me is:

        if(c.substr(0, 2) == "65" || c.substr(0, 5) == "6011" || c.substr(0, 7) >= "622126" && c.substr(0, 7) <= "622925" || c.substr(0, 4) >= "644" && c.substr(0, 4) <= "649")
            s = "You have a Discover card";
        if(c.substr(0, 2) >= "51" && c.substr(0, 2) <= "55")
            s = "You have a MasterCard card";
        if(c.substr(0, 1) == "4")
            s = "You have a Visa card";
        if(c.substr(0, 2) == "34" || c.substr(0, 2) == "37")
            s = "You have an American Express card"; 

This is my code.

#include <iostream>
#include <string>
using namespace std;

void validateCC();
string checkCC(string, int, bool&);
bool validateCCNum(string);

void main() {
    char again;
    cout << "Validate a credit card number (Y/N)? ";
    cin >> again;
    while (toupper(again) == 'Y') {
        validateCC();
        cout << "Validate a credit card number (Y/N)? ";
        cin >> again;
    }
}

void validateCC() {
    string ccn, msg;
    bool OK;
    int ccLen;
    cout << "Please enter a 15 or 16 digit credit card number."
        << "\n(No spaces or hyphens): ";
    cin >> ccn;
    ccLen = ccn.length();
    msg = checkCC(ccn, ccLen, OK);
    if(!OK)
        cout << msg;
    else
        if(validateCCNum(ccn))
            cout << "Valid credit card number\n";
        else
            cout << "Invalid credit card number\n";
    cout << "\n" << endl;
}

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(c.substr(0, 2) == "65" || c.substr(0, 5) == "6011" || c.substr(0, 7) >= "622126" && c.substr(0, 7) <= "622925" || c.substr(0, 4) >= "644" && c.substr(0, 4) <= "649")
            s = "You have a Discover card";
        if(c.substr(0, 2) >= "51" && c.substr(0, 2) <= "55")
            s = "You have a MasterCard card";
        if(c.substr(0, 1) == "4")
            s = "You have a Visa card";
        if(c.substr(0, 2) == "34" || c.substr(0, 2) == "37")
            s = "You have an American Express card"; 

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

I don’t know whether the problematic chunk belongs where it currently is, or if I place it in the validateCC() section and then add a parameter for it.

Thank you.

  • 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-17T05:53:35+00:00Added an answer on June 17, 2026 at 5:53 am

    Try to change the substrings to int (stringVar >> IntVar;) so you can evaluate the <= >= etc…

        #include <iostream>
    #include <string>
    using namespace std;
    
    void validateCC();
    string checkCC(string, int, bool&);
    bool validateCCNum(string);
    
    void main() {
        char again;
        cout << "Validate a credit card number (Y/N)? ";
        cin >> again;
        while (toupper(again) == 'Y') {
            validateCC();
            cout << "Validate a credit card number (Y/N)? ";
            cin >> again;
        }
    }
    string checkCardType(string c){
    if(atoi(c.substr(0, 2).c_str()) == 65 || atoi(c.substr(0, 5).c_str()) == 6011 || atoi(c.substr(0, 7).c_str()) >= 622126 && atoi(c.substr(0, 7).c_str()) <= 622925 || atoi(c.substr(0, 4).c_str()) >= 644 && atoi(c.substr(0, 4).c_str()) <= 649)
                return "You have a Discover card";
            if(atoi(c.substr(0, 2).c_str()) >= 51 && atoi(c.substr(0, 2).c_str()) <= 55)
                return "You have a MasterCard card";
            if(atoi(c.substr(0, 1).c_str()) == 4)
                return "You have a Visa card";
            if(atoi(c.substr(0, 2).c_str()) == 34 || atoi(c.substr(0, 2).c_str()) == 37)
                return "You have an American Express card"; 
            return "Card not recognized";
    }
    void validateCC() {
        string ccn, msg;
        bool OK;
        int ccLen;
        cout << "Please enter a 15 or 16 digit credit card number."
            << "\n(No spaces or hyphens): ";
        cin >> ccn;
        ccLen = ccn.length();
        msg = checkCC(ccn, ccLen, OK);
        if(!OK)
            cout << msg;
        else
            if(validateCCNum(ccn)){
                cout << "Valid credit card number\n";
                cout << checkCardType(ccn);}
            else
                cout << "Invalid credit card number\n";
        cout << "\n" << endl;
    }
    
    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;
        int 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently begun working on a pre-existing PHP application, and am having a
I've recently begun learning C# but have encountered an annoying problem. Every variable I
I usually script/program using python but have recently begun programming with JavaScript and have
I have recently begun the process of trying to learn Ruby/Rails and have been
I've recently begun using C and I'm having some problems with the simple arithmetic
I have recently begun working with QT and I'm having a small issue. I
I have recently (yesterday, in fact) begun trying to learn MATLAB for a couple
I have recently begun delving into the world that is database programming with C++
I have recently begun using Scala. I've written a DSL in it which can
Recently I have been trying to update some code to utilise the standard C++

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.