Credit Card Verification Program

Hi friends! I'm taking a programming class and we have an assignment to make a credit card verification program.

Here's the prompt to give you some context:

First your program should ask if the credit card is MasterCard or visa card. Then ask the 16 digit credit card number. Then add all the digits in the credit card number and get the modulo 10. If the modulo 10 of the summation of all the digits is zero, then its valid visa card. If the modulo 10 of the summation of all the digits is 1 then its valid MasterCard. Otherwise, the number customer has entered is an invalid card number.

For example, say the card number is 1234 5678 6789 1235 and its visa card. To validate it, first add all the digits

1+2+3+4+ 5+6+7+8+ 6+7+8+9 +1+2+3+5 = 77 then get mod 10 of 77 (77%10 = 7) Since itโ€™s not zero, this is not a valid visa card number

I have the following code below but the program will just keep going after I enter the CC number. I think I'm just overlooking something. Any help you can give is much appreciated ๐Ÿ˜Š

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
  #include <iostream>
using namespace std;
int main()
{
    int choice=0;
    const int cardNumbers=16;
    char visa[cardNumbers],mastercard[cardNumbers];
    int visaTotal=0;
    int visaCount=0;
    int visaModTotal;
    int mastercardTotal=0;
    int mastercardCount=0;
    int mastercardModTotal;
    
    
    
    while(choice !=3)
    {
        if(choice !=3)
        {
            cout<<"Please enter your credit card type.\n\nEnter 1 for Visa\n\nEnter 2 for Mastercard\n\nEnter 3 to end program.\n\n";
            cin>>choice;
        }
        switch(choice)
        {
            case 1: {
                cout<<"Please enter your 16 digit Visa card number without spaces.\n";
                string visaString;
                int visaNumber[cardNumbers];
                
                cout << "Enter Visa number: ";
                cin >> visaString;
                
                cout << "\nArray contains: ";
                for (int i=0; i<cardNumbers; i++)
                {
                    visaNumber[i] = visaString[i] - '0';
                    cout << visaNumber[i];
                    visaTotal += visaNumber[i];
                }
                
                cout << "\nThe total of the numbers entered: " << visaTotal;
                
                for(char visaCount=0; visaCount < cardNumbers; visaCount++)
                {
                    visaTotal+=visa[visaCount];
                    visaModTotal=visaTotal%10;
                }
                if (visaModTotal==0)
                    cout<<"Card is valid\n\n\n";
                else(visaModTotal!=0);
                cout<<"This is not a valid card\n\n\n";
                
                break;
        }
            case 2: {
                cout<<"Please enter your 16 digit MasterCard number without spaces.\n";
                string mastercardString;
                int mastercardNumber[cardNumbers];
                
                cout << "Enter MasterCard number: ";
                cin >> mastercardString;
                
                cout << "\nArray contains: ";
                for (int i=0; i<cardNumbers; i++)
                {
                    mastercardNumber[i] = mastercardString[i] - '0';
                    cout << mastercardNumber[i];
                    mastercardTotal += mastercardNumber[i];
                }
                
                cout << "\nThe total of the numbers entered: " << visaTotal;
                for(char mastercardCount=0; mastercardCount < cardNumbers; mastercardCount++)
                {
                    mastercardTotal+=mastercard[mastercardCount];
                    mastercardModTotal=visaTotal%10;
                }
                if (mastercardModTotal==1)
                    cout<<"Card is valid\n\n\n";
                else(mastercardModTotal!=1);
                cout<<"This is not a valid card\n\n\n";
                
                break;
            }
            case 3: {
                cout<<"Ending Program\n";
                
                break;
            
            default:
                cout<<"Please enter a valid card choice\nEnter 1 for Visa\nEnter 2 for MasterCard\nEnter 3 to end program\n\n\n";
                
                break;
            }
        }
    }
    
    system("pause");
    
    return 0;
}
I added #include <string>, but it works fine when I ran it.
What exactly is the problem?
The program ends when I hit 3.
If you wanted the program to end after checking whether the card is valid or not, right before your break; statement you can say
1
2
3
cout << "This is not a valid card\n\n\n";
choice = 3;
break;
When I enter a credit card number, the program runs but it just keeps looping.
Topic archived. No new replies allowed.