Can you find the infinite loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tmthomp3
    New Member
    • Oct 2006
    • 1

    Can you find the infinite loop

    i am trying to write a program but it is not working the way it should?

    can you find the infinite loop...



    #include <iostream>
    using namespace std;

    int main() {
    int input;
    int InputCount=0;
    int RowSize=2;
    int ColumId = 0;

    cin >> input;
    if (input == 0);
    cout << "done";

    while((input <= -1)||(input >= 1));
    {
    while (InputCount <= RowSize){

    if (InputCount == 0);
    ColumId =(InputCount % RowSize);
    cout << ColumId << endl;
    cout << input;
    InputCount ++;

    if (InputCount == 1);
    ColumId =(InputCount % RowSize);
    cout << ColumId << endl;
    cout << input;
    InputCount ++;

    if (InputCount == 2);
    ColumId =(InputCount % RowSize);
    cout << ColumId << endl;
    cout << input;
    InputCount ++;

    }

    }}
  • analwarsi
    New Member
    • Oct 2006
    • 7

    #2
    The Outer most while loop of ur program will exit only if the value of input is 0.but the value of input is never modified which means if u enter input=2. then its going to retain that value throughout the program and that is why u r getting infinite loop. i hope that this should solve ur problem
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    int input;
    int InputCount=0;
    int RowSize=2;
    int ColumId = 0;
    
    cin >> input;
    if (input == 0);
    cout << "done";
    
    [B]while((input <= -1)||(input >= 1));[/B]
    {
    while (InputCount <= RowSize){
    
    if (InputCount == 0);
    ColumId =(InputCount % RowSize);
    cout << ColumId << endl;
    cout <<[B] input;[/B]
    InputCount ++;
    
    if (InputCount == 1);
    ColumId =(InputCount % RowSize);
    cout << ColumId << endl;
    cout <<[B] input;[/B]
    InputCount ++;
    
    if (InputCount == 2);
    ColumId =(InputCount % RowSize);
    cout << ColumId << endl;
    cout << [B]input;[/B]
    InputCount ++;
    
    }
    
    }}

    Comment

    • analwarsi
      New Member
      • Oct 2006
      • 7

      #3
      One more problem in ur code.see the highlighted line i.e; the first while loop u have put a semicolon at the end of the line which should be removed.
      Code:
      #include <iostream>
      using namespace std;
      
      int main() {
      int input;
      int InputCount=0;
      int RowSize=2;
      int ColumId = 0;
      
      cin >> input;
      if (input == 0);
      cout << "done";
      
      [B]while((input <= -1)||(input >= 1));[/B]
      {
      while (InputCount <= RowSize){
      
      if (InputCount == 0);
      ColumId =(InputCount % RowSize);
      cout << ColumId << endl;
      cout << input;
      InputCount ++;
      
      if (InputCount == 1);
      ColumId =(InputCount % RowSize);
      cout << ColumId << endl;
      cout << input;
      InputCount ++;
      
      if (InputCount == 2);
      ColumId =(InputCount % RowSize);
      cout << ColumId << endl;
      cout << input;
      InputCount ++;
      
      }
      
      }}

      Comment

      • pragatiswain
        Recognized Expert New Member
        • Nov 2006
        • 96

        #4
        Optimized code is given below. Hope this helps.

        #include <iostream>
        using namespace std;

        int main()
        {
        int input;
        int InputCount=0;
        int RowSize=2;
        int ColumId = 0;

        cout << "Please specify the Input (Integer only): ";
        cin >> input;

        if (input == 0) cout << "Done" << endl;

        if ((input <= -1)||(input >= 1))
        {
        while (InputCount <= RowSize)
        {
        ColumId =(InputCount % RowSize);
        cout << ColumId << endl;
        InputCount ++;
        }
        cout << "Input Value: " << input << endl;
        }
        }

        Comment

        Working...