Manipulating ints that are in a string variable type

I'm trying to write a program that uses the Mod 10 check to verify credit card numbers. One of my function headers are called sumOfDoubleEvenPlace which should find the sum of all odd positioned numbers (moving from right to left) multiplied by 2 (if this product exceeds 9 (x>=10) then take the singles/tens digit and add those together (i.e if the number is 8 (8x2 = 16 (16>=10) 1+6 = 7))). The issue I am having is getting the correct sum value after this is done. I believe it is since I am pulling the value from the string then trying to multiply it, its taking the ASCII value of the number then going through the program. I've tried using static_cast<char>(value), converting the actual string to an int using stoi(string) and reassigning the type by setting that number equal to a different variable type. None of these seem to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  int sumOfDoubleEvenPlace(const string& cardNumber){
     int length = cardNumber.size();
     int sum = 0;
    for(int x = length-1; x >= 0; x = x--){
        int test = static_cast<char>(cardNumber.at(x));
        if((test)>=5){
            int temp = test * 2;
            int temp1 = temp%10;
            int temp2 = temp/10;
            if(x!=1 || x!=0){
                
            x--;
            }
            
            
            sum+=(temp1 + temp2);
         
        }else{
            sum +=(test * 2); 
            if(x!=1 || x!=0){
            x--;
            }
        }
    }
    
    return sum;
}
int main()
{
 cout << "Please enter the card number to check if it is valid" << endl;
 string cardNumber;
 cin >> cardNumber;

cout << sumOfDoubleEvenPlace(cardNumber) << endl;
return 0;
}


Right now if I enter 12345 it will return 69 when it should return (5x2 = 10 (1) + 3x2 = 6 (6) + 1x2 = 2 (2) => 1+6+2 = 9. I'm quite sure that the string to int is not my only issue, since going through my code i'm sure there are quite a few logic errors as well.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;

using std::string;
using std::stoi;

int sumOfDoubleEvenPlace(const string);

int main()
{
	string cardNumber;

	cout << "Please enter the card number to check if it is valid: ";
	cin >> cardNumber;

	cout << sumOfDoubleEvenPlace(cardNumber) << endl;

	return 0;
}

int sumOfDoubleEvenPlace(const string aCardNumber)
{
	int length = aCardNumber.size();
	int sum = 0;
	
	for (int index = 0; index < length; index += 2)
		sum += ( 2 * ( aCardNumber[length - 1 - index] - '0' ) ) % 9;
	
	return sum;
}
Please enter the card number to check if it is valid: 54321
9
 
Exit code: 0 (normal program termination)


Oops
Please enter the card number to check if it is valid: 12345
9
 
Exit code: 0 (normal program termination)


This might be an easier way.
Last edited on
closed account (48T7M4Gy)
Duplicate posts are bad
http://www.cplusplus.com/forum/general/179685/
Ye sorry. I wasn't sure if I had placed it in the right sub-forum. I had put it in the beginner at first since I had just started learning C++, but then I took at a look at other posts and they seemed quite simple even to me. That's why I had reposted it to the other sub-forum. My bad won't happen again. Thanks!

Edit: Also this makes a lot of sense. After posting I thought of just subtracting the ASCII value of 0 from it. Once again thanks.

Edit: I also had another question. I just put using namespace std; Should I get in the habit of importing each function like you did? If so why?
Last edited on
closed account (48T7M4Gy)
Thanks Robert234. It doesn't matter which one you post in, both forums achieve the same.

Regarding the namespace thing - it has the purpose among others of avoiding ambiguity that could come in by the very broad using namespace std. it also has the advantage of listing out library functions being used which is a common coding convention. Often the list is made as a comment against the #include file, but that is only a comment.

There are numerous references to it via the dreaded Google.



Oh okay. I had just read somewhere that it was bad practice to use 'using namespace std'. Thanks for the more concise response.
Topic archived. No new replies allowed.