What can cause a Debug Assertion Error. Specifically, in my code

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am working on writing a compiler for a school project, and this assignment calls for me to print the tokens of a text file to the console window. I want to make it clear I do not want my homework done for me.

I have been working with this stupid function that iterates through the file, and concatenates a char or a c-string value (my tutor was vague on this part of his instructions...) to a string variable named "token." I can work through the first line of the file fine, which is "main()", but when I try and access the next line I get 1 of two errors. The first is a string subscript out of range error, though I think this was because I was trying to access part of a string array that didn't exist. The most prevalent error I am getting is a debug assertion error:

Debug Assertion Failed Final.exe File:f:\dd\vctools\crt_bld\self_x86\crt\src\isctype.c Expression: (unsigned)(c+1) <= 256

I have included my function and its associated header file. Nothing is going on in main except a function call. If at all possible, could you see what I am failing to see. I realize that my code structure is poor, I won't lie (I am in school after all). So, any comments, criticism, and suggestions are very welcome. And always,thank you for any and all time.

.CPP File (As it is now)

    #include  
    #include <string>

    using namespace std; 

    void tokenWork::openFile()
    {
        fileName = "test.txt"; 

        source.open(fileName);

        if(!source.is_open())
        {
       cout << "Cannot find file " << endl; 
        }
    }

    void tokenWork::traveseLine()
    {

    pos = 0; 
    while (!source.eof())
    {  
          getline(source,myLine); 
          int length = myLine.length(); 
          letters = new char[length];
          myLine.copy(letters,length); 

          c = letters[pos]; 

          if (isalpha(c))
             token = token + myLine[pos]; 
          else if (isdigit(c))
             token = token + letters; 
          else 
          {
             cout << token << endl; 
             token = ""; 
          }

          if (c == '{' || c == '}' || c == '+' || c == '=' || myLine[pos] == '(' || c == ')' || c == ';')
                cout << myLine[pos] << endl;  
          c = letters[pos++]; 
    }
}

.h file

    #ifndef H_LEX
    #define H_LEX

    #include <string> 
    #include  
    #include  

    using namespace std; 

    class tokenWork
    {
    public: 
    std::string fileName; 
    std::string myLine; 
    std::string token; 

    int pos; 
    int
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

That debug assertion error originates from the call to std::isalpha/isdigit when you pass it an argument with a value > 255, which is the maximum value the type char (which you should probably be using instead of int here) can hold. I can't tell you the exact origin though as you don't provide the source file, but you should be able to figure it out yourself quite easily: run the program under the debugger, it will break at the assert. Move up in the call stack and inspect the values of your variables, that should give you a clue.

a couple of tips:

  • you're using c++ here, no need for using raw char arrays. Use std::string instead.
  • don't put using namepsace std; in header files: everything incuding that file will import the entire std namespace
  • if you include in a header, you don't have t include it again in the source file
  • learn to use the debugger, it's an invaluable tool and will aid you in learning why something is wrong

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.