473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

ticky problem

I am in a situation wherein I want to process input line by line .
Each line contians numbers.

sample input :

1 2 3 4 5
5 6 7
1
2 3 4 56 7 8
0
my program must take 1 2 3 4 5 first -process it -5 6 7 -process
it and likewise. How can i do it without considering each line as a
string and seperating out numbers ? Can I directly use some kind of
loop ? and how is the loop going to terminate ?
May 31 '08 #1
7 1358
onkar wrote:
I am in a situation wherein I want to process input line by line .
Each line contians numbers.

sample input :

1 2 3 4 5
5 6 7
1
2 3 4 56 7 8
0
my program must take 1 2 3 4 5 first -process it -5 6 7 -process
it and likewise. How can i do it without considering each line as a
string and seperating out numbers ? Can I directly use some kind of
loop ? and how is the loop going to terminate ?
The easiest way is probably read the input line by line with
getline(), create an istringstream from that line, and then read the
values with >until there are no more.
May 31 '08 #2
On 2008-05-31 11:24:46 -0400, onkar <on*******@gmail.comsaid:
I am in a situation wherein I want to process input line by line .
Each line contians numbers.

sample input :

1 2 3 4 5
5 6 7
1
2 3 4 56 7 8
0
my program must take 1 2 3 4 5 first -process it -5 6 7 -process
it and likewise. How can i do it without considering each line as a
string and seperating out numbers ?
That's the usual way to do it. Why don't you want to do it this way?
Can I directly use some kind of
loop ? and how is the loop going to terminate ?
Sure. You can read a character at a time, parse the numbers and
separators, and start processing the line when you hit a newline.
That's a lot more work than the usual approach.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

May 31 '08 #3
onkar wrote:
I am in a situation wherein I want to process input line by line .
Each line contians numbers.

sample input :

1 2 3 4 5
5 6 7
1
2 3 4 56 7 8
0
my program must take 1 2 3 4 5 first -process it -5 6 7 -process
it and likewise. How can i do it without considering each line as a
string and seperating out numbers ? Can I directly use some kind of
loop ? and how is the loop going to terminate ?
I recently had to do something like this. I decided to treat the input file
as a set of logical records, rather than as a set of lines. A record can
span multiple lines. Each record is terminated by a semicolon.

I have a loop that uses the >operator to read tokens into a string
variable (called Token!). Each token string is then pushed into a list of
strings (list<std:stringfor further processing. A record is complete when
I find a token that is (or ends with) a semicolon.

The need for a semicolon terminator is necessary to give me the flexibility
to have records spanning multiple lines.

If you don't need this flexibility then the approach posted by juha Nieminem
looks sensible to me.

Chris Gordon-Smith
www.simsoup.info
May 31 '08 #4
onkar <on*******@gmail.comwrote:
I am in a situation wherein I want to process input line by line .
Each line contians numbers.

sample input :

1 2 3 4 5
5 6 7
1
2 3 4 56 7 8
0
my program must take 1 2 3 4 5 first -process it -5 6 7 -process
it and likewise. How can i do it without considering each line as a
string and seperating out numbers ? Can I directly use some kind of
loop ? and how is the loop going to terminate ?
void process( const vector<int>& numbers );

void foo( istream& is )
{
string str;
while ( getline( is, str ) )
{
istringstream iss( str );
vector<intnumbers;
copy( istream_iterator<int>( iss ), istream_iterator<int>(),
back_inserter( numbers ) );
process( numbers );
}
}

What's wrong with the above?
May 31 '08 #5
On 2008-05-31 18:58, Chris Gordon-Smith wrote:
onkar wrote:
>I am in a situation wherein I want to process input line by line .
Each line contians numbers.

sample input :

1 2 3 4 5
5 6 7
1
2 3 4 56 7 8
0
my program must take 1 2 3 4 5 first -process it -5 6 7 -process
it and likewise. How can i do it without considering each line as a
string and seperating out numbers ? Can I directly use some kind of
loop ? and how is the loop going to terminate ?

I recently had to do something like this. I decided to treat the input file
as a set of logical records, rather than as a set of lines. A record can
span multiple lines. Each record is terminated by a semicolon.

I have a loop that uses the >operator to read tokens into a string
variable (called Token!). Each token string is then pushed into a list of
strings (list<std:stringfor further processing. A record is complete when
I find a token that is (or ends with) a semicolon.
Could you not just have used getline(file, string, ';') instead of doing
multiple >operations?

--
Erik Wikström
May 31 '08 #6
Erik Wikström wrote:
On 2008-05-31 18:58, Chris Gordon-Smith wrote:
>onkar wrote:
>>I am in a situation wherein I want to process input line by line .
Each line contians numbers.

sample input :

1 2 3 4 5
5 6 7
1
2 3 4 56 7 8
0
my program must take 1 2 3 4 5 first -process it -5 6 7 -process
it and likewise. How can i do it without considering each line as a
string and seperating out numbers ? Can I directly use some kind of
loop ? and how is the loop going to terminate ?

I recently had to do something like this. I decided to treat the input
file as a set of logical records, rather than as a set of lines. A record
can span multiple lines. Each record is terminated by a semicolon.

I have a loop that uses the >operator to read tokens into a string
variable (called Token!). Each token string is then pushed into a list of
strings (list<std:stringfor further processing. A record is complete
when I find a token that is (or ends with) a semicolon.

Could you not just have used getline(file, string, ';') instead of doing
multiple >operations?
Possibly, although I would have still had to separate out the tokens.
getline on its own would just give me a single string without separating
out tokens.

(If I had known about the alternate version of getline you quote I probably
would indeed have used it!)

Chris Gordon-Smith
www.simsoup.info
May 31 '08 #7
On May 31, 8:26*pm, Chris Gordon-Smith <use.addr...@my.homepage>
wrote:
Erik Wikström wrote:
On 2008-05-31 18:58, Chris Gordon-Smith wrote:
onkar wrote:
>I am in a situation wherein I want to process *input line by line .
Each line contians numbers.
>sample input :
>1 2 3 4 5
5 6 7
1
2 3 4 56 7 8
0
>my program must take 1 2 3 4 5 first -process it -5 6 7 -process
it and likewise. How can i do it without considering each line as a
string and seperating out numbers ? Can I directly use some kind of
loop ? and how is the loop going to terminate ?
I recently had to do something like this. I decided to treat the input
file as a set of logical records, rather than as a set of lines. A record
can span multiple lines. Each record is terminated by a semicolon.
I have a loop that uses the >operator to read tokens into a string
variable (called Token!). Each token string is then pushed into a list of
strings (list<std:stringfor further processing. A record is complete
when I find a token that is (or ends with) a semicolon.
Could you not just have used getline(file, string, ';') instead of doing
multiple >operations?

Possibly, although I would have still had to separate out the tokens.
getline on its own would just give me a single string without separating
out tokens.

(If I had known about the alternate version of getline you quote I probably
would indeed have used it!)

Chris Gordon-Smithwww.simsoup.info
Can't you just:

std::vector<intnumbers(0);
std::vector<std::vector <int numberGroups(0);
while(cin){
while (cin.peek() != '\n'){
int number;
cin >number;
numbers.push_back(number);
}
numberGroups.push_back(numbers);
//yourfunctionhere e.g. weirdfunction(numberGroups);
numbers.clear();
}

Regards,

Wanas
http://programmingden.blogspot.com
Jun 1 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
117
by: Peter Olcott | last post by:
www.halting-problem.com
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.