Can anyone modify this VB code to C++ please?

Hi Guys!

I am very very new to C++. This is a code that I wrote in vb.NET previously.
This is a button click command:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Dim p As New List(Of Long)
            k = 3
            m = 2
            p.Add(m)
            For n = 1 To k
                For l = 0 To n
                    If m Mod p(l) = 0 Then
                        m = m + 1
                    End If
                Next l
                p.Add(m)
            Next n
            Beep()
            Me.TextBox2.Text = m
I tried:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::list<int> p;
int k = 3;
int m = 2;
p.push_back(m);
for( int n = 1; n < k; n++ ) {
for( int l = 0; l < n; l++ ) {
if ( m % p(l) == 0 );
{
m = m + 1;
};
};
p.push_back(m);
};
System::String^ b = m.ToString(); 
textBox2->Text = b;
};
I am sure I am using functions wrong like "push_back".
Ofcourse it's not working. Can you help me?
Thanks a lot!
Last edited on
Line 7 has a null body because of the trailing semi-colon.

What it really says is:

1
2
3
4
5
if (m % p(l) == 0)
  ;
{
  m = m + 1;
}


Also, you don't need semi-colons after closing braces. Except after class definitionsdeclarations. Logical, huh?

Apart from that, your code should be fine.

Jim
Last edited on
Realy? I'll try :) Thanks. Also for the line

if (m % p(l) == 0)

it is underlying p and says:
"expression must have integral or unscoped enum type."

Why?
Ah yes, missed that, sorry - the index operator is [], not (), so

 
if (m % p[l] == 0)


Jim
Same line:
if (m % p[l] == 0)Now, it is underlining p again and saying:
"expression must have pointer-to-object or handle-to-C++/CLI-array type"
And also:
"binary '[' : 'std::list<_Ty>' does not define this operator or a conversion to a type acceptable to the predefined operator"
Error for the whole line.
Last edited on
I changed my list to vector and it did the trick!

Thanks a lot for everything :)
Topic archived. No new replies allowed.