while(true) break executing but not other code

I have a testing code where I try to cout "got here" when I have a condition and then break out of an infinite loop, but it's not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main(){
  int count = 0;
vector<int> c; 
   while(true){
c.clear();
c.push_back(count); //testing if clear works and if c only contains the latest element


    if (count ==99){
    cout<<"got here!"<<endl; //not executed
   cout<<c[0]<<endl; //also not executed
break;
    }
    count++;
}
}


My code doesn't run forever, but got here isn't executed.
Last edited on
In function 'int main()': 11:3: error: 'class std::vector<int>' has no member named 'pushback'

That code does not compile, so it's hard to say for certain what the problem is.
When I change pushback to push_back, "got here!" is executed just fine for me.

Are you sure you're posting correct, up-to-date code?

(Also your formatting is frankly atrocious. Please use consistent indentation.)

My code doesn't run forever, but got here isn't executed.
What do you mean by "got here isn't executed"? Do you mean you are not seeing anything printed? Is your console staying open long enough to see the result?

Try using a debugger and putting a break point on line 15...
Last edited on
Topic archived. No new replies allowed.