for loop not working as expected

This needs to print two columns of numbers, but it just prints the number 11. I'm sure I'm missing something simple, but any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>

using namespace std;


int main(){
    vector <int> vec{1, 2, 3};
   int result{};
for (size_t i {0}; i <vec.size(); ++i)
for (size_t j {i+1}; j <vec.size(); ++j)
result = result + vec.at(i) * vec.at(j);
cout <<result;
   cout << endl;
  
    return 0;

}
http://www.cplusplus.com/doc/tutorial/control/ writes:
Many of the flow control statements explained in this section require a generic (sub)statement as part of its syntax. This statement may either be a simple C++ statement, -such as a single instruction, terminated with a semicolon (;) - or a compound statement. A compound statement is a group of statements (each of them terminated by its own semicolon), but all grouped together in a block, enclosed in curly braces: {}:

{ statement1; statement2; statement3; }

The entire block is considered a single statement (composed itself of multiple substatements). Whenever a generic statement is part of the syntax of a flow control statement, this can either be a simple statement or a compound statement.

We can add braces around a simple statement; it does not change the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>

using namespace std;

int main(){
   vector <int> vec{1, 2, 3};
   int result{};
   for (size_t i {0}; i <vec.size(); ++i) {
      for (size_t j {i+1}; j <vec.size(); ++j) {
         result = result + vec.at(i) * vec.at(j);
      }
   }
   cout <<result;
   cout << endl;
   return 0;
}

Does this style change clarify the logic that you have written?
It does...I understand the need for the braces, however, this does not solve what I need to do for this or something close to this to produce two columns of numbers, each first and second number separated by a comma.
nevermind...i figured it out
Topic archived. No new replies allowed.