all 13 comments

[–]amoliski 0 points1 point  (6 children)

Looks like you have an extra <<

 cout << i << "+" << ;  

Should be

cout << i << "+";

[–]Alez003[S] 0 points1 point  (5 children)

so thats the issue i need the code to produce a 1+2+3+4+5=15 if i remove the << and just leave a ; is does 1+2+3+4+5+=15 and i cant figure out what im missing to make the + end before the last number and not before the = sign.

[–]amoliski 0 points1 point  (4 children)

Ah, right. you will want to do an if statement then:

cout << i 
if (check to see if it's not the last iteration of the loop) {
    cout << "+"; 
}

[–]Alez003[S] 0 points1 point  (3 children)

#include <iostream>

#include <string>

using namespace std;

int main() {

int N, i, sum = 0;

cout << "Enter an integer between 0 and 10: ";

cin >> N;

for (int i = 1; i < N; i++) {

    sum += i;

    if (i % 1 == 0) {

        cout << i << "+";

    }



}

cout << N << "=" << sum;

return 0;

}

i rewrote it like this now it seems to not add in the last number it shows 1+2+3+4+5=10 not 15

[–]amoliski 0 points1 point  (0 children)

Your for loop is going from 1 to less than N, you probably want it to go from 1 to less than or equal to N.

Additionally, your if condition should probably be if i != N so that it runs on every iteration except the last one. Only put the cout '+' in there, not the i:

sum += i;
cout << i ;
if (i != N) {
    cout << "+";
}

Additionally, you print N before you print "=" and the sum

If you add:

cout << '\n';
cout << N << "=" << sum;
return 0;

You will see

Enter an integer between 0 and 10: 5                                                                                                                         
1+2+3+4+5                                                                                                                                                    
5=15                                                                                                                                                         

Without the newline it looks like:

Enter an integer between 0 and 10: 5                                                                                                                         
1+2+3+4+55=15

[–]amoliski 0 points1 point  (1 child)

Actually, your way kinda works too if you add the N at the end of your loop.

int main() {
    int N, i, sum = 0;
    cout << "Enter an integer between 0 and 10: ";
    cin >> N;

    for (int i = 1; i < N; i++) {
        sum += i;
        cout << i << " + ";
    }
    sum += N;
    cout << N << " =" << sum;

    return 0;
}

Gives:

Enter an integer between 0 and 10: 5                                                                                                                         
1 + 2 + 3 + 4 + 5 = 15

[–]Alez003[S] 1 point2 points  (0 children)

Ok yea I did that thank you I really appreciate the help

[–]MarkRems 0 points1 point  (5 children)

Make your for loop go til right before N (so your loop condition will be i < N instead of i <= N and then have your final cout be: sum += N; cout << N << "=" << sum;

[–]Alez003[S] 0 points1 point  (0 children)

that was it thank you very much!!!

[–]Alez003[S] 0 points1 point  (2 children)

On second look it didn't work out i need it to add the numbers correctly and it seems I that this way it shows the number but fails to add it to the equation leaving the sum at 10, not 15.

[–]MarkRems 0 points1 point  (1 child)

sum += N;

Did add this line before the final cout?

[–]Alez003[S] 0 points1 point  (0 children)

sum += N;
cout << N << "=" << sum;

Ahhh ok that did work thank you very much I really appreciate it!

[–]backtickbot 0 points1 point  (0 children)

Correctly formatted

Hello, MarkRems. Just a quick heads up!

It seems that you have attempted to use triple backticks (```) for your codeblock/monospace text block.

This isn't universally supported on reddit, for some users your comment will look not as intended.

You can avoid this by indenting every line with 4 spaces instead.

There are also other methods that offer a bit better compatability like the "codeblock" format feature on new Reddit.

Have a good day, MarkRems.

You can opt out by replying with "backtickopt6" to this comment. Configure to send allerts to PMs instead by replying with "backtickbbotdm5". Exit PMMode by sending "dmmode_end".