Beginner Project

Good evening:

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?

Any help is greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
// This code determines validity of credit cards.

#include<iostream>
#include<iomanip>

using namespace std;
int main()

{

int choice;

const int 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.
Last edited on
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.
1
2
3
cout<<"Please enter what type of card you use?\n\n
    << "MasterCard\n"
    <<"Visa\n"<<endl; 


It should be :
1
2
3
cout<<"Please enter what type of card you use?\n\n" // Missing double-quotation mark (")
    << "MasterCard\n"
    <<"Visa\n"<<endl;
Thank you, this is very helpful.

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


system("pause");
return 0;
}
Last edited on
Heres what I did

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
#include<iostream>
#include<iomanip>

using namespace std;

int main()

{

	long long cardNumber;

	int choice, total=0;
	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;

	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;
Last edited on
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.

I appreciate all the help and advice.
Topic archived. No new replies allowed.