Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

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

General Tech Learning Aids/Tools
Max. 2000 characters
Replies

usr_profile.png
Rahul Chaudhary

User

( 5 months 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 <iostream> 
    #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 <iostream> 
    #include <fstream> 

    using namespace std; 

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

    int pos; 
    int
								 								 
							usr_profile.png
							
Liza Sain

User

( 5 months 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

what's your interest