-
A question about Vectors
Hello i have a string vector which contains something like this 1 2 3 a b 4 -1 seperatly.
String vector displays 1 as 1, 2 as 2, a as a(for sure) but it does not display -1 properly. When i cout -1 value, it displays something like û1 this.
I have to write a if statement when -1 equals something. what should i write
if(a == here), to work.
Thanks!
-
-
I did not redesign my functions yet. I just want to solve my problem first. What i want to do is in the vector array when it sees -1 understand that a new line is beginning.
My input file dfa.txt is:
0 0 1 a -1
1 0 3 a 2 b -1
-1
Code:
#include <iostream>
#include "graph.h"
#include <fstream> // for ifstream
#include <string>
using namespace std;
#include "tvector.h"
#include "stdlib.h"
template <class T>
void PutInputToArray(ifstream &dfa,tvector<T> &v)
{
T word;
while(dfa >> word)
{
if(word == "-1")
{
cout<<"hey"<<endl;
}
v.push_back(word);
}
}
void GetIntVal(string &strConvert, int &i) {
i = atoi(strConvert.c_str());
}
int main() {
Graph myGraph;
tvector<string> v;
ifstream dfa;
string file="dfa.txt";
int NumberofStatesInt;
int j=0;
dfa.open(file.c_str());
PutInputToArray(dfa,v);
string NumberofStates = v[0];
GetIntVal(NumberofStates,NumberofStatesInt);
for(j; j< NumberofStatesInt; j++)
{
myGraph.AddVertex(j);
}
int k = 2;
string first = v[2];
string second = v[4];
string second2 = v[6];
int firstint, secondint;
GetIntVal(first,firstint);
GetIntVal(second,secondint);
myGraph.AddEdge(firstint,secondint);
return 0;
}
-
A few comments:
- Do not use using directives and declarations in headers (except within a local scope) and before including a header.
- "stdlib.h" should be <cstdlib> (or at least <stdlib.h>. However, at a glance, it looks like you do not need to include that header.
- Declare variables near first use. For example, you should declare j in the for loop itself. k does not ever appear to be used. (More on this in the last point.)
- Indent consistently with either spaces or tabs, but not both (unless you are very careful, but it typically is not worth the care).
- Post the smallest and simplest code that demonstrates the problem. In this case you are using a custom vector interface and implementation, so perhaps the bug lies there.
-
There is more than one '-' character. My guess is that your input uses one that is different from the simple dash in ASCII. Make sure that the input file has that simple dash.
If you're on windows, use Notepad to edit it. Check the dash. Try typing it in with Notepad and saving the file.