Simple for loop question.

How do I get rid of that plus sign?

http://postimg.org/image/5dfggoh3r/

#include <iostream>
using namespace std;

int main ()

{
int i, n;
int sum = 0;


cout << "--------------------------------------------------------------------" << endl;
cout << "This program will sum up from 0 to the number user entered." << endl;
cout << "--------------------------------------------------------------------" << endl;

cout << "Please enter a whole numer: ";
cin >> n;
system ( "cls" );

for ( i = 1;i <= n;i++ )
{

sum = sum + i;

if ( i < n )
{
cout << i << " + ";
}



}



cout << " = " << sum << endl;
cout << endl;
cout << "The number you entered is: " << n << endl;
cout << "You ran the loop " << n << " times" << endl;
cout << "The total added up from 0 to " << n << " is: " << sum << endl;

return 0;

}
you just get rid off of that plus sign.
closed account (18hRX9L8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

int main () {
    int i, n;
    int sum = 0;

    cout << "--------------------------------------------------------------------" << endl;
    cout << "This program will sum up from 0 to the number user entered." << endl;
    cout << "--------------------------------------------------------------------" << endl;

    cout << "Please enter a whole numer: ";
    cin >> n;

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

    cout << " = " << sum << endl;
    cout << endl;
    cout << "The number you entered is: " << n << endl;
    cout << "You ran the loop " << n << " times" << endl;
    cout << "The total added up from 0 to " << n << " is: " << sum << endl;

    return 0;
}


This works. Just loop until the last one and then custom-cout it without the plus sign.
Last edited on
YEAHHHHHHHH!!!!!!!! it works!!! thank you "usandfriends"!
Topic archived. No new replies allowed.