Credit Card Validation

Hello,

I was wondering if you could help me with a program question. We have to ask the user to select either a Visa card type or Mastercard type. Then, we must ask the user to enter the 16 digit card number and determine if the card is valid or not using module 10. If the answer is zero then it is a valid Visa card and if the answer is zero the answer is a valid MasterCard. Please help me with the code. I will post what I have. My problem is that the I have not separated the validation for MasterCard separate from visa.

Here is what I have so far:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{

string type, num;

int sum = 0, i;

cout << "Please enter the type of card: Visa or MasterCard? "/ <<endl;

cin >> type;

cout << "Please enter the 16 digit card number? ";

cin>>num;

for(i = 0; i < 16; i++)
{

sum += (num[i] - 48);

}

sum %= 10;

if(sum == 0 && num.size() == 16) cout << "Its a valid Visa card.\n";

else if(sum == 1 && num.size() == 16) cout << "Its a valid MasterCard.\n";

else cout << "Invalid Card\n";

system ("pause");

return 0;

}
Last edited on
It's Modulo, not Module, and replace your `?` with `.`.
Your whole question is confusing, please clarify on which is 1 and which is 0.
Sorry, Visa is supposed to be equal to zero and MasterCard is supposed to be 1. My question is what am I missing in my code to not make visa and MasterCard run at the same time? As I have it now, they will both run. I am trying to separate them.
Run at the same time? What do you mean? Do you mean that it would report "valid Visa" if you input "Visa" at the beginning? In that case, you'll need to actually use the "type" variable you read that input into.
I am trying to make the program run the visa check if the user selects visa and MasterCard verification of the user selects mastercard. I just do not want the program checking MasterCard and visa validation at the same time.
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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{

string type, num;

int sum = 0, i;

cout << "Please enter the type of card: Visa or MasterCard? "/ <<endl;

cin >> type;

if(type=="Visa"){
cout << "Please enter the 16 digit card number? ";
cin>>num;
for(i = 0; i < 16; i++){
sum += (num[i] - 48);}
sum %= 10;
if(sum == 0 && num.size() == 16) cout << "Its a valid Visa card.\n";
else cout << "Invalid Card\n";
}else if(type=="MasterCard"){
cout << "Please enter the 16 digit card number? ";
cin>>num;
for(i = 0; i < 16; i++){
sum += (num[i] - 48);}
sum %= 10;
if(sum == 1 && num.size() == 16) cout << "Its a valid MasterCard.\n";
else cout << "Invalid Card\n";
}
system ("pause");
return 0;
}
Topic archived. No new replies allowed.