2-D vectors..string2int

R

Rich

HI,

I am working on a program which implements the simplex aglorithm. I
have decided to use a vector of vectors to set up my matrix. My
initial thought was to fill the row vector with type string, then
convert all but the operator(<=,>=,=) column to type int. This is
proving a bit harder then I thought. I thought the member function
atoi() did this, but I'm having problems. I'm also having some
problems with this code that I have been working on. I am able to fill
the first row, but that's about it. It seems to skip all but the first
iteration. Do I need to flush the stream buffer? Any tips on the
conversion from string to int(is it possible)? I'm a new user and
appreciate any input at all. Thanks for your time, this has sure taken
up some of mine. I'm compiler on Unix AIX 5.1

class TheMatrix
{
public:
vector <string> P;
vector< vector<string> > M;

~TheMatrix();
void ReadIn();
void AsktoReadIn();
};

// Deconstructor
TheMatrix::~TheMatrix()
{}

void TheMatrix::ReadIn()
{
string eqn;
string temp;

{
cout<<"Enter an equation:\n" << endl;
getline(cin, eqn);
istringstream ins;
ins.str(eqn);
string eqn;
string temp;

{
cout<<"Enter an equation:\n" << endl;
getline(cin, eqn);
istringstream ins;
ins.str(eqn);

//vector <string> X;

while (ins >> temp)
{

P.push_back(temp);

}
M.push_back(P);
}


}

void TheMatrix::AsktoReadIn()
{
char ch;
//ReadIn();
do
{

ReadIn();
cout << "another equation?";
cin >> ch; //seems to be skipping by this???
}while (ch=='y');

for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < P.size(); j++)
{
cout << P[j] << " ";
}

}

}

int main()
{
TheMatrix A;
A.AsktoReadIn();
return 0;
 
J

John Harrison

HI,

I am working on a program which implements the simplex aglorithm. I
have decided to use a vector of vectors to set up my matrix. My
initial thought was to fill the row vector with type string, then
convert all but the operator(<=,>=,=) column to type int. This is
proving a bit harder then I thought. I thought the member function
atoi() did this, but I'm having problems. I'm also having some
problems with this code that I have been working on. I am able to fill
the first row, but that's about it. It seems to skip all but the first
iteration. Do I need to flush the stream buffer? Any tips on the
conversion from string to int(is it possible)? I'm a new user and
appreciate any input at all. Thanks for your time, this has sure taken
up some of mine. I'm compiler on Unix AIX 5.1

class TheMatrix
{
public:
vector <string> P;
vector< vector<string> > M;

~TheMatrix();
void ReadIn();
void AsktoReadIn();
};

// Deconstructor
TheMatrix::~TheMatrix()
{}

void TheMatrix::ReadIn()
{
string eqn;
string temp;

{
cout<<"Enter an equation:\n" << endl;
getline(cin, eqn);
istringstream ins;
ins.str(eqn);
string eqn;
string temp;

{
cout<<"Enter an equation:\n" << endl;
getline(cin, eqn);
istringstream ins;
ins.str(eqn);

//vector <string> X;

while (ins >> temp)
{

P.push_back(temp);

}
M.push_back(P);
}


}

void TheMatrix::AsktoReadIn()
{
char ch;
//ReadIn();
do
{

ReadIn();
cout << "another equation?";
cin >> ch; //seems to be skipping by this???
}while (ch=='y');

for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < P.size(); j++)
{
cout << P[j] << " ";
}

}

}

int main()
{
TheMatrix A;
A.AsktoReadIn();
return 0;

There seems to be quite a long wrong with your code and your design. Here
are some miscellaneous points

1) What is the P doing as a class member? You are trying to build up a
matrix M, I can understand that, but then why P?

I think your code should look more like this

cout<<"Enter an equation:\n" << endl;
getline(cin, eqn);
istringstream ins;
ins.str(eqn);
vector <string> P;
while (ins >> temp)
{
P.push_back(temp);
}
M.push_back(P);

See how P is a local variable not a class member.

2) Look at the code that prints out the matrix.

for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < P.size(); j++)
{
cout << P[j] << " ";
}
}

This just prints out values from P again. Values from the matrix M are
never printed at all. I think you want something more like this

for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < M.size(); j++)
{
cout << M[j] << " ";
}
cout << '\n';
}

3) In the function TheMatrix::ReadIn you seem to have duplicated the same
piece of code twice, except that you ignore what you read in the first
time. I don't know what the reason for that is. Maybe you news client
messed up your code?

4) atoi is used to convert strings to integers (its not a member function
however just an ordinary function). Here's an example of its use

string a_string;
cin >> a_string;
int an_int = atoi(a_string.c_str());

5) I don't see the skipping past behaviour that you describe and I can't
see any reason in the code you've posted why that would happen. You
certainly don't need to flush stream buffer however.

6) But really I think your design is wrong. I would think about starting
again with a better design. It's a while since I studied the simplex
algorithm but as I recall its about solving systems of linear
inequalities. I think you need something better than a vector<string> to
hold an equation. I think you will need to invent your own classes to hold
equation data.

For instance

// a term in an equation
struct Term
{
double coeff;
char var;
};

// equation types
enum
{
lessThan,
lessThanOrEqual,
greaterThan,
greaterThanOrEqual,
};

struct LinearEquation
{
vector<Term> terms; // the terms on the left hand side
int type; // the inequality operator
double value; // the value on the right hand side
};

So for instance if the user entered the equation 5x < 6y + 7 you would end
up executing code like this

Equation eqn;
Term t;
t1.coeff = 5.0;
t1.var = 'x';
eqn.push_back(t);
t1.coeff = -6.0; // -6 because 6y has moved from the rhs to the lhs
t1.var = 'y';
eqn.push_back(t);
eqn.type = lessThan;
eqn.value = 7.0;

Or something like that, that was just off the top of my head but I think
you need a bit more structure than just a simple vector<string>. It does
mean that the code to input an equation will be more complex, but I don't
think short cuts are advisable here, you will just end up with more
complexity later on.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Lexical Analysis on C++ 1
Character operations in C++ 2
Filter sober in c++ don't pass test 0
TF-IDF 2
Crossword 2
Crossword 14
I need help 1
GET NEIL DEGRASSES TYSON, I ripped a hole with this one... 0

Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,479
Latest member
JaysonK723

Latest Threads

Top