-1

I have a C++ string in my code that is like:

"1 2 3 4 5 6 7 8"

I know the string is composed of integers separated by a space char. How can I sum them?

I'm quite a C++ newbie and in Java I'd simply do:

String str = "1 2 3 4 5 6 7 8";
int sum = 0;


for (int i = 0; i < str.split(" ").length; i++ {
    sum += Integer.parse(str.split(" ")[i];
}

How can I do just like this with my string object in C++?

Some people suggested me stringstream but I still can't understand this object and I need to read the string entirely, getting every single digit within it.

Thanks in advance!

Update: some guys nicely tried to help me but still it's not working. Perhaps because of some quirk of my problem which I haven't clarified before. So here it goes:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;


int main()
{
freopen("variable-exercise.in", "r", stdin);

int sum = 0, start = 0;
string line;


while(getline(cin ,line)) {
    istringstream iss(line);

    while(iss >> start) {
        sum += start;
    }

    cout << start << endl;
    sum = start = 0;
}

return 0;
}

Ah, the input file contains the following:

1
3 4
8 1 1
7 2 9 3
1 1 1 1 1
0 1 2 5 6 10

So, for each line, the program must print the sum of all integers in the string line. This example would generate:

1
7
10
21
5
24

thanks

rodrigoalvesvieira
  • 7,895
  • 15
  • 63
  • 84

3 Answers3

5

Some people suggested me stringstream but I still can't understand this object and I need to read the string entirely

I guess you were given a good advice. With std::istringstream you could just read in values one after the other as you would read them from the standard input (or any other input stream).

For instance:

#include <sstream>
#include <string>
#include <iostream>

int main()
{
    // Suppose at some time you have this string...
    std::string s = "1 2 3 4 5 6 7 8 9 10";

    // You can create an istringstream object from it...
    std::istringstream iss(s);

    int i = 0;
    int sum = 0;

    // And read all values one after the other...
    while (iss >> i)
    {
        // ...of course updating the sum each time
        sum += i;
    }

    std::cout << sum;
}
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • What's the reason for explicitly constructing an `std::string`? –  Jun 27 '13 at 18:33
  • 1
    @H2CO3: No reason in particular, I thought that at some point the OP may end up with a `string` object (perhaps somehow constructed by some other function) and wanted to get the numbers out of it. – Andy Prowl Jun 27 '13 at 18:34
  • Andy is correct. My update describes the problem better and show real world scenario. Hope you guys are still willing to look at it. – rodrigoalvesvieira Jun 27 '13 at 18:40
  • @rodrigoalves So, did you look at my answer or something? –  Jun 27 '13 at 18:50
0

Like this:

std::stringstream s("1 2 3 4 5 6 7 8 9");
int n = 0;
int x;
while (s >> x)
    n += x;

std::cout << n << std::endl;

After your edit:

cout << start << endl;

This is wrong, you should be printing sum instead:

cout << sum << endl;
  • @rodrigoalves (What kind of update? There's no edit made to your question.) Well, it **must** work. [Demonstration.](http://ideone.com/OpIIPH) –  Jun 27 '13 at 18:36
  • Sorry, I commented before actually finishing the update. There it is. I'm sorry. – rodrigoalvesvieira Jun 27 '13 at 18:39
  • 1
    @rodrigoalves **Read** your code. You are not printing the sum but the last value. See edit. –  Jun 27 '13 at 18:44
0

I used C code to solve this problem. Here's the final solution:

#include <stdio.h>
#include <string.h>

int main() {
    char *c;
    char line[100];
    int x, sum = 0;

    while(gets(line)) {
        for(c = strtok(line, " "); c ; c = strtok(NULL, " ")) {
            sscanf(c, "%d", &x);
            sum += x;
        }

        printf("%d\n", sum);
        sum = 0;
    }

    return 0;
}

Hope it helps anyone who might have the same problem!

rodrigoalvesvieira
  • 7,895
  • 15
  • 63
  • 84