Thread: Complicated (for me) function HELP!

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    Complicated (for me) function HELP!

    Here is the assignment for a function that will be part of my program that I can't figure out how to start even: (its for a program that validates credit card numbers)

    Function that checks if the last digit is correct, use the following algorithm:
    -In each of the chars with an EVEN index (indices 0,2,4,6, etc)
    -get the int value represented by the char (for example 7
    from '7') // our teacher mentioned in class that we can do
    this by doing something like '2' - '0' but I have no idea how to
    put this into a code.
    -double it (for example 7*2, which is 14)
    -if doubling it has 2 digits, add those digits (ex 1+4, which
    is 5) otherwise, just use the doubled value (if it has one
    digit) // I'm super confused...
    -add that to a sum. //what sum?
    -Add the values of each of the chars (converted to the int it represents) n the ODD index positions to the sum. //again, confused
    -If a sum is a multiple of 10 (use % operator), its correct (return in a return statement 1) otherwise, its incorrect (return 0)

    -I don't want or need anybody to simply hand me the solution, but PLEASE help me out. This seems like the hardest function to do in our program assignment and I can't figure out how to code it.

    This is all I have started for this particular code:
    Code:
    char lastChar;
    if(lastChar == '0' || lastChar == '2' || lastChar == '4' || lastChar == '6' || lastChar == '8')
    -Pathetic, I know. But I'm desperate to finally finish this. Please help me out any way you can!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hint: you should use loops, e.g., a for loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Should that be instead of the "if" statement I have?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Personally, I think that is off track to begin with. I would expect one or two loops. For the two loop version, you would be looping over the chars in even indices with one loop and the chars in odd indices in the other loop.

    Quote Originally Posted by hotshotennis
    -add that to a sum. //what sum?
    A variable that you create.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Ok thank you, I'll try to play around with some loops. If you have any further suggestions, please let me know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bit more complicated problem
    By uural4792 in forum C Programming
    Replies: 4
    Last Post: 10-13-2011, 11:23 PM
  2. Probably way too complicated for me.
    By Necrofear in forum Windows Programming
    Replies: 3
    Last Post: 05-21-2006, 05:41 AM
  3. complicated
    By ZakkWylde969 in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2003, 09:15 AM
  4. Nothing complicated, just...
    By SMurf in forum Windows Programming
    Replies: 1
    Last Post: 09-01-2002, 05:59 PM
  5. Replies: 3
    Last Post: 06-04-2002, 06:11 PM