confused about this exercise

Okay, so there's this program on C++ primer.
It says to write your own version of a program that prints the sum of a set of integers read from cin.
I don't understand what it's asking, so I just put the previous example it showed us.
Is there another way to write this or am I reading the exercise wrong?

1
2
3
4
5
6
7
8
9
10
  #include <iostream>
int main()
{
    int sum = 0, value = 0;
    while (std::cin >> value)
        sum += value;
    std::cout << "Sum is " << sum << std::endl;
    return 0;
}


More often than not, I'm not able to actually "write my own version" of whatever it is they are asking me to write my own version of the first time I encounter such exercises. I'm going to assume the problem you're facing is the problem I faced, they teach you a bunch of new stuff and you don't really get what they actually do because hey, first time seeing it.

As you go along the chapters things get clearer and if you go back, you can definitely write your own version of whatever it is. Or you could bullhead your way on the exercise and try to come up with something else. I think it's just designed for you to look closely at what they're doing instead of skimming through their examples.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
int main()
{
    int sum = 0, value = 0;
    int limit = 0;

    std::cout << "Set a limit on the set: ";
    std::cin >> limit;

    //Impose limit and check that cin is still in a good state.
    while (std::cin >> value && (--limit != 0))
        sum += value;
    std::cout << "Sum is " << sum << std::endl;
    return 0;
}


There are still other stuff that could be done, like actually checking if the input for limit is actually an int, have a cout in the while loop to update in real time what the running sum is, etc. More importantly imo is understanding what the code they have written is actually doing.
Last edited on
How would you recommend learning it?
Just reading the book or reading and doing the exercises?
Reading and doing the exercises. I skip some exercises due to frustration but I go back to them later on. At least for me, just reading doesn't work at all.
Any specific exercises you skip?
Olysold, I tried your code with Code::Blocks and MinGW, but it omits the last entry from the total. This is quite a puzzle to me! Donnie
Last edited on
@lurks

Can't remember, just anything that was too frustrating to deal with.

@Donnie

I made a mistake. Part of the while loop's condition is to prompt the user for an input, but doesn't actually enter the loop to update the total on the last limit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
int main()
{
    int sum = 0, value = 0;
    int limit = 0;

    std::cout << "Set a limit on the set: ";
    std::cin >> limit;

    //Impose limit and check that cin is still in a good state.
    while (std::cin.good() && limit-- != 0)
    {
        std::cin >> value;
        sum += value;
    }
    std::cout << "Sum is " << sum << std::endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.