Help needed!!

K

kittykat

Hello!

Below is my code which is supposed to do the following:
1) store each token of a line as elements of a vector, and store this
vector in another vector.

2) if the data is > 0, it should be stored as 1, else, it should be stored
as zero, in another vector called binaryVec.

It compiles correctly, however, the output is always 1 for every element
in the vector, even if it is < 0. Can someone please tell me what i'm
doing wrong?

#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
#include <istream>
#include <cstdlib>
#include <vector>
#include <sstream>
using namespace std;

typedef vector<string> lines;

int main()
{
ifstream inFile;
inFile.open("data.txt");

if (inFile.fail())
{
cout <<"\nThe file was not successfully opened" << endl;
exit(1);
}

vector<lines> SourceVector;
string one_line;
char token[128];
istringstream lineStream(one_line);

vector<bool> binaryVec;
bool binary;

while (getline(inFile, one_line, '\n'))
{
cout << "Read from file: " << one_line << endl;
vector<string> text;
istringstream lineStream( one_line );

while( lineStream >> token )
{
cout << " Token: " << token << endl;
text.push_back( token ); // stores tokens in text vector.


double newToken = atof(token);
if ( newToken >= 0)
{ binary = true; }

else
{ binary = false; }

binaryVec.push_back(binary);
}

SourceVector.push_back( text ); // stores text vector in
SourceVector.

//Testing output for binaryVec
for( int i = 1; i < binaryVec.size(); i++ )
{
cout << "Binary element " << i << " is " << binaryVec <<
endl;
cout << text.front() << endl;
}


}
system("PAUSE");
return 0;
}

by the way, the file looks something like:

A 0.1 0.2 0.3 0.4
B 0.2 0 0.4 0
..

Thanx in advance!
 
V

Victor Bazarov

kittykat said:
Below is my code which is supposed to do the following:
1) store each token of a line as elements of a vector, and store this
vector in another vector.

2) if the data is > 0, it should be stored as 1, else, it should be stored
^^^
I'd like to draw your attention to the operator. "Greater".
as zero, in another vector called binaryVec.

It compiles correctly, however, the output is always 1 for every element
in the vector, even if it is < 0.
^^^^^^^^^^^^^^^^^
I'd also like to draw your attention to this "even if it is < 0" thing.
Can someone please tell me what i'm
doing wrong?
[...]

double newToken = atof(token);
if ( newToken >= 0)
{ binary = true; }

else
{ binary = false; }

The four (five) lines above are bad style. You should simply write

binary = newToken ??? 0;

Now, I put ??? there because the operator seems to be different from
the one used in the part 2 of your description. Why is that?
binaryVec.push_back(binary);
}

SourceVector.push_back( text ); // stores text vector in
SourceVector.

//Testing output for binaryVec
for( int i = 1; i < binaryVec.size(); i++ )

In C++ we count elements of arrays, vectors, etc., FROM ZERO. Try to
remember that.
{
cout << "Binary element " << i << " is " << binaryVec <<
endl;
cout << text.front() << endl;
}


}
system("PAUSE");
return 0;
}

by the way, the file looks something like:

A 0.1 0.2 0.3 0.4
B 0.2 0 0.4 0
.


BTW, what does your output look like? What do you expect it to look
like? You speak of "even if it is < 0". I don't see any data less
than 0, do you?

V
 
S

Shezan Baig

kittykat said:
Hello!

Below is my code which is supposed to do the following:
1) store each token of a line as elements of a vector, and store this
vector in another vector.

2) if the data is > 0, it should be stored as 1, else, it should be stored
as zero, in another vector called binaryVec.

It compiles correctly, however, the output is always 1 for every element
in the vector, even if it is < 0. Can someone please tell me what i'm
doing wrong?

[snip]

by the way, the file looks something like:

A 0.1 0.2 0.3 0.4
B 0.2 0 0.4 0
.

I don't see any negative numbers in your data file...

-shez-
 
K

Karl Heinz Buchegger

kittykat said:
Hello!

Below is my code which is supposed to do the following:
1) store each token of a line as elements of a vector, and store this
vector in another vector.

2) if the data is > 0, it should be stored as 1, else, it should be stored
as zero, in another vector called binaryVec.

That's not what you coded. Here you say that the data has to be greater
then 0.0 But you coded: greater or equal 0.0
It compiles correctly, however, the output is always 1 for every element
in the vector, even if it is < 0. Can someone please tell me what i'm
doing wrong?

Sorry. I tested your program with an input file of mine and it works
as expected (ignoring the flaw from above on how to treat 0.0)

What input file are you using and where do you think your program
is wrong?
 
K

kittykat

I am so stupid!!!! Thanx so much for pointing that out!! I don't know where
my mind has been!!

I just have one more question:

the last line of the file is as follows:

C 43 65 76 98 098 96 32

but for some reason, it says that it has "Binary elements" from 0 to 25,
when it should only be from 0 to 8. Any suggestions of what the reason
could be??
 
K

Karl Heinz Buchegger

kittykat said:
I am so stupid!!!! Thanx so much for pointing that out!! I don't know where
my mind has been!!

I just have one more question:

the last line of the file is as follows:

C 43 65 76 98 098 96 32

but for some reason, it says that it has "Binary elements" from 0 to 25,
when it should only be from 0 to 8. Any suggestions of what the reason
could be??

Actually I think you had that problem already in a similar way
(remember your problem with the 'text' vector ?):
binaryVec never gets cleared. All the numbers from all the lines
accumulate in it during the whole file.
 
V

Victor Bazarov

kittykat said:
I am so stupid!!!! Thanx so much for pointing that out!! I don't know where
my mind has been!!

I just have one more question:

the last line of the file is as follows:

C 43 65 76 98 098 96 32

but for some reason, it says that it has "Binary elements" from 0 to 25,
when it should only be from 0 to 8. Any suggestions of what the reason
could be??

Doesn't it count your binary element over the whole file instead of
over each line separately? Just a thought...
 
K

kittykat

yes...you are right. where in the code would it be the right place to clear
it?
 
V

Victor Bazarov

kittykat said:
yes...you are right. where in the code would it be the right place to clear
it?

[From the original post:]
while (getline(inFile, one_line, '\n'))
{
cout << "Read from file: " << one_line << endl;
vector<string> text;

<here, for example>

V
 
V

Victor Bazarov

kittykat said:
how can i make it count over each line seperatly?

As Karl suggested, clear the bool vector. See my other
reply for the place where to do it.
 
K

Karl Heinz Buchegger

kittykat said:
yes...you are right. where in the code would it be the right place to clear
it?

How should I say it in a friendly way
Hmm.
I bail out now. I have the strong feeling that you are completely
depending on the group to do your work. At the moment you are
not programming, you are just trying to insert statements into
your code without thinking to much. Then, when you recognize
that things don't work like you want them to be, you ask the group
to analyze what you have done, make suggestions on how to fix it
and fix it for you. You have to think on your own or you will never
get a hold in programming.

PS: Your question is easily answered with: You clear it where it
is required. I inserted the reference to a former problem of yours
into my reply for a reason: It was exactly the same problem, so how
did you fix it then?
 
K

kittykat

Last time i fixed the problem by rearranging the code. I didnt clear the
"text" vector as u initially suggested (because i found that other
problems came up).

I just tried clearing it where it was suggested, and it only couts the
first "binary element" of each line.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top