c++ terminate std::logic error ......where did my code go wrong? - c++

I'm encountering this really weird error and I dont know where I'm constructing a string with a null parameter, could someone please help me out? Thanks!
Here's the error:
terminate_called_after_throwing_an_instance_of_'std::logic_error'
__what():__basic_string::_M_construct_null_not_valid
Here's my code:
/*
ID: 2005amr1
TASK: friday
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
using namespace std;
vector <string> days = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
bool leap (int y){
if (y%100 == 0){
if (y%400 == 0){
return true;
}
else{
return false;
}
}
else if (y%4 == 0){
return true;
}
else{
return false;
}
}
int targetedSearch (vector<string> array, string key){
int index = 0;
while( (index < array.size( )) && (key != array[index])){
if (array[index] != key){
index++;
}
}
return (index);
}
string findLastDay(string FDAY, int daysInMonth){ // all good here
int indexOfString = targetedSearch(days, FDAY);
if (FDAY == "Mon" && daysInMonth == 28){
return ("Sun");
}
return (days[(daysInMonth - (7*(daysInMonth/7))-1) + indexOfString]);
}
string find13Day (string firstDay){ // all good here
int indexOfString = targetedSearch(days, firstDay);
return (days[indexOfString+5]);
}
string findFirstDayNextMonth (string LDAY){
int indexOfString = targetedSearch(days, LDAY);
if (indexOfString+1 > 6){
return (days[(indexOfString+1)-7]);
}
else{
return (days[indexOfString+1]);
}
}
int numDays (int month, int year){
switch(month) {
case 0:
return 31;
break;
case 1:
if (leap(year) == true){
return 29;
}
else{
return 28;
}
break;
case 2:
return 31;
break;
case 3:
return 30;
break;
case 4:
return 31;
break;
case 5:
return 30;
break;
case 6:
return 31;
break;
case 7:
return 31;
break;
case 8:
return 30;
break;
case 9:
return 31;
break;
case 10:
return 30;
break;
case 11:
return 31;
break;
}
return 31;
}
int main() {
ofstream fout ("friday.out");
ifstream fin ("friday.in");
string startDay = "Mon";
int dayValues[7] = {0,0,0,0,0,0,0};
int n;
fin >> n;
map <string,int> dayValue;
for (int i = 1900; i< 1900+n; i++){
for (int m =0; m<12; m++){
int PAN = numDays (m,i);
string tday = find13Day(startDay);
dayValues[targetedSearch(days,tday)]++;
string LDAY = findLastDay(startDay, PAN);
startDay = findFirstDayNextMonth(LDAY);
}
}
fout << " " << dayValues[5] << " " << dayValues[6]<< " " << dayValues[0]<< " " << dayValues[1]<< " " << dayValues[2]<< " " << dayValues[3]<< " " << dayValues[4];
}

Related

Program freezing up, not accepting inputs? Correct clock?

#include<iostream>
#include<conio.h>
#include <chrono>
#include <ctime>
using namespace std;
typedef std::chrono::high_resolution_clock Clock;
/*struct*/ class Car
{
private:
int carYear;
string carMake;
string carModel;
int carSpeed;
float carGallons;
public:
Car(int cY = 0, string cMa = "", string cMo = "", int cS = 0, float cG = 10)
{
carYear = cY;
carMake = cMa;
carModel = cMo;
carSpeed = cS;
carGallons = cG;
}
Car()
{
carMake = "Bugatti";
carYear = 2020;
carModel = "Chiron";
}
int getCarYear() { return carYear; }
string getCarMake() { return carMake; }
string getCarModel() { return carModel; }
int getCarSpeed() { return carSpeed; }
float getCarGallons() { return carGallons; }
void setCarSpeed(int cS) { carSpeed = cS; }
void setCarGallons(float cG) { carGallons = cG; }
void accelerate()
{
startCar();
if(carGallons>0)
{
carSpeed += 5;
carGallons -= .5;
}
else if (carGallons < 0)
{
carGallons = 0;
cout << "Please refill tank!";
}
}
void brake()
{
startCar();
if(carSpeed>0&&carGallons>0)
{
carSpeed -= 5;
carGallons -= .2;
}
else if(carSpeed<0&&carGallons<0)
{
carSpeed = 0;
cout << "Your car isn't moving!";
}
}
void fillUP()
{
if (carSpeed > 0)
{
cout << "The car is still moving!";
}
else if(carSpeed==0)
{
if (carGallons > 22)
{
cout << "Car is full!";
carGallons = 22;
}
else
{
carGallons += .2;
}
}
}
void startCar(bool carOn=true)
{
while (true)
{
std::chrono::system_clock::time_point then;
auto now = std::chrono::system_clock::now();
auto duration = now - then;
then = now;
if (false) return;
{
auto s = std::chrono::duration_cast<std::chrono::duration<double>>(duration);
carGallons -= s.count() * .05f;
}
}
}
};
int main()
{
bool carOn = false;
Car userCar(2020, "Bugatti", "Chiron");
cout << "\nStart your new " << userCar.getCarYear()<<" "<< userCar.getCarMake() <<" "<< userCar.getCarModel()<<"!";
cout << "\nHit O to start your Engine!";
char ch;
cin >> ch;
if (ch== 'o'|| ch=='O') {
bool carOn = true;
cout << "\nCar is now on! Safe driving!\n";
int c = 0;
while (1)
{
c = 0;
switch (c = _getch())
{
case 32:
userCar.fillUP();
break;
case 72:
cout << userCar.getCarSpeed() << " " << userCar.getCarGallons();
userCar.accelerate();
break;
case 80:
userCar.brake();
break;
case 75:cout << "\nleft"; break;
case 77:cout << "\nright"; break;
default:
cout << " ";
}
}
}
else
{
cout << "\nCar is not on!";
}
return 0;
}
I'm running into an issue where after taking input from the user to turn on their car the inputs freeze up and the switch doesn't accept any other inputs. I know it has something to do with how I'm calling my functions inside the switch but I'm at a loss for how to correct it. Also, I'm fairly certain I've incorrectly implemented my clock inside startCar, it's supposed to subtract gas while the car is on.

