1

It's my first time with C++.

i searched other question, but i couldn't solve my problem :(

I just want to print all of element of list and print sum.

But, i got some problem of char "+"

This is my code.

#include <iostream>
#include <list>
using namespace std;

int main() {
    int count = 0, sum = 0;
    list<int> square;
    int n, m;
    cout << "Please enter tow positive integers between M and N -> ";
    cin >> m >> n;
    while (m>=n) {
        cout << "Please try agrin" << endl;
        cin >> m >> n;
    }
    for (int i = 1; i <= n; i=i+1) {
        /*cout << i << endl;*/
        if (i*i >= m && i*i <= n) {
            square.push_back(i*i);
            count=count+1;
            sum = sum + (i*i); 
            //cout << i << endl;
        }
    }
    for (list<int>::iterator i = square.begin(); i != square.end(); ++i)
        cout << *i << "+";
    cout << "=" << sum;
    return 0;
}

I got output like below.

16+25+36+49+64+81+100+=371

but i wanna get like below.

16+25+36+49+64+81+100=371

that has no "+" char at last element.

But i don't know how to do.

Is there any idea for this?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Jiwon Kim
  • 139
  • 3
  • 12
  • 1
    Iterate to the back of your list and print the last element after the loop. – George Nov 24 '17 at 08:01
  • @George Your point is valid. It seems however to be improveable by rephrasing with more details. Consider making an answer to have more formatting features available. – Yunnosch Nov 24 '17 at 08:04
  • [Here](https://stackoverflow.com/questions/3496982/printing-lists-with-commas-c) you can find a lot of different solution to your problem – Oneiros Nov 24 '17 at 08:27

3 Answers3

3

Check in every iteration if you are not printing the last element with (std::prev), and then print the plus sign.

Like this:

for (list<int>::iterator i = square.begin(); i != square.end(); ++i) {
    cout << *i; if(i != std::prev(square.end())) cout << "+";
}

PS: In order to avoid checking the iterator twice in every iteration, you would need to print with the plus sign appended until the prelast element and then print the last element outside the loop without the plus sign.

Example:

for (list<int>::iterator i = square.begin(); i != std::prev(square.end()); ++i)
    cout << *i << "+";
cout << *std::prev(square.end());
cout << "=" << sum;
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • not the most efficient though, because it checks the iterator twice each time. – ROX Nov 24 '17 at 11:02
  • 1
    @ROX I agree, and in fact this was my initial approach, but I was downvotes, thus changed course. I now injected it back to my answer, what to you say? – gsamaras Nov 24 '17 at 11:37
  • yes, that's what I was thinking, given the = sign means there was always going to be a special case at the end of the loop anyway ( although I suppose one could put that in the loop too ). don't know why it attracted downvotes - no comments to explain that. – ROX Nov 24 '17 at 11:51
  • 1
    by the way typo outise -> outside (I couldn't just correct it for you due to stupid rule about minimum number of characters in edit) – ROX Nov 24 '17 at 11:54
1

Using a ostream_joiner one can solve your problem elegantly:

#include <experimental/iterator>
#include <algorithm>

std::copy(cbegin(square), cend(square),
          std::experimental::make_ostream_joiner(std::cout, "+"));
std::cout << "=" << sum;

It depends on your compiler whether ostream_joiner is already implemented.

sv90
  • 522
  • 5
  • 11
0

You can print the numbers while iterating and print the '+' only between the elements:

for (int i = 1; i <= n; i=i+1) {
    if (i*i >= m && i*i <= n) {
        if (count) // If we already printed a number then include also a plus sign
        {
            cout << "+";
        }
        cout << (i*i);
        square.push_back(i*i);
        count=count+1;
        sum = sum + (i*i); 
    }
}

cout << "=" << sum;

Or if yo dont want to mix the output code with the other yo can modify only the print loop:

for (list<int>::iterator i = square.begin(); i != square.end(); ++i)
{
    if (i != square.begin()) 
    {
        cout <<"+"; 
    }
    cout << *i;
}
Mihayl
  • 3,821
  • 2
  • 13
  • 32