Anyone who is good at C++

Jump to First Reply
Posted:
in Genius Bar edited January 2014
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>

Comments

  • Reply 1 of 17
    jutusjutus Posts: 272member
    The trick is to use mod. mod is the operation that gives you the remainder after division.



    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
     0Likes 0Dislikes 0Informatives
  • Reply 2 of 17
    airslufairsluf Posts: 1,861member
     0Likes 0Dislikes 0Informatives
  • Reply 3 of 17
    I'm still stuck.



    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?
     0Likes 0Dislikes 0Informatives
  • Reply 4 of 17
    ast3r3xast3r3x Posts: 5,012member
    weird, we are doing something almost exactly like this





    ...getting ready for the national programming thing? question from previous yr?



    [ 10-17-2002: Message edited by: ast3r3x ]</p>
     0Likes 0Dislikes 0Informatives
  • Reply 5 of 17
    airslufairsluf Posts: 1,861member
     0Likes 0Dislikes 0Informatives
  • Reply 6 of 17
    Nope.



    Alright so I've got this:



    #include &lt;iostream&gt;

    #include &lt;string&gt;



    using namespace std;



    int main(void)

    {



    int FirstDigit, SecondDigit, ThirdDigit, FourthDigit, FifthDigit, SixthDigit, SeventhDigit, EighthDigit



    cout&lt;&lt;"Please enter credit card number \\\

    "endl;



    cin&gt;&gt;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?
     0Likes 0Dislikes 0Informatives
  • Reply 7 of 17
    Can someone IM me or something...supermacG9
     0Likes 0Dislikes 0Informatives
  • Reply 8 of 17
    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
     0Likes 0Dislikes 0Informatives
  • Reply 9 of 17
    addon to my post above



    &gt;Write a loop to repeat the whole task until a &gt;correct CCNO is entered,



    um thats a simple mod 10 checksum

    google is your buddy
     0Likes 0Dislikes 0Informatives
  • Reply 9 of 17
    Can you IM me please and tell me more about the array?
     0Likes 0Dislikes 0Informatives
  • Reply 11 of 17
    airslufairsluf Posts: 1,861member
     0Likes 0Dislikes 0Informatives
  • Reply 12 of 17
    amorphamorph Posts: 7,112member
    What is getche()?! I don't recall that from the Standard Library. If it's a compiler-specific extension, it has no place in a general C++ assignment.



    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 &lt;whatever you entered&gt;." 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>
     0Likes 0Dislikes 0Informatives
  • Reply 13 of 17
    ast3r3xast3r3x Posts: 5,012member
    [code] void numcruncher()

    {

    int digit1,digit2,digit3,digit4,digit5,digit6,digit,di git8,ccnum,sum;

    cout&lt;&lt;"Input 8 digit Credit Card Number: "&lt;&lt;endl;

    cin&gt;&gt;ccnum;



    if(ccnum&gt;9999999 && ccnum&lt;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&gt;=100)

    {

    sum-100

    }

    else

    {

    sum=sum

    }



    cout&lt;&lt;sum&lt;&lt;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>
     0Likes 0Dislikes 0Informatives
  • Reply 14 of 17
    That program lookks to be checking for 8 digitsso if I did something like the sum of all numbers, multiply by 3, add 1, w/out hundreds etc. and then use something like if ' ' whatever to define it. Then if it they don't match up it will give you an invalid message. Does this sound like it would work?



    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.
     0Likes 0Dislikes 0Informatives
  • Reply 15 of 17
    [quote]Originally posted by madmax559:

    <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()'
     0Likes 0Dislikes 0Informatives
  • Reply 16 of 17
    dogcowdogcow Posts: 713member
    getche(); captures the keystroke entered. I remember using this with Turbo C++ for DOS. I really don't know if its a standard command or just for that compiler.
     0Likes 0Dislikes 0Informatives
  • Reply 17 of 17
    Thanks man.



    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&lt;=8; lcv++)

    {
     0Likes 0Dislikes 0Informatives
Sign In or Register to comment.