very simple program crashes

Hi guys,I'm just wondering why my program crashes and gets a runtime error,I get no compiler messages and the program builds and runs it says program terminate called after throwing an instance of 'std:out_of_range_ on the terminal window

but I don't know why I get an out of range exception

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <string>
using namespace std;

int main()
{

   string s1 = "hello";
   string s2("hello");

   for(int i =0; s2.length();i++){

        cout << s2.at(i);

   }
}


sorry guys I forgot the less than sign my bad,problem solved if anyone still wants to tell me why the program actually runs and builds without the condition would be great as I thought it would generate a compiler error
Last edited on
Consider your loop's conditional expression:

for(int i =0; s2.length();i++){

s2 is never changed, so it always has length.

i will eventually be larger than s2's indexible elements.

string::at() throws an exception when i>=s2.length().

Hope this helps.
that makes sense =) thanks
Topic archived. No new replies allowed.