I am working in a beginner program to help me learn C++. The current code I am trying to generate is to determine validity of C++. It is suppose to have the user enter the type of Credit Card (MasterCard or Visa). Then their number. From their the code is suppose to add the digits of the CC number and find the %10. If it is a MC and the %10 comes to 1 it is valid. If it is a Visa and the %10 comes to 0 it is valid. I am assuming this will need a 16 digit array. How do we code to add the digits of the array together?
// This code determines validity of credit cards.
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
int choice;
constint choice1=Mastercard, choice2=Visa;
cout<<"Please enter what type of card you use?\n\n
<< "MasterCard\n"
<<"Visa\n"<<endl;
cin>>choice;
This is where I am a little confused. How does the array start exactly. Should I use an if else statement? Or a for loop?
Again any sight I would really appreciate. Thank you.
if you are using modules 10 your just finding what the number ends in so what if doesn't end in 1 or 0. Also if all you are doing with the number is finding if %10 of it equals 0 or one just use a long long int for taking the card number.
I double checked, I am suppose to add the numbers together and find the remainder if I divide by ten. After I get the card number for example "1234 5678 9123 4567" How do I code adding all these individual digits together?
I updated my code below.
//This code tests the validty of credit cards.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
long long Mastercard;
long long Visa;
int choice;
const int Mastercard_choice = 1, Visa_choice = 2;
cout << "Please enter which type of credit card you use.\n\n"
<< "1. Mastercard\n"
<< "2. Visa" << endl;
cin >> choice;
if (Mastercard_choice)
{
cout << "Please enter your credit card number." << endl;
cin >> Mastercard;
}
else if (Visa)
{
cout << "Please enter credit card number." << endl;
cin >> Visa;
}
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
longlong cardNumber;
int choice, total=0;
constint Mastercard_choice = 1, Visa_choice = 2;
cout << "Please enter which type of credit card you use.\n\n"
<< "1. Mastercard\n"
<< "2. Visa" << endl;
cin >> choice;
while (choice != 1 && choice != 2)//check for valid entry
{
cout << "Your entry was invalid." << endl;
cout << "Please enter which type of credit card you use.\n\n"
<< "1. Mastercard\n"
<< "2. Visa" << endl;
cin >> choice;
}
cout << "Please enter credit card number." << endl;
cin >> cardNumber;
while (cardNumber != 0)//runs till you have added every number
{
total = total + (cardNumber % 10);//gets last digit and adds it to total
cardNumber /= 10;//removes last digit
}
total = total % 10;//taks the sum of the numbers and finds what it ends in
cout << total << endl;
system("pause");
return 0;
Thank you everyone. I signed up for the class I am taking, thinking it was a class to learn Excel and Word haha. I found out it wasn't but had already paid so figured I would give it a go.