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

No output...

P: n/a
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...
Jul 19 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a


supratik chakraborty wrote:

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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2

P: n/a

supratik chakraborty <sc******@students.uiuc.edu> wrote in message
news:Pi**************************************@ux7. cso.uiuc.edu...
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
Jul 19 '05 #3

P: n/a


On Wed, 13 Aug 2003, Mike Wahler wrote:

supratik chakraborty <sc******@students.uiuc.edu> wrote in message
news:Pi**************************************@ux7. cso.uiuc.edu...
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

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
......
......
Jul 19 '05 #4

P: n/a

supratik chakraborty <sc******@students.uiuc.edu> wrote in message
news:Pi**************************************@ux12 .cso.uiuc.edu...


On Wed, 13 Aug 2003, Mike Wahler wrote:

supratik chakraborty <sc******@students.uiuc.edu> wrote in message
news:Pi**************************************@ux7. cso.uiuc.edu...
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

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

Jul 19 '05 #5

P: n/a
supratik chakraborty wrote:

On Wed, 13 Aug 2003, Mike Wahler wrote:

supratik chakraborty <sc******@students.uiuc.edu> wrote in message
news:Pi**************************************@ux 7.cso.uiuc.edu...
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


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

Jul 19 '05 #6

P: n/a


On Wed, 13 Aug 2003, Thomas Matthews wrote:
supratik chakraborty wrote:

On Wed, 13 Aug 2003, Mike Wahler wrote:

supratik chakraborty <sc******@students.uiuc.edu> wrote in message
news:Pi**************************************@ux 7.cso.uiuc.edu...

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


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

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..?

Jul 19 '05 #7

P: n/a

supratik chakraborty <sc******@students.uiuc.edu> wrote in message
news:Pi**************************************@ux13 .cso.uiuc.edu...


On Wed, 13 Aug 2003, Thomas Matthews wrote:
supratik chakraborty wrote:

On Wed, 13 Aug 2003, Mike Wahler wrote:
>supratik chakraborty <sc******@students.uiuc.edu> wrote in message
>news:Pi**************************************@ux 7.cso.uiuc.edu...
>
>>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
>
>
>

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

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

Jul 19 '05 #8

P: n/a
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
Jul 19 '05 #9

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

P: n/a


On Thu, 14 Aug 2003, Stephen Howe wrote:
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

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::ofstream 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[i];
std::copy(list4.begin(),list4.end(),ostream_iterat or<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.

Jul 19 '05 #11

P: n/a
supratik chakraborty wrote:
[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::ofstream 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[i];
std::copy(list4.begin(),list4.end(),ostream_iterat or<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.

Jul 19 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.