By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Help needed!!

P: n/a
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[i] <<
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!

Jul 23 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

kittykat wrote:
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-

Jul 23 '05 #2

P: n/a
kittykat wrote:
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[i] <<
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
Jul 23 '05 #3

P: n/a
kittykat wrote:

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?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #4

P: n/a
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??
Jul 23 '05 #5

P: n/a
kittykat wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6

P: n/a
kittykat wrote:
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...
Jul 23 '05 #7

P: n/a
yes...you are right. where in the code would it be the right place to clear
it?

Jul 23 '05 #8

P: n/a
how can i make it count over each line seperatly?

Jul 23 '05 #9

P: n/a
kittykat wrote:
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
Jul 23 '05 #10

P: n/a
kittykat wrote:
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.
Jul 23 '05 #11

P: n/a
kittykat wrote:

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?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #12

P: n/a
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.

Jul 23 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.