Luhn算法C ++的问题

时间:2015-11-20 20:51:46

标签: c++ algorithm compiler-warnings

我是C ++的初学者,并且一直在编写程序来使用Luhn算法验证信用卡号。我收到了来自编译器的12个错误,但即使花了很长时间从更大的数字中减少这些错误,我也无法进一步减少它。我非常感谢任何帮助。非常感谢

(30):error C3867: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::at': non-standard syntax; use '&' to create a pointer to member
(30): error C2109: subscript requires array or pointer type
(34): error C3867: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::at': non-standard syntax; use '&' to create a pointer to member
(34): error C2109: subscript requires array or pointer type
(71): error C2440: 'initializing': cannot convert from 'initializer list' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
(71): note: No constructor could take the source type, or constructor overload resolution was ambiguous
(77): error C2664: 'bool validateCardNumber(const std::string)': cannot convert argument 1 from 'const char' to 'const std::string'
(77): note: No constructor could take the source type, or constructor overload resolution was ambiguous
(79): error C2228: left of '.c_str' must have class/struct/union
(79): note: type is 'const char'
(82): error C2228: left of '.c_str' must have class/struct/union
(82): note: type is 'const char'

我的代码如下:

#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;


int convertChartoInt(const char digit)
{
    int numericalDigit = digit - '0';

    if (numericalDigit < 0 || numericalDigit > 9)
    {
        throw(0); //not a numerical digit - throw an exception
    }
    return (numericalDigit);
}

bool validateCardNumber(const string number) {

    bool cardStatus = false;
    int evenCount = 0, oddCount = 0,
        calculatedCheckDigit = 0, checkDigit = 0;
    int reverseNumber[15];

    try {

        checkDigit = convertChartoInt(number.at[15]);

        for (int i; i < 15; i++) //Reverse digits
        {
            reverseNumber[14 - i] = convertChartoInt(number.at[i]);

            for (int i = 0; i < 15; i = i + 2) //calculate the multiple by 2 of the odd numbers
            {
                int doubledigit = 0;
                doubledigit = 2 * reverseNumber[i];
                if (doubledigit > 9)
                {
                    doubledigit = doubledigit - 9;
                }
                evenCount = evenCount + doubledigit;
            }
        }
        for (int i = 1; i < 15; i = i + 2) //calculate the sum of the even numbers
        {
            oddCount = oddCount + reverseNumber[i];
        }
        calculatedCheckDigit = (evenCount + oddCount) % 10; //calculate the check digit
        cardStatus = (calculatedCheckDigit == checkDigit);
    }
    catch (...) {
        cardStatus = false;
    }
    return(cardStatus);
}

int main()
{
    const string testCard = { "4686006570307405",
                            "4686006570307407",
                            "4093650457937474",
                            "4340423439668810",
                            "1234567812345670",
                            "5509415774265347",
                            "X234567812345670",
                            "4539281167952835",
                            "4532528637398511",
                            "4653549906803760" };

    int numberOfCards = sizeof(testCard) / sizeof(testCard[0]);

    for (int i = 0; i < 10; i++)
    {
        if (validateCardNumber(testCard[i]))
        {
            cout << "Card : " << testCard[i].c_str() << " is valid" << endl;
        }
        else {
            cout << " Card : " << testCard[i].c_str() << " is invalid" << endl;
        }

        }
    }

1 个答案:

答案 0 :(得分:1)

使用at方法

number.at(i)

或索引表示法

number[i]

您目前正在使用

number.at[i]   // incorrect