Thread: HELP! With strings ... Luhn Alogorithm

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    Exclamation HELP! With strings ... Luhn Alogorithm

    Hi everyone... im using C++ Microsoft visual studio and would appreciate if you can help me out here. Ive been trying to do this the whole day ... and canttt figure it outtt PLEASEE HELPPP !! I am a total beginner so sorry if i have very stupid mistakes.



    Code:
    #include <iostream>
    #include <cmath>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        int odd = 0;
        int oddsum = 0;
        int x;
        int n=0;
        int oddx2 = 0;
            
        string code = "7896550100164702";
    
    
        // Reversing the code order
    
    
        code = string(code.rbegin(), code.rend());
        cout << "The reverse of your code is: " << code << endl;
    
        // Finding the sum of the odd positions
            // First multiply each number by 2
            // If the result is less than 10, then sum it
            // If the result is 10 or greater, then add the two digits which make this 2 digit number, e.g. 12 = 1+2
    
    
        cout << "The Odd Positions Of This Code are as follow: " << endl;
        
        for(int x=1; x<code.length(); x+=2)
        {        
            cout << code.at(x) << endl;
            oddsum += code[x] - '0';
        }
        cout << "The sum of the digits in the odd position = " << oddsum << endl;
        oddx2 = oddsum*2;
    
    
        cout << oddx2;
        cout << endl;
    
        for(int x=1; x<code.length(); x+=2)
        {        
    ???????????????????????????????????????????
    
    
    {


    I am very stuck at the red part, i dont know how to write this part where i have to multiply the odd positions by 2 and decide if it is greater than 9.
    I managed to sum the odd positions though that is not useful

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
        string code = "7896550100164702";
    ...
        cout << "The Odd Positions Of This Code are as follow: " << endl;
        for(int x=1; x<code.length(); x+=2)
    If 7 and 9 are the first two odd positions, then the code subscripts would be 0 and 2.

    Most indices in C and C++ start at 0, not 1.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Hey thanks for the response...

    The order is revered to "2074610010556987"
    so it would be 0,4,1 ...

    what do you mean by the code subscripts would be 0 and 2?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well try it
    Code:
        cout << "Starting at 1" << endl;
        for(int x=1; x<code.length(); x+=2)
        {        
            cout << x << "=" << code.at(x) << endl;
        }
        cout << "Starting at 0" << endl;
        for(int x=0; x<code.length(); x+=2)
        {        
            cout << x << "=" << code.at(x) << endl;
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    I understand what you mean...
    But how would you multiply these "code.at(x)" values by 2?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I thought you'd already figured that bit out, when you converted a numeric char to it's numeric value with
    oddsum += code[x] - '0';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Thats to sum the odd positions, i cant seem to multiply each value by 2 ... i tried oddx2 += code[x]*2 but thats totally wrong ... i was told it is something to do with the '0' at the end.
    What does the '0' at the end mean may i ask??

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    '1' (the character) - '0' (the character) = 1 (the integer)

    Subtracting '0' from '0' to '9' gives you the corresponding numeric value.

    So perhaps
    oddx2 += (code[x]-'0') * 2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    ok i fixed it Thanks .... But what my main problem is ... if x +> 5 i have to add the two digits making the number and if x < 5 add to oddsum :/

    can i put a if statement in a if statement ???

    Code:
    for(int x=0; x<code.length(); x+=1)
    	{
    		
    		if (x%2 != 0)
    		{
    			cout << code.at(x) << endl;
    			cout << endl;
    			xx = (code.at(x) - '0') *2;
    			cout << "Odd Position x2 = " << xx;
    			cout << endl;
    		}
    		
    		else 
    		{
    			cout << endl;
    		}
    	}
    Last edited by xcindiix; 11-11-2012 at 01:13 AM.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    I have finally figured it outt :') Happpyyy tearss !!! FEELS GOOODD !!!
    But hey Salem thanks for yourr helppp !!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  2. C: Credit Card Number Validator - Luhn Algorithm
    By Lioneyexp in forum C Programming
    Replies: 1
    Last Post: 02-07-2012, 07:06 PM
  3. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM

Tags for this Thread