< Back to forum

reasons for infinite loop

int main() {

int i,j; cin>>i>>j;

while(i>0){ int k; j=k; while(k>0){

cout<<"*"; k--; } i--;
}

return 0;

}

Asked by: Anonymous on May 13, 2022, 8:15 a.m. Last updated on May 13, 2022, 8:15 a.m.


It should be k=j, instead of j=k. k was declared but now initialized so, it have large garbage value initially, and you wrote j=k. So every time k is having large garbage value, so its taking too much time

abhishekzobin02052000 last updated on May 16, 2022, 12:52 p.m.

Enter your answer details below:


Preview

Enter your comment details below:

Preview




1 Answer(s)

avatar

Here you have not taken the value of k so compiler is giving a garbage value and if the garbage value is big then it will print '*' repeatedly.

You should have done k = j instead of j = k.

rakeshraman272611 last updated on May 16, 2022, 12:53 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.