Having problems again, go figure!

Ok so we are asked to check credit card numbers to see if they are valid. The user has to select what type of card it is and then enter their card number. If the mod of a visa card is 0 then it is a good card, and if the mod of a mastercard is 1 then it is good as well. I have wrote a program but after the initial selection by the user it does not stop so the user can put in their card number. I am sure it is something simple that I am missing, or maybe I have it all screwed up, who knows. If someone could please look this over and give me some direction I would be indebted to you.

Thanks

Nate


//Week 5 Homework Assignment 4
//Credit Card Number Verification Program
//Nathan Smith

#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";
cin.getline(visa, cardNumbers);

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";
cin.getline(mastercard, cardNumbers);

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";
}
}
system("pause");
return 0;
}
syntax for integer values in case statements in switch statements would be

case 1: not case(1):


What are you trying to do with the credit card number input? Add up each of the individual digits and then find the remainder after dividing the total by 10?
Yes we have to add all 16 digits and then divide by 10. If it is a visa card then the answer after you divide has to be 0 or it isnt a valid care. For Matercard it has to be 1
cin.getline(visa, cardNumbers);

This isn't working to read in each character entered into the individual elements of the array.

One way to do this is to read in the number as a string (need to #include <string>) and convert each character in the string into an integer in an array. There's also a way to do this using vector ( http://stackoverflow.com/questions/14006389/convert-a-string-into-an-array-of-integers ) Someone else might have another solution to this.

But here's an example of how this would work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
std::string visaString;
int visaNumber[cardNumbers];

std::cout << "Enter Visa number: ";
std::cin >> visaString;

std::cout << "\nArray contains: ";

//note - counter i is an integer because that's a valid index for an array - you're 
//using a char in your for loop and that won't be valid for an array index

for (int i=0; i<cardNumbers; i++)
{
	visaNumber[i] = visaString[i] - '0';
	std::cout << visaNumber[i];
	visaTotal += visaNumber[i];
}

std::cout << "\nThe total of the numbers entered: " << visaTotal;


Enter Visa number: 
Array contains: 4114123456789012
The total of the numbers entered: 58

supper that will help a lot. I like programming, but it can be frustrating at times. I have been in front of this computer for the last three days strait and I am burnt out. I really appreciate the help. It means the world to me. Have a great Easter.
Topic archived. No new replies allowed.