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! 12 1088
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-
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
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
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??
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
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...
yes...you are right. where in the code would it be the right place to clear
it?
how can i make it count over each line seperatly?
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
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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sims |
last post by:
Hi,
Given a string $txt and an array of strings $txt_array
what would be the best/fastest way to search in _insensitive_ case if $txt
is in $text_array and, if it is, where is it?
Because I...
|
by: Jack Smith |
last post by:
Help needed on this question. Any help is appreciated. Thanks in
advance.
Given a binary string (i.e. a finite sequence of 0's and 1's) we
choose any two digit substring 01 and replace it by a...
|
by: worzel |
last post by:
Hi All,
I am looking for a reg ex that will match email addresses withing <a
href=mailto blah > links.
Actually, I already crafted my own, but with a slight problem:
<a...
|
by: Willie |
last post by:
I try to setup SQL Server to work with .NET Framework
Anyone here willing to help?
Step By Step Help Needed.
Thanks
|
by: tbird2340 |
last post by:
I want to write an if / then statement and have tried using this:
var MyVarMailto;
if (Request.Form("LoanRequest") == "Under $250,000") {
if (Request.Form("Organization") == "1") {
MyVarMailto...
|
by: BHPexpert |
last post by:
Regular Expression help needed
--------------------------------------------------------------------------------
I want to extract all text that is contained inside the brackets after the word...
|
by: Brian Gallagher |
last post by:
I have an aspnet application which I only want part of to be avaliable
between the hours of 9am and 5:30pm but I dont know where to start.
Help needed!
|
by: Christopher |
last post by:
Urgent Help Needed: The EPVH-1.1 Visual Hull Library.
Dear All,
I am a student doing research in computer vision. The EPVH-1.1 Visual
Hull Library will really help a lot in my research.
I...
|
by: Joel Fireman |
last post by:
Help Needed: Upgrade Fedora 4 / Apache 2 to PHP 5.2.x from 5.0.4
I've been testing Joomla as a content manager for the County offices,
and it looks pretty good. Unfortunately, I decided to...
|
by: oasis |
last post by:
HELP NEEDED : I have developed a testing system thats generates question paper. i want to display this paper to 20 different users in a Server/Client architecture.
is there any built-in function...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |