0

Excuse my not so concise code, but I wrote this method, and I get three errors. The first one is when I declare int k=0; it says expresion expected and points to int. Two later errors are that k is undeclared.

The question is: is this the problem with how I initialize k or is it the problem with the code itself?

bool checkSum(long cardNumber, int numberOfDigits){
      char str[250];
      sprintf(str, "%ld", cardNumber);
      printf("%s\n", str);



      switch(numberOfDigits)
      {
          case 13:
              int k = 0;
              int sum=0;
              int sum1=0;
              int finalsum;
              int multiplied;
              int sumOfPartition = 0;
              for(int i = 0; i<13; i++)
              {
                  k++;

                  if(k%2==0)
                  {
                      char digit = str[13-i];
                      //converting char to int
                      int number = digit - '0';
                      multiplied = number*2;
                      // if multiplied has two digits
                      if(multiplied>9)
                      {

                          char partitioned[2];
                          sprintf(partitioned, "%d", multiplied);
                          int firstDigitOfPartition = partitioned[0]-'0';
                          int secondDigitOfPartition = partitioned[1]-'0';
                          sumOfPartition = firstDigitOfPartition+secondDigitOfPartition;
                          multiplied = 0;
                      }
                  }
                  else
                  {
                      char digit1 = str[13-i];
                      int number1 = digit1-'0';
                      sum1 = sum1 +number1;


                  }
                 sum =  sum + sumOfPartition+multiplied;
                 sumOfPartition = 0;
              }
              finalsum = sum+sum1;
              if(finalsum%10==0)
              {
                  return true;
              } else{
                  return false;
              }

          case 14:


          case 15:

          case 16:

          default:
              printf("heh");

      }




      return true;
  }
Martynas
  • 309
  • 1
  • 2
  • 9
  • Please try to create a [mcve] that replicates the error you get, and remember the *minimal* part. Also copy-paste the full and complete error output into the question. And add comments in the code you show to mark where you get the errors. – Some programmer dude Oct 20 '19 at 17:51
  • You can’t label declarations. You can only label statements. They’re case labels. You need braces around the block of code, or you need a statement (possibly even a null statement) after the case label and before the declaration. – Jonathan Leffler Oct 20 '19 at 17:53

0 Answers0