Is this code wrong?

So here is the code coming from the array tutorial here in this site

i try to copy paste it to try and see the output
apparently it looks like its broken . But Im not sure about that maybe is there some thing i dont understand

So heres what happen whne i run it

It ouput infinite 7208852
so is it Wronng OR something i need to understand

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

using namespace std;

int main()
{
  int myarray[3] = {10,20,30};

  for (int i=0; i<3; ++i)
    ++myarray[i];

  for (int elem : myarray)
    cout << elem << '\n';
}
Your code is fine. Something else is the issue, perhaps your build is out of date, try cleaning your project.

Output:
11
21
31
Last edited on
What do you mean cleaning your project?

And build is out of date?
I meant that your program hasn't been re-compiled since you changed your program.

In IDEs, there is usually a "Clean" or "Rebuild" button (e.g. Visual Studio).
If you don't have this in your environment, just try deleting all your .o (object) files and .exe/.out (executable) files, or whatever it is that is generated when you compile your program.

You can see that your code is fine using a web compiler:
http://cpp.sh/5w7dg
Last edited on
Im using code blocks

And i found the rebuild button but it stillss doesnt work hahaha

Maybe on the codeblock version??
Code::Blocks 17.12, and the code works just fine as Ganado mentioned.
Registered users can post here. Sign in or register to post.