No output...

M

Mike Wahler

supratik chakraborty said:
I am not getting any output for the following lines of code...

while(line_buf>>value)
{
cout<<"Pushing a value in the list"<<endl;
line_list.push_front(value);//add the value to the list
cout<<"Got the value in the list"<<endl;
}

The above given code isn't executed.For some reason the while loop in the
code doesn't gets executed.

I have defined line_buf as the following:

std::istringstream line_buf(line);

What are the definition and contents of 'line'?

-Mike
 
S

supratik chakraborty

I am not getting any output for the following lines of code...

while(line_buf>>value)
{
cout<<"Pushing a value in the list"<<endl;
line_list.push_front(value);//add the value to the list
cout<<"Got the value in the list"<<endl;
}

The above given code isn't executed.For some reason the while loop in the
code doesn't gets executed.

I have defined line_buf as the following:

std::istringstream line_buf(line);

Any help would be appreciated...
 
K

Karl Heinz Buchegger

supratik said:
I am not getting any output for the following lines of code...

while(line_buf>>value)
{
cout<<"Pushing a value in the list"<<endl;
line_list.push_front(value);//add the value to the list
cout<<"Got the value in the list"<<endl;
}

The above given code isn't executed.For some reason the while loop in the
code doesn't gets executed.

So what might that reason be?
If a while loop body is not executed, then it is because the
while condition evaluates to false.
In your case: the extraction of value has failed.

To sort this issue out, we need more details:
what is the data type of value?
what is stored in line_buf?
has the string stream already gone into a fail state?
etc...
 
S

supratik chakraborty

What are the definition and contents of 'line'?

-Mike
Line is a string.The string takes in values from a .txt file.The text file
has its data arranged as follows:

1,2,3
3,4,5
6,7,8
......
......
 
M

Mike Wahler

supratik chakraborty said:
Line is a string.The string takes in values from a .txt file.The text file
has its data arranged as follows:

1,2,3
3,4,5
6,7,8

What is the value of 'line' at the point you use
it to initialize 'line_buf'?

Also, have you checked the stream states?

This is like pulling teeth. :)

-Mike
 
T

Thomas Matthews

supratik said:
Line is a string.The string takes in values from a .txt file.The text file
has its data arranged as follows:

1,2,3
3,4,5
6,7,8
.....
.....
You may be getting a error because the comma (',') is not a valid
character for creating an integer.
Try adding this after you push the value:
char comma;
line_buf.get(comma);

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
S

supratik chakraborty

You may be getting a error because the comma (',') is not a valid
character for creating an integer.
Try adding this after you push the value:
char comma;
line_buf.get(comma);

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
I am using the replace function to replace the commas by white spaces.Is
there some way to change the number in the strings to a float..?
 
M

Mike Wahler

supratik chakraborty said:
I am using the replace function to replace the commas by white spaces.Is
there some way to change the number in the strings to a float..?

#include <iostream>
#include <sstream>

int main()
{
float f(0);
std::istringstream iss("3.14");
iss >> f;
std::cout << f << '\n';
return 0;
}

-Mike
 
S

Stephen Howe

Look at your original post again:
while(line_buf>>value)
{
cout<<"Pushing a value in the list"<<endl;
line_list.push_front(value);//add the value to the list
cout<<"Got the value in the list"<<endl;
}
We don't know what type line_buf is, we still don't know what type value is.
You should post _COMPLETE_ _COMPILABLE_ code that illustrates your problem.
Why should we have to spend ages extracting information out of you,
information that you should provided in your first post?
It is not difficult to provide this. It is not rocket science.

Assuming value is numeric, the above won't work will it?

The first time though the loop with "1,2,3"

the "1" will be extracted. But on the 2nd extraction, it will fail on the
comma as that is not a numeric value.

Stephen Howe
 
S

Stephen Howe

What exactly do you mean by stream states..?

He means the state of istringstream.

All streams can have 4 bits set: goodbit, eofbit, badbit or failbit.

So have a look at

line_buf.rdstate()

For your istringstream to be functional, none of eofbit, badbit or failbit
should be set.

failbit will be set if you try extracting a type that the istringstream does
not contain.
eofbit will be set if you the istringstream is empty.
badbit will be set if you the istringstream is non-operational from the
start.

Stephen Howe
 
S

supratik chakraborty

He means the state of istringstream.

All streams can have 4 bits set: goodbit, eofbit, badbit or failbit.

So have a look at

line_buf.rdstate()

For your istringstream to be functional, none of eofbit, badbit or failbit
should be set.

failbit will be set if you try extracting a type that the istringstream does
not contain.
eofbit will be set if you the istringstream is empty.
badbit will be set if you the istringstream is non-operational from the
start.

Stephen Howe
Sorry abt not being very specific on the newsgroup.This won't happen
again :=).I think I have fixed my problem regarding the while loop.
I have come up with this new problem where I have to export my data to a
..txt file.

My input file has data as follows:

1,2,3
4,5,6
7,8,9
......
......

My outputfile has data in the following form:

1,2,3,4,5,6,7,8,9.........
I want the data in my ouput file to be in the same form as I have in my
output file.

The lines in my input .txt file is stored as a list in a deque.To export
all the data in my structure I am doing the following:
code snippet::
std::eek:fstream o_file("output.txt");//My output file
for(int i=0;i<=(counter--);i++)//I am looping through the size of my array
{ /*Since there's a list in each of the cells of the array,I am creating a
reference to each of the lists in my array cells.
*/
std::list<float>& list4 = data;
std::copy(list4.begin(),list4.end(),ostream_iterator<float>(o_file,","));
//The above given line exports my list to my output file
}

I think I am missing something in the given snippet that will cause the
newline to occur for the new list in the output file.Any help be hugely
appreciated.
 
R

red floyd

supratik said:
[other stuff redacted]
1,2,3,4,5,6,7,8,9.........
I want the data in my ouput file to be in the same form as I have in my
output file.

The lines in my input .txt file is stored as a list in a deque.To export
all the data in my structure I am doing the following:
code snippet::
std::eek:fstream o_file("output.txt");//My output file
for(int i=0;i<=(counter--);i++)//I am looping through the size of my array
{ /*Since there's a list in each of the cells of the array,I am creating a
reference to each of the lists in my array cells.
*/
std::list<float>& list4 = data;
std::copy(list4.begin(),list4.end(),ostream_iterator<float>(o_file,","));
//The above given line exports my list to my output file
}

I think I am missing something in the given snippet that will cause the
newline to occur for the new list in the output file.Any help be hugely
appreciated.


try putting this after your loop:

ofile << std::endl;

IANALG (I am not a language guru), but I don't believe std::copy with an ostream_iterator
puts a newline.
 

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

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top