Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Learning Aids/Tools 2 years ago
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.
Turn Your Knowledge into Earnings.
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 REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
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.
std::isalpha/isdigit
char
int
a couple of tips:
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.
General Tech 10 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 9 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.