Блог пользователя rashid_aziz

Автор rashid_aziz, история, 23 месяца назад, По-английски

include

using namespace std; int main() { string s = "abcd"; int i = -1; while(i < s.size()){ cout << "inside "; i++; } cout << "outside"; return 0; }

why this loop does not run, where as when we initialize i = 0, then it runs properly

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
23 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by rashid_aziz (previous revision, new revision, compare).

»
23 месяца назад, # |
  Проголосовать: нравится +17 Проголосовать: не нравится

You make a comparison between i, which is a signed int, and s.size(), which of an unsigned type. One of them must be casted to the type of the other one in order for the comparison to be calculated. Try casting s.size() to int manually and the code will run