Segmentation fault 11 in C++ on Mac HighSierra

It gets segmentation fault 11 when I tried to inqueue the data in my queue program. I'm using the GCC compiler in my Atom text editor.
#include<iostream>
#include<stdlib.h>
#define MAX 5
using namespace std;
struct queue{
int data[MAX];
int awal, akhir;
}antrean;
void first(){
antrean.awal = -1;
antrean.akhir = -1;
}
bool isfull(){
if(antrean.akhir == MAX-1){
return true;
}else{
return false;
}
}
bool isempty(){
if(antrean.akhir == -1){
return true;
}else{
return false;
}
}
void tampildata(){
if(!isempty()){
for(int i=antrean.awal; i<antrean.akhir; i++){
cout<<antrean.data[i]<<" | ";
}
}
cout<<endl;
}
void inqueue(){
int elemen;
if(isempty()){
cout<<"input data : ";
cin>>elemen;
antrean.data[antrean.akhir] = elemen;
antrean.akhir++;
cout<<"data berhasil ditambah"<<endl;
tampildata();
}else{
cout<<"Queue penuh";
}
}
void dequeue(){
tampildata();
if(!isempty()){
cout<<"mengambil data "<<antrean.data[antrean.awal]<<endl;
for(int i=antrean.awal; i<antrean.akhir; i++){
antrean.data[i]=antrean.data[i+1];
}
antrean.akhir--;
}else{
cout<<"antrean empty";
}
}
void cari(){
if(!isempty() == 1){
int cari;
bool state = false;
cout << "Data yang dicari :";
cin >> cari;
for(int i = antrean.awal; i<antrean.akhir; i++){
if (antrean.data[i] == cari) {
cout << "Data ditemukan pada indeks Ke-:" << antrean.data[i]+1<< endl;
state = true;
break;
}
}
} else{
cout << "Data tidak ditemukan";
}
}
void totalarray(){
int temp = 0;
for(int i=-1; i<antrean.akhir; i++){
temp = temp+antrean.data[i];
}
cout << "Total nilainya :" << temp;
}
int main(){
int pilihan, elemen;
first();
do{
tampildata();
cout<<"1. Init"<<endl<<"2. inQueue"<<endl<<"3. deQueue"<<endl<<"4. tampil data"<<endl<<"5. Cari data"<<endl << "6. Total nilai"<<endl<<"7. Rata-rata Array"<<endl;
cout<<"8. Nilai terbesar"<<endl<<"9. Nilai terkecil"<<endl<<"10. Clear"<<endl<<"11. Keluar";
cout<<"input pilihan :";
cin>>pilihan;
switch(pilihan){
case 1:
first();
break;
case 2:
inqueue();
break;
case 3:
dequeue();
break;
case 4:
tampildata();
break;
case 5:
cari();
break;
case 6:
totalarray();
break;
case 7:
exit(0);
}
}
while(pilihan!=7);
return 0;
}
Your problem is in the value of your counter. You initially set it to -1 (although 0 might make more sense if it has 0 elements?). Then, when you're adding to your queue for the first time you basically want to add an element to the (-1)st place. This causes you to write outside the bounds of the array and a segmentation fault occurs.
antrean.akhir = -1;
...
antrean.data[antrean.akhir] = elemen;
You can simply set the counter to 0 and have it actually count the number of elements in the array.
I have not gone through the rest of the code, so I can't say it won't crash for some other reason later on.

