How to read input several lines of integers seperated by spaces (in C++)?
up vote
-3
down vote
favorite
So there are Q lines.
Each line has an arbitrary number of integers separated by spaces, I need to process those numbers.
Example Input
Q = 4
12 32 4 3 2
1 2 3 4
0
2 3 1 223 4 2 3
I am reading each line as string and extracting the numbers as string and then using atoi to convert them to int, ie,
while(Q>0){
for(char c:s){
if (c==' '){
int x = atoi(temp.c_str());
temp = "";
//process x
continue;
}
temp +=c;
}
Q--;
}
Surely there is a better way to do this?
Edit: The numbers of each line are processed differently.
For example, say
Line 1 has 1,2,3,4. Line 2 has 14,15.
Then 1,2,3,4 will be processed differently and 13,14 differently.
This is why I can't use std::cin as it ignores all whitespaces (both spaces and newline).
c++ atoi
|
show 1 more comment
up vote
-3
down vote
favorite
So there are Q lines.
Each line has an arbitrary number of integers separated by spaces, I need to process those numbers.
Example Input
Q = 4
12 32 4 3 2
1 2 3 4
0
2 3 1 223 4 2 3
I am reading each line as string and extracting the numbers as string and then using atoi to convert them to int, ie,
while(Q>0){
for(char c:s){
if (c==' '){
int x = atoi(temp.c_str());
temp = "";
//process x
continue;
}
temp +=c;
}
Q--;
}
Surely there is a better way to do this?
Edit: The numbers of each line are processed differently.
For example, say
Line 1 has 1,2,3,4. Line 2 has 14,15.
Then 1,2,3,4 will be processed differently and 13,14 differently.
This is why I can't use std::cin as it ignores all whitespaces (both spaces and newline).
c++ atoi
I'd just read each line and parse using regex.
– Ryan Pierce Williams
Nov 11 at 11:24
@RyanPierceWilliams could you please elaborate how to do that?
– Little_idiot
Nov 11 at 11:29
You could use a regex like this on each line: "([d]+)" to find all integers.
– Ryan Pierce Williams
Nov 11 at 11:32
Try it out here: regex101.com
– Ryan Pierce Williams
Nov 11 at 11:41
Is there a pattern on how you process them differently?
– Andreas DM
Nov 11 at 12:10
|
show 1 more comment
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
So there are Q lines.
Each line has an arbitrary number of integers separated by spaces, I need to process those numbers.
Example Input
Q = 4
12 32 4 3 2
1 2 3 4
0
2 3 1 223 4 2 3
I am reading each line as string and extracting the numbers as string and then using atoi to convert them to int, ie,
while(Q>0){
for(char c:s){
if (c==' '){
int x = atoi(temp.c_str());
temp = "";
//process x
continue;
}
temp +=c;
}
Q--;
}
Surely there is a better way to do this?
Edit: The numbers of each line are processed differently.
For example, say
Line 1 has 1,2,3,4. Line 2 has 14,15.
Then 1,2,3,4 will be processed differently and 13,14 differently.
This is why I can't use std::cin as it ignores all whitespaces (both spaces and newline).
c++ atoi
So there are Q lines.
Each line has an arbitrary number of integers separated by spaces, I need to process those numbers.
Example Input
Q = 4
12 32 4 3 2
1 2 3 4
0
2 3 1 223 4 2 3
I am reading each line as string and extracting the numbers as string and then using atoi to convert them to int, ie,
while(Q>0){
for(char c:s){
if (c==' '){
int x = atoi(temp.c_str());
temp = "";
//process x
continue;
}
temp +=c;
}
Q--;
}
Surely there is a better way to do this?
Edit: The numbers of each line are processed differently.
For example, say
Line 1 has 1,2,3,4. Line 2 has 14,15.
Then 1,2,3,4 will be processed differently and 13,14 differently.
This is why I can't use std::cin as it ignores all whitespaces (both spaces and newline).
c++ atoi
c++ atoi
edited Nov 11 at 11:32
asked Nov 11 at 11:21
Little_idiot
12
12
I'd just read each line and parse using regex.
– Ryan Pierce Williams
Nov 11 at 11:24
@RyanPierceWilliams could you please elaborate how to do that?
– Little_idiot
Nov 11 at 11:29
You could use a regex like this on each line: "([d]+)" to find all integers.
– Ryan Pierce Williams
Nov 11 at 11:32
Try it out here: regex101.com
– Ryan Pierce Williams
Nov 11 at 11:41
Is there a pattern on how you process them differently?
– Andreas DM
Nov 11 at 12:10
|
show 1 more comment
I'd just read each line and parse using regex.
– Ryan Pierce Williams
Nov 11 at 11:24
@RyanPierceWilliams could you please elaborate how to do that?
– Little_idiot
Nov 11 at 11:29
You could use a regex like this on each line: "([d]+)" to find all integers.
– Ryan Pierce Williams
Nov 11 at 11:32
Try it out here: regex101.com
– Ryan Pierce Williams
Nov 11 at 11:41
Is there a pattern on how you process them differently?
– Andreas DM
Nov 11 at 12:10
I'd just read each line and parse using regex.
– Ryan Pierce Williams
Nov 11 at 11:24
I'd just read each line and parse using regex.
– Ryan Pierce Williams
Nov 11 at 11:24
@RyanPierceWilliams could you please elaborate how to do that?
– Little_idiot
Nov 11 at 11:29
@RyanPierceWilliams could you please elaborate how to do that?
– Little_idiot
Nov 11 at 11:29
You could use a regex like this on each line: "([d]+)" to find all integers.
– Ryan Pierce Williams
Nov 11 at 11:32
You could use a regex like this on each line: "([d]+)" to find all integers.
– Ryan Pierce Williams
Nov 11 at 11:32
Try it out here: regex101.com
– Ryan Pierce Williams
Nov 11 at 11:41
Try it out here: regex101.com
– Ryan Pierce Williams
Nov 11 at 11:41
Is there a pattern on how you process them differently?
– Andreas DM
Nov 11 at 12:10
Is there a pattern on how you process them differently?
– Andreas DM
Nov 11 at 12:10
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
1
down vote
#include <iostream>
// ...
int x;
while (std::cin >> x)
; // process x
edited cause of your comment:
#include <iostream>
// ...
int x;
char ch;
while (std::cin >> x >> std::noskipws >> ch >> std::skipws) {
if (ch == 'n')
; // process x differently
}
cin ignores all whitespaces by default. I need to separately process integers belonging to each line. Example line 1: 1 2 3 4 line 2: 12 34 Here 1,2,3,4 will be processed differently and 12,34 differently.
– Little_idiot
Nov 11 at 11:26
@Little_idiot you didn't say so in your question.
– Swordfish
Nov 11 at 11:29
Sorry fixed it now.
– Little_idiot
Nov 11 at 11:42
@Little_idiot fixed my answer as well.
– Swordfish
Nov 11 at 11:42
add a comment |
up vote
0
down vote
If you need line oriented parsing and still want to use the power of iostream then I suggest you use std::getline and std::istringstream. Example below
#include <iostream>
#include <sstream>
int main() {
char ch = '';
std::cin >> ch;
if (!std::cin || ch != 'Q') return EXIT_FAILURE;
std::cin >> ch;
if (!std::cin || ch != '=') return EXIT_FAILURE;
int Q = 0;
std::cin >> Q;
if (!std::cin) return EXIT_FAILURE;
std::string line;
std::getline(std::cin, line); // get ready for a new line
while (Q > 0) {
if (!std::getline(std::cin, line)) return EXIT_FAILURE;
std::istringstream iss(line);
int count = 0;
int sum = 0;
int i;
while (iss >> i) {
++count;
sum += i;
}
std::cout << "Found " << count << " integers who's sum was " << sum << 'n';
Q--;
}
std::cout << "Donen";
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
#include <iostream>
// ...
int x;
while (std::cin >> x)
; // process x
edited cause of your comment:
#include <iostream>
// ...
int x;
char ch;
while (std::cin >> x >> std::noskipws >> ch >> std::skipws) {
if (ch == 'n')
; // process x differently
}
cin ignores all whitespaces by default. I need to separately process integers belonging to each line. Example line 1: 1 2 3 4 line 2: 12 34 Here 1,2,3,4 will be processed differently and 12,34 differently.
– Little_idiot
Nov 11 at 11:26
@Little_idiot you didn't say so in your question.
– Swordfish
Nov 11 at 11:29
Sorry fixed it now.
– Little_idiot
Nov 11 at 11:42
@Little_idiot fixed my answer as well.
– Swordfish
Nov 11 at 11:42
add a comment |
up vote
1
down vote
#include <iostream>
// ...
int x;
while (std::cin >> x)
; // process x
edited cause of your comment:
#include <iostream>
// ...
int x;
char ch;
while (std::cin >> x >> std::noskipws >> ch >> std::skipws) {
if (ch == 'n')
; // process x differently
}
cin ignores all whitespaces by default. I need to separately process integers belonging to each line. Example line 1: 1 2 3 4 line 2: 12 34 Here 1,2,3,4 will be processed differently and 12,34 differently.
– Little_idiot
Nov 11 at 11:26
@Little_idiot you didn't say so in your question.
– Swordfish
Nov 11 at 11:29
Sorry fixed it now.
– Little_idiot
Nov 11 at 11:42
@Little_idiot fixed my answer as well.
– Swordfish
Nov 11 at 11:42
add a comment |
up vote
1
down vote
up vote
1
down vote
#include <iostream>
// ...
int x;
while (std::cin >> x)
; // process x
edited cause of your comment:
#include <iostream>
// ...
int x;
char ch;
while (std::cin >> x >> std::noskipws >> ch >> std::skipws) {
if (ch == 'n')
; // process x differently
}
#include <iostream>
// ...
int x;
while (std::cin >> x)
; // process x
edited cause of your comment:
#include <iostream>
// ...
int x;
char ch;
while (std::cin >> x >> std::noskipws >> ch >> std::skipws) {
if (ch == 'n')
; // process x differently
}
edited Nov 11 at 11:36
answered Nov 11 at 11:25
Swordfish
8,86511335
8,86511335
cin ignores all whitespaces by default. I need to separately process integers belonging to each line. Example line 1: 1 2 3 4 line 2: 12 34 Here 1,2,3,4 will be processed differently and 12,34 differently.
– Little_idiot
Nov 11 at 11:26
@Little_idiot you didn't say so in your question.
– Swordfish
Nov 11 at 11:29
Sorry fixed it now.
– Little_idiot
Nov 11 at 11:42
@Little_idiot fixed my answer as well.
– Swordfish
Nov 11 at 11:42
add a comment |
cin ignores all whitespaces by default. I need to separately process integers belonging to each line. Example line 1: 1 2 3 4 line 2: 12 34 Here 1,2,3,4 will be processed differently and 12,34 differently.
– Little_idiot
Nov 11 at 11:26
@Little_idiot you didn't say so in your question.
– Swordfish
Nov 11 at 11:29
Sorry fixed it now.
– Little_idiot
Nov 11 at 11:42
@Little_idiot fixed my answer as well.
– Swordfish
Nov 11 at 11:42
cin ignores all whitespaces by default. I need to separately process integers belonging to each line. Example line 1: 1 2 3 4 line 2: 12 34 Here 1,2,3,4 will be processed differently and 12,34 differently.
– Little_idiot
Nov 11 at 11:26
cin ignores all whitespaces by default. I need to separately process integers belonging to each line. Example line 1: 1 2 3 4 line 2: 12 34 Here 1,2,3,4 will be processed differently and 12,34 differently.
– Little_idiot
Nov 11 at 11:26
@Little_idiot you didn't say so in your question.
– Swordfish
Nov 11 at 11:29
@Little_idiot you didn't say so in your question.
– Swordfish
Nov 11 at 11:29
Sorry fixed it now.
– Little_idiot
Nov 11 at 11:42
Sorry fixed it now.
– Little_idiot
Nov 11 at 11:42
@Little_idiot fixed my answer as well.
– Swordfish
Nov 11 at 11:42
@Little_idiot fixed my answer as well.
– Swordfish
Nov 11 at 11:42
add a comment |
up vote
0
down vote
If you need line oriented parsing and still want to use the power of iostream then I suggest you use std::getline and std::istringstream. Example below
#include <iostream>
#include <sstream>
int main() {
char ch = '';
std::cin >> ch;
if (!std::cin || ch != 'Q') return EXIT_FAILURE;
std::cin >> ch;
if (!std::cin || ch != '=') return EXIT_FAILURE;
int Q = 0;
std::cin >> Q;
if (!std::cin) return EXIT_FAILURE;
std::string line;
std::getline(std::cin, line); // get ready for a new line
while (Q > 0) {
if (!std::getline(std::cin, line)) return EXIT_FAILURE;
std::istringstream iss(line);
int count = 0;
int sum = 0;
int i;
while (iss >> i) {
++count;
sum += i;
}
std::cout << "Found " << count << " integers who's sum was " << sum << 'n';
Q--;
}
std::cout << "Donen";
}
add a comment |
up vote
0
down vote
If you need line oriented parsing and still want to use the power of iostream then I suggest you use std::getline and std::istringstream. Example below
#include <iostream>
#include <sstream>
int main() {
char ch = '';
std::cin >> ch;
if (!std::cin || ch != 'Q') return EXIT_FAILURE;
std::cin >> ch;
if (!std::cin || ch != '=') return EXIT_FAILURE;
int Q = 0;
std::cin >> Q;
if (!std::cin) return EXIT_FAILURE;
std::string line;
std::getline(std::cin, line); // get ready for a new line
while (Q > 0) {
if (!std::getline(std::cin, line)) return EXIT_FAILURE;
std::istringstream iss(line);
int count = 0;
int sum = 0;
int i;
while (iss >> i) {
++count;
sum += i;
}
std::cout << "Found " << count << " integers who's sum was " << sum << 'n';
Q--;
}
std::cout << "Donen";
}
add a comment |
up vote
0
down vote
up vote
0
down vote
If you need line oriented parsing and still want to use the power of iostream then I suggest you use std::getline and std::istringstream. Example below
#include <iostream>
#include <sstream>
int main() {
char ch = '';
std::cin >> ch;
if (!std::cin || ch != 'Q') return EXIT_FAILURE;
std::cin >> ch;
if (!std::cin || ch != '=') return EXIT_FAILURE;
int Q = 0;
std::cin >> Q;
if (!std::cin) return EXIT_FAILURE;
std::string line;
std::getline(std::cin, line); // get ready for a new line
while (Q > 0) {
if (!std::getline(std::cin, line)) return EXIT_FAILURE;
std::istringstream iss(line);
int count = 0;
int sum = 0;
int i;
while (iss >> i) {
++count;
sum += i;
}
std::cout << "Found " << count << " integers who's sum was " << sum << 'n';
Q--;
}
std::cout << "Donen";
}
If you need line oriented parsing and still want to use the power of iostream then I suggest you use std::getline and std::istringstream. Example below
#include <iostream>
#include <sstream>
int main() {
char ch = '';
std::cin >> ch;
if (!std::cin || ch != 'Q') return EXIT_FAILURE;
std::cin >> ch;
if (!std::cin || ch != '=') return EXIT_FAILURE;
int Q = 0;
std::cin >> Q;
if (!std::cin) return EXIT_FAILURE;
std::string line;
std::getline(std::cin, line); // get ready for a new line
while (Q > 0) {
if (!std::getline(std::cin, line)) return EXIT_FAILURE;
std::istringstream iss(line);
int count = 0;
int sum = 0;
int i;
while (iss >> i) {
++count;
sum += i;
}
std::cout << "Found " << count << " integers who's sum was " << sum << 'n';
Q--;
}
std::cout << "Donen";
}
answered Nov 11 at 12:40
Bo R
601110
601110
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248204%2fhow-to-read-input-several-lines-of-integers-seperated-by-spaces-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'd just read each line and parse using regex.
– Ryan Pierce Williams
Nov 11 at 11:24
@RyanPierceWilliams could you please elaborate how to do that?
– Little_idiot
Nov 11 at 11:29
You could use a regex like this on each line: "([d]+)" to find all integers.
– Ryan Pierce Williams
Nov 11 at 11:32
Try it out here: regex101.com
– Ryan Pierce Williams
Nov 11 at 11:41
Is there a pattern on how you process them differently?
– Andreas DM
Nov 11 at 12:10