2015-11-20 2 views
-1

Я начинаю с C++ и пишу программу для проверки номеров кредитных карт с использованием алгоритма Луна. Я получил 12 ошибок от моего компилятора, однако даже после долгого времени, избавляя эти ошибки от гораздо большего числа, я не могу его уменьшить. Я был бы очень признателен за любую помощь в этом. Большое спасибоПроблемы с алгоритмом Луна C++

(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; 
     } 

     } 
    } 
+0

Вам нужно сделать 'testCard' массив' string'. 'const string testCard [] = ...'. –

ответ

1

Либо использовать метод at

number.at(i) 

или индекс обозначения

number[i] 

вы в настоящее время используется как

number.at[i] // incorrect 
Смежные вопросы