none
Debug assertion failed

    Question

  • Keep getting error message and can't figure out how to solve it. Please help.

    Seems it got something to do with the char pointer str. I simply want to input a string and can't figure out what went wrong.

    My code:

    #include<iostream>
    #define NULL 0

    int record[10];

    char* transform(char *str)
    {
     memset(record, 0, sizeof(record));
     int i = 0;
     while (str[i] != '\n')
     {
      record[str[i] - '0']++;
      i++;
     }
     char tmp[30];
     int k = 0;
     for (int j = 0; j < 10; j++)
     {
      if (record[j])
      {
       if (record[j] > 10)
       {
        tmp[k++] = record[j] / 10 + '0';
        tmp[k++] = record[j] % 10 + '0';
       }
       else
        tmp[k++] = record[j] % 10 + '0';
      }
     }
     tmp[k] = '\n';
     return tmp;
    }

    int main()
    {
     char *str;
     std::cin >> str;
     char *inter1 = str, *inter2 = inter1;
     while (str != "-1")
     {
      bool end = false;
      if (strcmp(str, transform(str)) == 0)
      {
       std::cout << str << " is self-inventorying" << std::endl;
       std::cin >> str;
       break;
      }
      for (int i = 0; i < 15; i++)
      {
       if (strcmp(inter2, transform(inter2)) == 0)
       {
        std::cout << str << " is self-inventorying after " << i + 1 << " steps" << std::endl;
        std::cin >> str;
        break;
       }
       if (end)
       {
        std::cin >> str;
        break;
       }
       for (int j = 0; j < 15 - i; j++)
       {
        inter2 = transform(inter2);
        if (strcmp(inter1, inter2) == 0)
        {
         std::cout << str << " enters an inventory loop of length " << j + 1 << std::endl;
         end = true;
         break;
        }
       }
       inter1 = transform(inter1);
       inter2 = inter1;
      }
     }
    }

    Tuesday, February 21, 2017 1:51 PM

Answers

  • Seems that str is still uninitialized. Try this definition:

      char str[333];

    then use ‘cin.getline( str, 333 )’ instead of ‘cin >> str’.

    Also fix the comparison of str with “-1”.

    • Marked as answer by scofx Thursday, February 23, 2017 3:09 PM
    Tuesday, February 21, 2017 5:36 PM

All replies

  • Seems that str is still uninitialized. Try this definition:

      char str[333];

    then use ‘cin.getline( str, 333 )’ instead of ‘cin >> str’.

    Also fix the comparison of str with “-1”.

    • Marked as answer by scofx Thursday, February 23, 2017 3:09 PM
    Tuesday, February 21, 2017 5:36 PM
  • Hi scofx,

    I'm glad your issue has been resolved by the help of Viorel's reply, and thank you for marking the answer.

    I'm going to close the thread. But please feel free to let us know if you need any other help at any time.

    Best regards,

    Fletch


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 24, 2017 1:27 AM