# Thread: HELP! With strings ... Luhn Alogorithm

1. ## 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. 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.

3. 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. 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;
}```

5. I understand what you mean...
But how would you multiply these "code.at(x)" values by 2?

6. 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';

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. '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

9. 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;
}
}```

10. I have finally figured it outt :') Happpyyy tearss !!! FEELS GOOODD !!!
But hey Salem thanks for yourr helppp !!!!!