Allocating array of objects dynamically freeze

This is for an assignment before you tell me not to use an array. Unfortunately I have to.
I've troubleshot this for a good amount of time and I've finally given up and come to ask the smart people here. I know this is something to do with how I'm allocating the array, but I can't figure what's wrong. It goes through the inner loop once and then freeze on the second run. Sorry if I left anything important out. I will add any needed info.
Variables from Card
private:
string *cardRank;
string *suit;
int rankNum;
int value;
Problem Function
void initArray(Card **cPtr)
{
int i;
int j;
int index=0;
cPtr = new Card*[DECK]; //deck is const int 52
for(i=0; i < 4; ++i)
{
for(j=1; j < 14; ++j)
{
cPtr[index] = new Card(j, j, i); //freezes here. does not make it to the first
//function in the constructor
cout << cPtr[index] << endl;
++index;
}
}
}
Constructor
Card::Card(int cRank, int cValue, int suitNum)
{
setRankNum(cRank);
cout << "rank num set\n";
setValue(cValue);
cout << "val set\n";
setSuit(suitNum);
cout << "suit set\n";
setRank(cRank);
cout << "rank set\n";
}
Overloaded <<
ostream &operator << (ostream &strm, Card &aCard)
{
strm << aCard.getRank() << " of " << aCard.getSuit();
return strm;
}
whole program
#include <string>
#include "Card.h"
using namespace std;
const int DECK = 52;
void initArray(Card **&cPtr);
void shufflePArray(Card **pArray);
void determineHand(Card **pArray);
bool isFlush(Card *hand);
bool isStraight(Card *hand);
bool isFour(Card *hand);
bool isThree(Card *hand);
bool isTwo(Card *hand);
void drawHand(Card *hand, Card **pArray);
void displayHand(Card **hand);
int main()
{
Card **cArray;
initArray(cArray);
cout << "done.";
shufflePArray(cArray);
determineHand(cArray);
delete [] cArray;
return 0;
}
void initArray(Card **&cPtr)
{
int i;
int j;
int index=0;
cPtr = new Card*[DECK];
for(i=0; i < 4; ++i)
{
for(j=1; j < 13; ++j)
{ cout << "inner loop " << index << endl;
cPtr[index] = new Card(j, j, i);
cout << cPtr[index] << endl;
++index;
}
}
}
void shufflePArray(Card **pArray)
{
//code here
}
void determineHand(Card ***pArray)
{
Card hand[5];
drawHand(hand, pArray);
displayHand(pArray);
if (isFlush(hand) == true)
{
if(isStraight(hand) == true)
cout << "Straight flush!!!" << endl;
else
cout << "You got a flush!" << endl;
}
else if(isStraight(hand) == true)
{
cout << "You got a straight!" << endl;
}
else if(isFour(hand) == true)
{
cout << "Four of a kind!!!" << endl;
}
else if(isThree(hand)==true)
{
cout << "Three of a kind!" << endl;
}
else if(isTwo(hand) == true)
{
cout << "That's a pair";
}
}
bool isFlush(Card *hand)
{
int i;
int match;
for (i=0; i<5;++i)
{
if (hand[0].getSuit() == hand[i].getSuit())
match++;
}
if (match == 5)
return true;
else
return false;
}
bool isStraight(Card *hand)
{
int match;
for (int i=0; i<5;++i)
{
if (hand[0].getValue() == hand[i].getValue() - 1)
match++;
}
if (match == 5)
return true;
else
return false;
}
bool isFour(Card *hand)
{
int match = 0;
for(int i=0; i<5; ++i)
{
match=0;
for(int j=0;j<5;++j)
{
if (hand[i] == hand[j])
match++;
if (match == 4)
return true;
else
return false;
}
}
}
bool isThree(Card *hand)
{
int match = 0;
for(int i=0; i<5; ++i)
{
match=0;
for(int j=0;j<5;++j)
{
if (hand[i] == hand[j])
match++;
if (match == 3)
return true;
else
return false;
}
}
}
bool isTwo(Card *hand)
{
int match = 0;
for(int i=0; i<5; ++i)
{
match=0;
for(int j=0;j<5;++j)
{
if (hand[i] == hand[j])
match++;
if (match == 2)
return true;
else
return false;
}
}
}
void drawHand(Card *hand, Card **pArray)
{
for(int i=0; i<5;++i)
cout << hand[i];
}
}
{
hand[i] = *pArray[i];
}
}
void displayHand(Card **hand)
{
Card temp;
for (int i = 0; i < 5; ++i)
{
contents of card.h
#ifndef CARD_H
#define CARD_H
#include <string>
#include <iostream>
using namespace std;
class Card
{
private:
string *cardRank;
string *suit;
int rankNum;
int value;
public:
Card();
Card(int cRank, int cValue, int suitNum);
friend ostream &operator << (ostream &strm, Card &aCard);
bool operator > (const Card &aCard);
bool operator < (const Card &aCard);
bool operator == (const Card &aCard);
void setRank(int r);
void setSuit(int s);
void setValue(int v);
void setRankNum(int n);
string getRank();
string getSuit();
int getRankNum();
int getValue();
};
#endif // CARD_H
contents of Card.cpp
#include "Card.h"
#include <string>
#include <iostream>
Card::Card()
{
cardRank = NULL;
suit = NULL;
rankNum = 0;
value = 0;
}
Card::Card(int cRank, int cValue, int suitNum)
{
setRankNum(cRank);
cout << "rank num set\n";
setValue(cValue);
cout << "val set\n";
setSuit(suitNum);
cout << "suit set\n";
setRank(cRank);
cout << "rank set\n";
}
ostream &operator << (ostream &strm, Card &aCard)
{
strm << aCard.getRank() << " of " << aCard.getSuit();
return strm;
}
bool Card::operator > (const Card &aCard)
{
if (aCard.value > value)
return true;
else
return false;
}
bool Card::operator < (const Card &aCard)
{
if (aCard.value < value)
return true;
else
return false;
}
bool Card::operator == (const Card &aCard)
{
if (value == aCard.value)
return true;
else
return false;
}
void Card::setRank(int r)
{
switch(r)
{
case 13:
*cardRank = "Ace";
case 1:
*cardRank = "Two";
case 2:
*cardRank = "Three";
case 3:
*cardRank = "Four";
case 4:
*cardRank = "Five";
case 5:
*cardRank = "Six";
case 6:
*cardRank = "Seven";
case 7:
*cardRank = "Eight";
case 8:
*cardRank = "Nine";
case 9:
*cardRank = "Ten";
case 10:
*cardRank = "Jack";
case 11:
*cardRank = "Queen";
case 12:
*cardRank = "King";
}
}
void Card::setSuit(int s)
{
if(s==0){
*suit = "Hearts";
cout << "suit set";}
else if(s==1)
*suit = "Diamonds";
else if (s==2)
*suit = "Clubs";
else if (s==3)
*suit = "Spades";
else
cout << "Invalid suit num.";
}
void Card::setValue(int v)
{
if (v > 0)
value = v;
}
void Card::setRankNum(int n)
{
rankNum = n;
}
string Card::getRank()
{
return *cardRank;
}
string Card::getSuit()
{
return *suit;
}
int Card::getRankNum()
{
return rankNum;
}
int Card::getValue()
{
return value;
}
One possible reason for your troubles is that you are dereferencing a NULL pointer:
void Card::setRank(int r)
{
switch(r)
{
case 13:
*cardRank = "Ace";
case 1:
*cardRank = "Two";
//...
The cardRank is NULL. You now try to dereference a NULL pointer. Unless I missed something, where is the call to "cardRank = new std::string;"?
But that brings up a bigger point -- in your comment you stated that your professor wants you to use pointers. But honestly, there is no reason whatsoever to use pointers for those string members -- absolutely none.
You do need pointers to implement your dynamic array, but that's it. Either you are reading the professor's intentions incorrectly, or the professor needs to get another profession.
You're passing a pointer to a pointer as value in : initArray(Card **cPtr), apparently your intention is to change it (i.e return the allocated Card array.
I cannot really say what is happening exactly but some memory gets overwritten, the return address in the stack probably
Anyway either change it to reference i.e. initArray(Card **&cPtr) or use initArray(Card ***cPtr) and change the code accordingly.
The second problem in the use of strings, change string *cardRank to string cardRank and suit as well, you don't need pointers to strings (it's not like char*, those are objects any way, the assignments should be changed from *cardRank = "Ace" to cardRank = "Ace"

How to include global functions in a separate file

I am trying to organize my code by grouping functions in seperate header/source files. I've #included the header file in my main .cpp, but the compiler does not see the functions in convertTypes.cpp. What gives? And how do I use my 'key' typedef globally (so also in the seperated function sources)? Lots of code, sorry.
/*
* NoteMaker.cpp
*
* Created on: Sep 4, 2013
* Author: edwinrietmeijer
*/
typedef struct {
int keyNum;
int keyType;
} key;
#include <iostream>
#include <string>
#include <iomanip>
#include "convertTypes.h"
using namespace std;
const int KEYSET[ ] = { 0, 2, 4, 5, 7, 8, 9 };
int* generateNotes( int, key );
void echoNoteList( const int* , const int, const key );
string getKeyStringFromUser();
int main() {
key keyStruct;
int octave;
int nrOfNotes;
string firstNoteName;
// Get inputs
cout << "What key would you like to generate notes in? ( f, cis, es, etc.)" << endl;
firstNoteName = getKeyStringFromUser();
cout << "In what octave would you like to generate notes? (-1 / 9)" << endl;
cin >> octave;
octave += 1;
cout << "How many notes do you wish to generate?" << endl;
cin >> nrOfNotes;
// create a key data struct from input string
keyStruct = convertKeyStringToKeyStruct( firstNoteName );
// add the starting octave nr to the keyStruct
keyStruct.keyNum += octave * 12;
// generate note list
int* noteList = new int[ nrOfNotes ];
noteList = generateNotes( nrOfNotes, keyStruct );
// echo note list to terminal
echoNoteList( noteList , nrOfNotes, keyStruct);
cin.get();
}
int* generateNotes( int notes, key keyStruct) {
int* newList = new int [notes];
int currNote = keyStruct.keyNum + keyStruct.keyType;
int currDist = 0;
newList[0] = currNote;
for (int i=1; i < notes; i ++) {
currDist = i % 7;
if ( currDist == 0 || currDist == 3 ) // half step or whole step?
{ currNote = currNote + 1; }
else
{ currNote = currNote + 2; }
newList[ i ] = currNote;
}
cout << "Generated list." << endl;
return newList;
}
void echoNoteList( const int* noteList, const int nrOfNotes, const key thisKeyStruct )
{
int currNote;
for (int i = 0; i < nrOfNotes ; i ++) {
currNote = noteList[ i ] % 12;
if ( currNote < 0 )
currNote += 12;
cout << left;
cout << setw(5) << noteList[ i ] << setw( 5 ) << convertToNoteName( currNote, thisKeyStruct.keyType ) << endl;
}
}
string getKeyStringFromUser() {
bool correctInput = false;
string getKeyName;
int keyNum;
while ( ! correctInput ) {
cin >> getKeyName;
cout << endl;
keyNum = getKeyName[ 0 ];
if ( keyNum > 96 && keyNum < 104 ) {
correctInput = true;
}
else
{
cout << "Wrong input. Try again." << endl;
}
}
return getKeyName;
}
convertTypes.h
#ifdef CONVERTTYPES_H
#define CONVERTTYPES_H
std::string convertToNoteName( int, int );
key convertKeyStringToKeyStruct( std::string );
#endif
convertTypes.cpp
/*
* convertTypes.cpp
*
* Created on: Sep 5, 2013
* Author: edwinrietmeijer
*/
#include <string>
#include "convertTypes.h"
using namespace std;
typedef struct {
int keyNum;
int keyType;
} key;
key convertKeyStringToKeyStruct( string firstNote ) {
int stringSize;
int keyType = 0;
char keyChar;
key thisKey;
keyChar = firstNote[ 0 ];
// get key type (flat, sharp, normal)
stringSize = firstNote.size( );
if (stringSize > 1 ) {
switch( firstNote[ 1 ] ) {
case 'e':
keyType = -1; break;
case 's':
keyType = -1; break;
case 'i':
keyType = 1; break;
default:
keyType = 0; break;
}
}
// convert key char to ascii code
int ASkey = keyChar;
thisKey.keyNum = KEYSET[ ASkey - 99 ];
thisKey.keyType = keyType;
return thisKey;
}
string convertToNoteName( int thisNote, int thisKeyType = 0) {
string noteName;
char addKeyType;
switch( thisKeyType ) {
case -1:
addKeyType = 'b'; break;
case 0:
addKeyType =' '; break;
case 1:
addKeyType = '#'; break;
}
switch( thisNote ) {
case 0:
noteName = "C"; break;
case 1:
if( thisKeyType == 1)
noteName = string ("C") + addKeyType;
else
noteName = string("D") + addKeyType; break;
case 2:
noteName = "D"; break;
case 3:
if( thisKeyType == 1)
noteName = string ("D") + addKeyType;
else
noteName = string("E") + addKeyType; break;
case 4:
noteName = "E"; break;
case 5:
noteName = "F"; break;
case 6:
if( thisKeyType == 1)
noteName = string ("F") + addKeyType;
else
noteName = string("G") + addKeyType; break;
case 7:
noteName = "G"; break;
case 8:
if( thisKeyType == 1)
noteName = string ("G") + addKeyType;
else
noteName = string("A") + addKeyType; break;
case 9:
noteName = "A"; break;
case 10:
if( thisKeyType == 1)
noteName = string ("A") + addKeyType;
else
noteName = string("B") + addKeyType; break;
case 11:
noteName = "B"; break;
default:
noteName = "!"; break;
}
return noteName;
}
Change:
#ifdef CONVERTTYPES_H
to:
#ifndef CONVERTTYPES_H
You are effectively compiling-out your definitions.
As to your second point, move this:
typedef struct {
int keyNum;
int keyType;
} key;
into the header file (before its first use there).
However I would warn against using a name like key as it's commonly used as a variable name. I would go for key_t or MySpecialKeyForStuffImDoing (or somesuch).
In addition to #trojanfor's anwer: also create a new NoteMaker.h containing the key structure definition or move the stuct definition to convertTypes.hso that you don't duplicate it in multiple places

What does my destructor need to be in this function?

My program executes fine, except at the end, when I debug it, it's getting a segmentation error after calling my destructor. I'm not sure what the cause of the issue is. I've posted 2 of the relevant files.
Breakpoint 1, main () at Prog3.cc:12
12 cout << "Program executed" << endl;
(gdb) s
Program executed
~Lex (this=0x80375c4) at lex.cc:36
36 delete [] str;
(gdb) s
37 }
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0xfef49418 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string () from /usr/sfw/lib/libstdc++.so.6
(gdb) Quit
lex.h is below:
#ifndef LEX_H
#define LEX_H
#include "token.h"
#include <iostream>
#include <stdlib.h>
class Lex {
public:
Lex(istream& in, string fileName);
//Lex();
//Lex(const Lex& l);
~Lex();
static const int INTLIT = 1;
static const int FLOATLIT = 2;
static const int STRLIT = 3;
static const int IDENT = 4;
static const int PLUS = 5;
static const int MINUS = 6;
static const int TIMES = 7;
static const int DIVIDE = 8;
static const int REM = 9;
static const int ASSIGN = 10;
static const int LPAREN = 11;
static const int RPAREN = 12;
static const int COMMA = 13;
static const int EQ = 14;
static const int LT = 15;
static const int LE = 16;
static const int GT = 17;
static const int GE = 18;
static const int NE = 19;
static const int SET = 20;
static const int PRINT = 21;
static const int WHILE = 22;
static const int DO = 23;
static const int END = 24;
static const int AND = 25;
static const int OR = 26;
static const int IF = 27;
static const int THEN = 28;
static const int ELSE = 29;
static const int ENDIF = 30;
static const int PROGRAM = 31;
static const int ENDOFFILE = 32;
static const int ERROR = 33;
int charIndex;
int lineIndex;
int spaceIndex;
int lineNum;
int lineLength[100];
char ch;
bool hashCheck;
bool stopLoop;
Token nextToken();
char nextChar();
char str[256][256];
bool checkSet;
void printLex();
string idents[256];
int identCount;
friend ostream& operator<<(ostream& out, const Token& t);
};
#endif
#include "lex.h"
#include "token.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
lex.cc is here.
Lex::Lex(istream& in, string fileName) {
stopLoop = false;
charIndex = 0;
lineIndex = 0;
identCount = 0;
lineNum = 0;
hashCheck = false;
checkSet = false;
int tempSize;
ifstream file;
string temp;
for (int i = 0; i < 100; ++i)
lineLength[i] = 0;
if (!file.is_open()) { file.open(fileName.c_str()); }
while(!file.eof()) {
std::getline(file,temp);
tempSize = temp.size();
for (int i = 0; i < tempSize; ++i) {
str[lineNum][i] = temp[i];
lineLength[lineNum] += 1;
}
lineNum++;
}
file.close();
}
Lex::~Lex() {
delete [] str;
}
void Lex::printLex() {
charIndex = 0;
lineIndex = 0;
while (stopLoop == false) {
cout << nextToken();
// cout << "Line index: " << lineIndex << endl;
}
}
ostream& operator <<(ostream& out, const Token& t) {
out << t.type() << " \t " << t.lexeme() << " \t \t " << (t.line() + 1) << " \t \t " << t.pos() << endl;
return out;
}
bool isReal(char ch) {
string alphabet = "abcdefghijklmnopqrstuvwxyz1234567890(){}<>+-/=!*,%&|.";
if (alphabet.find(ch) != alphabet.npos) return true;
else return false;
}
bool isNum(char ch) {
string specialChars = "1234567890.";
if (specialChars.find(ch) != specialChars.npos) return true;
else return false;
}
bool isNumFinal(string b) {
int length = b.length();
const char* temp = b.c_str();
bool henry = true;
for (int i = 0; i < length; ++i) {
if (henry == false) { break; }
henry = isNum(temp[i]);
}
return henry;
}
bool isSpecialChar(char ch) {
string specialChars = "(){}<>+-/=!*,%&|";
if (specialChars.find(ch) != specialChars.npos) return true;
else return false;
}
char Lex::nextChar() {
if (lineIndex >= lineNum) {
// cout << "End of file reached\n";
stopLoop = true;
return '#';
}
else if (charIndex >= lineLength[lineIndex]) {
lineIndex++;
charIndex = 0;
return nextChar();
}
else if (str[lineIndex][charIndex] == '#') {
hashCheck = true;
while (hashCheck = true) {
if (str[lineIndex][charIndex] == '#') { hashCheck = false; }
charIndex++;
if (charIndex > lineLength[lineIndex]) {
charIndex = 0;
lineIndex++;
}
}
}
else {
ch = str[lineIndex][charIndex];
charIndex++;
return ch;
}
cout << "you shouldn't be here\n";
return str[lineIndex][charIndex];
}
Token Lex::nextToken() {
if (charIndex == lineIndex && charIndex == 0) { ch = nextChar(); }
while (ch == ' ') { ch = nextChar(); }
// cout << "CI: " << charIndex << endl;
string build;
int tempCharIndex = charIndex;
int tempLineIndex = lineIndex;
build += ch;
if (charIndex == lineIndex && charIndex == 0) { build = ""; }
if (checkSet == true) {
checkSet = false;
while (ch != ' ' && tempLineIndex == lineIndex) {
ch = nextChar();
if (ch != ' ' && tempLineIndex == lineIndex)
build += ch;
}
idents[identCount] = build;
identCount++;
return Token(IDENT, build, tempLineIndex, tempCharIndex);
}
else if (isSpecialChar(ch)) {
switch(ch) {
case '(':
ch = nextChar();
return Token(LPAREN, build, tempLineIndex, tempCharIndex);
case ')':
ch = nextChar();
return Token(RPAREN, build, tempLineIndex, tempCharIndex);
case '{':
ch = nextChar();
return Token(THEN, build, tempLineIndex, tempCharIndex);
case '}':
ch = nextChar();
return Token(ENDIF, build, tempLineIndex, tempCharIndex);
case '+':
ch = nextChar();
return Token(PLUS, build, tempLineIndex, tempCharIndex);
case '-':
ch = nextChar();
return Token(MINUS, build, tempLineIndex, tempCharIndex);
case '/':
ch = nextChar();
return Token(DIVIDE, build, tempLineIndex, tempCharIndex);
case '*':
ch = nextChar();
return Token(TIMES, build, tempLineIndex, tempCharIndex);
case '=':
ch = nextChar();
if (ch == '=') {
build += ch;
ch = nextChar();
return Token(EQ, build, tempLineIndex, tempCharIndex);
}
else {
return Token(ASSIGN, build, tempLineIndex, tempCharIndex);
}
case '>':
ch = nextChar();
if (ch == '=') {
build += ch;
ch = nextChar();
return Token(GE, build, tempLineIndex, tempCharIndex);
}
else return Token(GT, build, tempLineIndex, tempCharIndex);
case '<':
ch = nextChar();
if (ch == '=') {
build += ch;
ch = nextChar();
return Token(LE, build, tempLineIndex, tempCharIndex);
}
else return Token(LT, build, tempLineIndex, tempCharIndex);
case '!':
ch = nextChar();
if (ch == '=') {
build += ch;
ch = nextChar();
return Token(NE, build, tempLineIndex, tempCharIndex);
}
else return Token(ERROR, build, tempLineIndex, tempCharIndex);
case '%':
ch = nextChar();
return Token(REM, build, tempLineIndex, tempCharIndex);
case '&':
ch = nextChar();
return Token(AND, build, tempLineIndex, tempCharIndex);
case '|':
ch = nextChar();
return Token(OR, build, tempLineIndex, tempCharIndex);
default:
return Token(ERROR, build, tempLineIndex, tempCharIndex);
}
}
else if (isNum(ch)) {
tempCharIndex = charIndex;
while (ch != ' ' && tempLineIndex == lineIndex) {
ch = nextChar();
if (isSpecialChar(ch)) {
break;
}
if (ch != ' ' && tempLineIndex == lineIndex)
build += ch;
}
if (isNumFinal(build)) {
if (build.find('.') != build.npos)
return Token(FLOATLIT, build, tempLineIndex, tempCharIndex);
else return Token(INTLIT, build, tempLineIndex, tempCharIndex);
}
else return Token(ERROR, build, tempLineIndex, tempCharIndex);
}
else {
tempCharIndex = charIndex;
while (ch != ' ' && tempLineIndex == lineIndex) {
ch = nextChar();
// cout << "ch: " << ch << endl;
if (ch != ' ' && tempLineIndex == lineIndex)
//cout << "inserted: " << ch << endl;
build += ch;
}
if (build.compare("while") == 0)
return Token(WHILE, build, tempLineIndex, tempCharIndex);
else if (build.compare("if") == 0)
return Token(IF, build, tempLineIndex, tempCharIndex);
else if (build.compare("print") == 0)
return Token(PRINT, build, tempLineIndex, tempCharIndex);
else if (build.compare("end") == 0)
return Token(ENDOFFILE, build, tempLineIndex, tempCharIndex);
else if (build.compare("else") == 0)
return Token(ELSE, build, tempLineIndex, tempCharIndex);
else if (build.compare("do") == 0) { return Token(DO, build, tempLineIndex, tempCharIndex); }
else if (build.compare("set") == 0) {
checkSet = true;
// cout << "CI: " << str[lineIndex] << endl;
return Token(SET, build, tempLineIndex, tempCharIndex);
}
else {
for (int i = 0; i < identCount; ++i) {
if (build.compare(idents[i]) == 0) { return Token(IDENT, build, tempLineIndex, tempCharIndex); }
}
cout << "build:" << build << ".\n";
return Token(STRLIT, build, tempLineIndex, tempCharIndex);
}
}
}
Don't call delete[] unless you called new[]. str is a statically sized array in your class, you need neither new[] nor delete[] for it.
Your class consists entirely of objects which will either handle their own cleanup, or need no cleanup, so your destructor can simply be empty. Don't even declare it, and the compiler will provide the correct destructor for you in this case.
str does not have dynamic storage duration but you are trying to delete it in the destructor. Remove delete[] str from your destructor.
You also are not doing any bounds checking when you fill str. This may cause you to overwrite other member variables causing additional problems and is likely the cause of the fault you are experiencing.
// tempSize and/or lineNum may be greater than 256
for (int i = 0; i < tempSize; ++i) {
str[lineNum][i] = temp[i];
lineLength[lineNum] += 1;
}
You have
Lex::~Lex() {
delete [] str;
}
in your code, but str is not heap allocated. It is a data member of your Lex class
I believe a recent GCC invoked as g++ -Wall -g would probably have warned you about that mistake.
Well, cause you don't need to delete the str array at all, it's not created by you using new so you don't need to call delete.
VS gives me :
warning C4154: deletion of an array expression; conversion to pointer supplied