Anyone who is good at C++
Aight, I am tryin to write this program but I don't know how to do it without sub strings.
"Your program is required to prompt for an 8-digit credit card number as ONE integer, not as a string object, or 8 single-digit integer"
1) Sum up digit 1, 2, 3, 6, 7, 8.
2) Multiply the sum by 3 and increase the product by 1.
3) If the result is over 100 only keep the lower two digits.
4) These two digits should be the correct middle two-digits
Write a loop to repeat the whole task until a correct CCNO is entered, or any other additional features you like to add that will add dimensions to this very simple program.
I'm just starting out on this, so I don't really know much on C++.
[ 10-17-2002: Message edited by: TigerWoods99 ]</p>
"Your program is required to prompt for an 8-digit credit card number as ONE integer, not as a string object, or 8 single-digit integer"
1) Sum up digit 1, 2, 3, 6, 7, 8.
2) Multiply the sum by 3 and increase the product by 1.
3) If the result is over 100 only keep the lower two digits.
4) These two digits should be the correct middle two-digits
Write a loop to repeat the whole task until a correct CCNO is entered, or any other additional features you like to add that will add dimensions to this very simple program.
I'm just starting out on this, so I don't really know much on C++.
[ 10-17-2002: Message edited by: TigerWoods99 ]</p>
Comments
e.g. 13 mod 10 = 3
so say you have a credit card number as an int: 12345678
(12 million, 345 thousand, blah blah)
Let's say you want the third digit from left.
12345678 mod 10000000 = 2345678
2345678 mod 1000000 = 345678
345678 div 100000 = 3 (truncate)
3 is the third digit.
that's one way to do it.
The rest of your problem looks pretty easy if you use the above method to extract the nth digit.
good luck,
-j
Like, can I just use an if statement to tell the cpu how to get the middle digits or what? And can I do something like 'isValidCard' etc?
...getting ready for the national programming thing? question from previous yr?
[ 10-17-2002: Message edited by: ast3r3x ]</p>
Alright so I've got this:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
int FirstDigit, SecondDigit, ThirdDigit, FourthDigit, FifthDigit, SixthDigit, SeventhDigit, EighthDigit
cout<<"Please enter credit card number \\\
"endl;
cin>>num;
int Sum=FirstDigit + SecondDigit + etc.
so I can define the sum first, then get the multiple of 3, then get that, then get it w/out 100s using the mod.
Now what?
instead of using so many variables
use an array...
as the problem states you cant use a string
so dont use one
you can accept keyboard input & assign each
key into an array
something like
while (ch != '0x13')
{
a[i] = getche();
}
from then on its a piece of cake
also look at the stl classes if you dont
want to use an array
in which case a simple list would do the trick
>Write a loop to repeat the whole task until a >correct CCNO is entered,
um thats a simple mod 10 checksum
google is your buddy
The most fundamental problem you have, TW, is sketching out the program organization before you write a single line of code. Your followup question makes that clear. You should talk to your teacher about that. You should be able to sketch out the structure of the code first, using boxes and arrows and/or pseudocode and/or whatever strikes your fancy, and fire up the compiler when you actually have a design to work from.
The basic rule of thumb I use is to break a program down into a list of tasks. Anything that would take more than a handful of lines, or that has to be done more than once, ends up in a function (or method).
You can also break the problem down into smaller problems, and solve them one at a time. For instance, write a program that reads in an eight digit integer and prints out "You entered <whatever you entered>." When you've got it working, add something that tells you what the 5th digit of the number is, and so forth. Then you can test each part individually, and assemble your solution incrementally instead of trying to do everything at once.
[ 10-18-2002: Message edited by: Amorph ]</p>
{
int digit1,digit2,digit3,digit4,digit5,digit6,digit,di git8,ccnum,sum;
cout<<"Input 8 digit Credit Card Number: "<<endl;
cin>>ccnum;
if(ccnum>9999999 && ccnum<100000000)
{
digit1=ccnum/10000000;
digit2=(ccnum%10000000)/1000000;
digit3=((ccnum%10000000)%1000000)/100000;
digit4=(((ccnum%10000000)%1000000)%100000)/10000;
digit5=((((ccnum%10000000)%1000000)%100000)%10000)/1000;
digit6=(((((ccnum%10000000)%1000000)%100000)%10000 )%1000)/100;
digit7=((((((ccnum%10000000)%1000000)%100000)%1000 0)%1000)%100)/10;
digit8=((((((ccnum%10000000)%1000000)%100000)%1000 0)%1000)%100)%10;
sum=digit1+digit2+digit3+digit6+digit7+digit8;
sum=(sum*3)+1;
if(sum>=100)
{
sum-100
}
else
{
sum=sum
}
cout<<sum<<endl;
}
else
{
numcruncher();
}
}
void main()
{
numcruncher();
getch();
return0:
} </pre><hr></blockquote>
sorry i just did thta in textedit, no debugger or anything to test, so i'm sure there are some errors
just to let people knwo i did this in like 10min, so the code is a little crude and could be optimized...specially the way i got all the digits, because we did something like thi at school and there was a way that was alot easier and alot less code.
[ 10-18-2002: Message edited by: ast3r3x ]</p>
Something like:
Sum = digit1 + digit2 + digit3 etc...
then something for the middle 2 digits
I could do mod for all of them to get the nth digit however wouldn't that just be writing a lot?
Not trying to get you to do it really, I'm trying to understand everyone's posts. Thanks.
<strong>heres a hint,...
instead of using so many variables
use an array...
as the problem states you cant use a string
so dont use one
you can accept keyboard input & assign each
key into an array
something like
while (ch != '0x13')
{
a[i] = getche();
}
from then on its a piece of cake
also look at the stl classes if you dont
want to use an array
in which case a simple list would do the trick</strong><hr></blockquote>
So basically for that while statement you'd be getting the 13th number there?
What I don't understand is the 'getche()'
I just figured out I can do a for to make a loop in the program so what I did is
for (int lcv=0; lcv<=8; lcv++)
{