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!
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!