mokazan
mokazan

Reputation: 15

Need help on reading a character from a string

My program is supposed to ask the user to input their credit card number and then display whether the numbers are valid or invalid and if valid ,display the card type. I'm having trouble trying to read the first 2 characters in the cardNumber string in order to find out whether the card type is visa,mastercard,american express or discover (4 for visa, 5 for mastercard, 37 for american express, 6 for discover)

#include <iostream>
#include <string>

using namespace std;

int main()
{  
    string cardNumber; //this will carry the number given by the user.
    int sum(0), sumOfDoubleEven(0), sumOfOdd(0), evenDigit(0), oddDigit(0);

    cout << "This program is the Credit Card Number Validator." << endl;
    cout << "This program will check the validity status of your card number" << endl;

    //Main loop
    do
    {
        cout << endl << "Please provide a number to validate or type 'n' to quit ";
        cin >> cardNumber; //gets cardNumber from user
        if(cardNumber.length()>=13 && cardNumber.length()>=16)
        {
            if (cardNumber == "n") break; //exits loop on user request

            //this for loop repeats once for each digit of the cardNumber.
            //digitPosition decrements by one on each pass from last position to first position
            for (int digitPosition=cardNumber.length(); digitPosition>0; digitPosition--)
            {
                if (digitPosition%2 == 0)
                {   //executes the following code if digitPosition is even
                    oddDigit=(int)cardNumber[digitPosition-1]-'0';
                    sumOfOdd=sumOfOdd+oddDigit;
                } else {   //executes the following code if digitPosition is odd.
                    evenDigit=((int)cardNumber[digitPosition-1]-'0')*2;
                    evenDigit=evenDigit%10+evenDigit/10;
                    sumOfDoubleEven=sumOfDoubleEven + evenDigit;
                }
            }

            sum=sumOfOdd+sumOfDoubleEven; //sums the result
            cout << endl <<"The number "<<cardNumber<<" you provided is ";
            if (sum%10==0) //executes if sum is divisible by 10
                cout << "valid" << endl;
            else //executes if sum is not divisible by 10
                cout << "invalid" << endl;
            if(cardNumber.at(0)==4) {
                cout<<"Your Card type is VISA"<<endl;
            } else {
                cout<<"Sorry, you've entered a card number not in the range of 13-16 digits" <<endl;
            } 

        }
    }while (cardNumber != "n");

    return 0;
}

Upvotes: 0

Views: 145

Answers (3)

Alfred  Huang
Alfred Huang

Reputation: 463

if (cardNumber[0] == '4') {
  // VISA
} else if (cardNumber[0] == '5') {
  // mastercard
} else if (cardNumber[0] == '6') {
  // discover
} else if (cardNumber[0] == '3' && cardNumber[1] == '7') {
  // American Express
} else {
  // Invalid card type
}

By the way, your card number length verification condition is not what you expect, it should be

if (cardNumber.length() >= 13 && cardNumber.length() <= 16) {...}

Upvotes: 1

triple_r
triple_r

Reputation: 1047

There are some odd parts in your code, but the problem is you are testing the first digit against a number while your digit is stored in a string. So the test should be if (cardNumber.at(0) == '4') ....

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409136

One of your problems is that you get the first character (at position 0) and compare it to an int. The value of a character, while an integer, is the value the character have in the current encoding. For example, in ASCII encoding (which is the most common) the character '4' has the value 52.

So that's why the comparison cardNumber.at(0)==4 will fail, because 4 is not equal to '4'.

Upvotes: 1

Related Questions