C++ Interpreter for Custom Language Has Stopped Working

I wrote what is supposed to be a custom language interpreter of sorts. The first few commands worked, but then, after I added a few more commands, and had to change the ifstream read to a global opening it in main(), using getline() causes it to stop working. Why is this? I used a debugger and:
Access Violation(Segmentation Fault) raised in program.
Why is this? What is causing this? Is something wrong with the way I did that? The reason it is a global now is so that the JMP command works, as well as SIZ and SIN, so that those commands can getline() a number of times to skip those lines, and JMP can use seekg() to jump to another part of the code. Also, if I remove those commands and declare read in main(), it works and runs through, but skips some characters when reading the input file. I have absolutely no idea why? Strangely, it skips 3 on the first line, 2 on the second, 1 on the third, and none on the fourth. Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  #include<iostream>
#include<fstream>
#include<ctype.h>
#include<conio.h>
#include<string>
#include<vector>
using namespace std;

#define ADD "ADD"
#define SUB "SUB"
#define SET "SET"
#define ICO "ICO"
#define CCO "CCO"
#define IUI "IUI"
#define CUI "CUI"
#define COP "COP"
#define MUL "MUL"
#define DIV "DIV"
#define MOD "MOD"
#define TPC "TPC"
#define PKP "PKP"
#define SIZ "SIZ"
#define SIN "SIN"
#define JMP "JMP"

/*struct expr{
       string torun;
       int param1;
       int param2;       
};*/

ifstream read;
int* posoflines;
int linesread;
int mem[16];
bool cont;
void parse(string line);
void run(string com, int first, int second);

int main(int argc, const char* argv[]){
    cout << "Main function loaded." << endl;
    /*if(argc<=1){
                cout << "Filename required.";
                getch();
                return 0;
    }*///If no filename supplied, terminate program.
    cout << "Filename recieved." << endl;
    cont=true;
    linesread=0;
    string filename="addthese.txt";
    string sStore;
    for(int x=0; x<16; x++){ //Allocate 0 to all spots of memory.
            mem[x]=0;
    }
    read.open(filename.c_str()); //Open file.
    cout << "File loaded." << endl;
    if(read.good()){
                    cout << "File good." << endl;
    }
    read >> sStore;
    cout << "First line read: "  << sStore << endl;
    int lines=atoi(sStore.c_str());
    cout << "Converted to int." << endl;
    posoflines = new int[lines];
    cout << "Array initialized." << endl;
    while(getline(read, sStore) && cont){ //Call parse fuction for each line.
    posoflines[linesread++]=read.tellg();
    parse(sStore);
    cout << "Parsing line" << endl;
    }
    getch(); //Pause at end of program.
}

void parse(string line){
     char cur;
     string command;
     string store = "";
     int left=0;
     int right=0;
     int curpos=0;
     int iCur=0; //Allocate all registers as blank.
     for(int i=0; i<line.length(); i++){
             cur = line[i]; //Set cur.
             if(isspace(cur)){ //If it is a space.
                              if(curpos==0){
                                            command = store;
                                            curpos++;
                              }else if(curpos==1){
                                    for(int e=0; e<store.length(); e++){
                                            int quick=(store[e]-'0');
                                            iCur*=10;
                                            iCur+=quick;
                                    }
                                    left = iCur;
                                    iCur=0;
                                    curpos++;
                              }else if(curpos==2){
                                    for(int e=0; e<store.length(); e++){
                                            int quick=(store[e]-'0');
                                            iCur*=10;
                                            iCur+=quick;
                                    }
                                    right = iCur;
                              } //Store in appropriate register.
                              store=""; //Reset store.
             }else{ //If not a space.
                   store+=cur; //Append to store.
             }
             cout << "Character Parsed:" << cur << endl;
     }
     run(command, left, right);
}

void run(string com, int first, int second){
     string nulled;
     cout << "Running command" << endl;
     if(com==SET){
                  mem[first]=second;
     }else if(com==ADD){ //Add command.
           mem[first]+=mem[second];
     }else if(com==SUB){ //Subtract command.
           mem[first]-=mem[second];
     }else if(com==ICO){ //Integer Console Ouput command.
           cout << mem[first];
     }else if(com==CCO){ //Character Console Output command.
           cout << (char)mem[first];
     }else if(com==IUI){ //Integer User Input command.
           cin >> mem[first];
     }else if(com==CUI){ //Character User Input command.
           mem[first]=getch();
     }else if(com==COP){ //Copy command.
           mem[first]=mem[second];
     }else if(com==MUL){ //Multiply command.
           mem[first]*=mem[second];
     }else if(com==DIV){ //Divide command.
           mem[first]/=mem[second];
     }else if(com==MOD){ //Modulo command.
           mem[first]%=mem[second];
     }else if(com==TPC){ //Terminate Program at Command command.
           cont=false;
     }else if(com==PKP){ //Pause until Key Pressed command.
           getch();
     }else if(com==SIZ){ //Skip If Zero command.
           if(mem[first]==0){
                             for(int j=0; j<second; j++){
                                     getline(read, nulled);
                             }
           }
     }else if(com==SIN){ //Skip If Non-Zero command.
           if(mem[first]!=0){
                             for(int j=0; j<second; j++){
                                     getline(read, nulled);
                             }
           }
     }else if(com==JMP){
           read.seekg(posoflines[first]);
     }
}


Also, if you leave an answer, please don't just say which line is causing a problem or something like that, tell me what I need to change my code to WITH EXAMPLE CODE.

addthese.txt:
1
2
3
4
5
4
SET 0 4
SET 1 1
SUB 0 1
ICO 0 0 

(the second 0 in ICO is just a filler space)
But the program reads it as:
1
2
3
4
 0 4
T 1 1
UB 0 1
ICO 0 0

Why?

EDIT:
Okay, I fixed the problem where it skips characters, and I'm going to use a completely different method for jumping now, but I still need to know why I can't use getline() if ifstream read is declared as a global. Any ideas?
Last edited on
Topic archived. No new replies allowed.