Difficulty with pointer-vectors
Mar 10, 2012 at 5:21pm UTC
Hey, so for this school project (Data structures) I need to store items from a text document in a matrix so that I may later sort them and merge lists. I've been trying to get this to work but it keeps crashing, though I feel it should work. Can anyone identify what I'm doing wrong? I'm not that great with pointers and allocating memory, but worse yet is my syntax when it comes to any dynamic memory storage. I will gladly accept any help that comes my way, at this point I'm just intensely frustrated and am therefore taking a short break.
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<cmath>
#include<cstdlib>
#include "lexer.h"
using namespace std;
//How to make a vector matrix:
// vector<vector<type>> name
string trim(string x)
{
x = x.substr(1, (x.size()-1));
return x;
}
bool hasParentheses(string x)//Edit
{
bool open = false ;
bool closed = false ;
for (int i = 0; i < x.length(); i++)
{
if (x[i] == '(' )
{
open = true ;
}
if (open == true )
{
if (x[i] == ')' )
closed = true ;
}
}
if (open == true && closed == true )
return true ;
else
return false ;
}
void display(vector<Token> x)
{
for (int i = 0; i < x.size(); i++)
cout << x[i].value << "\t" ;
}
void display(vector< vector<Token> > x)
{
for (int i = 0; i < x.size(); i++)
{
for (int j = 0; j < x[i].size(); j++)
{
cout << x[i][j].value << "\t" ;
}
cout << endl;
}
}
void sort(vector< vector<Token> > x)
{
}
int main()
{
//Variables
Token tok;
Lexer lex;
string input;
// "display" variables
string file;
string sortedBy;
char * offnen;
fstream doc;
int numColumns = 0; //reset at end of function
vector<string> categories;
Token tok2;
vector< vector<Token> > docMatrix;
vector<Token> columns;
vector< vector<Token> > temptrix;
//Needs to be a vector or it can't be pushed onto the vector matrix
vector<Token> * line;
bool columnsDone = false ; // Needs to be reset after display command is called
int place = 0; //reset at end of function
////////////////////////////////////////////////////////////
// MAIN MENU
////////////////////////////////////////////////////////////
while (true )
{
cout << ">" ;
getline(cin, input);
lex.set_input(input);
tok = lex.next_token();
if (tok.value == "display" )
{
file = lex.next_token().value;
cout << file << endl;
if (lex.next_token().value != "sortedby" )
goto bad;
sortedBy = lex.next_token().value;
cout << sortedBy << endl;
offnen = (char *) file.c_str();
doc.open(offnen);
//////////////Goes on till end of Doc
while (!doc.eof())
{
getline(doc, input);
lex.set_input(input);
//Potential for error IF:
//There is anything else with parentheses
//Goes on till end of string
while (lex.has_more_token())
{
next:
tok = lex.next_token();
if (columnsDone == false )
{
tok2 = lex.next_token();
if (hasParentheses(tok2.value) == true )
{
columns.push_back(tok);
numColumns++;
}
else
{
docMatrix.push_back(columns);
line = new vector<Token>(numColumns);
columnsDone = true ;
goto next;
}
//cout << tok.value;
}
if (columnsDone == true )
{
for (int i = place; i < numColumns; i++)
{
(*line)[i] = tok; // <--Error Here
place = i; //should allow place to keep i's last location
if (i == (numColumns-1))
{
docMatrix.push_back((*line));
line = new vector<Token>[numColumns];
}
}
}
}
}
display(docMatrix);
}
else if (tok.value == "nljoin" )
{
}
else if (tok.value == "smjoin" )
{
}
else if (tok.value == "exit" )
{
break ;
}
else
{
bad:
cout << "Incorrect input" << endl;
}
}
return 0;
}
//Code for potential reuse
/*
//traverses from row 1 to end
for(int i = 1; i < docMatrix.size(); i++)
{
if(docMatrix[i].size() < columns)
{
docMatrix[i].push_back(tok);
break;
}
}
//Now do it again, but this time for tok2
for(int i = 1; i < docMatrix.size(); i++)
{
if(docMatrix[i].size() < columns)
{
docMatrix[i].push_back(tok2);
break;
}
}
*/
/*
//Now vector matrix should be filled with tokens
//so let's display it
for(int i = 0; i < docMatrix.size(); i++)
{
for(int j = 0; j < docMatrix[i].size(); j++)
cout << docMatrix[i][j].value << "\t";
cout << endl;
}
doc.close();
*/
Topic archived. No new replies allowed.