ticky problem

O

onkar

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

Juha Nieminen

onkar said:
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.
 
C

Chris Gordon-Smith

onkar said:
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:string> for 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
 
E

Erik Wikström

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:string> for 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?
 
C

Chris Gordon-Smith

Erik said:
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
 
E

El-Hassan Wanas

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<int> numbers(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
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top